我想在我的程序中,当用户按下像 F1-12 这样的特殊按键时接收到一个中断,这是用 nasm 编写的。我只需要在我的主要功能开始时等待功能击键。我知道这可以通过 BIOS 的 int 16h
实现,它返回一个扫描码。我如何在 Linux 下执行此操作?
最佳答案
为此所需的代码相当复杂;我最终想出了如何使用原始 ioctl、读取和写入在 C 中检查 F1。如果您熟悉汇编和 Linux 系统调用,那么转换为 nasm 应该很简单。
这并不是您想要的,因为它只检查 F1,而不检查其余部分。 F1的顺序是0x1b、0x4f、0x50。您可以使用 od -t x1
并按 键找到其他序列。例如F2为0x1b、0x4f、0x51。
基本思想是我们获取当前终端属性,将它们更新为原始 (cfmakeraw),然后将它们设置回去。 ioctl 系统调用用于此目的。
在处于原始模式的终端上,read()
将获取用户键入的任何字符,这与内核使用退格键和控制键进行行编辑的“cooked”模式不同u 直到用户通过按 enter 或 control-d (EOF) 提交该行。
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
struct ktermios {
tcflag_t c_iflag;
tcflag_t c_oflag;
tcflag_t c_cflag;
tcflag_t c_lflag;
cc_t c_line;
cc_t c_cc[19];
};
int getch() {
unsigned char c;
read(0, &c, sizeof(c));
return c;
}
int main(int argc, char *argv[]) {
struct ktermios orig, new;
ioctl(0, TCGETS, &orig);
ioctl(0, TCGETS, &new); // or more simply new = orig;
// from cfmakeraw documentation
new.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
new.c_oflag &= ~OPOST;
new.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
new.c_cflag &= ~(CSIZE | PARENB);
new.c_cflag |= CS8;
ioctl(0, TCSETS, &new);
while (1) {
if (getch() == 0x1b && getch() == 0x4f && getch() == 0x50) {
break;
}
}
write(1, "Got F1!\n", 8);
ioctl(0, TCSETS, &orig); // restore original settings before exiting!
return 0;
}
我基于 this answer ,这非常有帮助。
关于linux - 如何在 Linux 上使用系统调用等待击键中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27365528/