如何在Linux上通过系统调用等待按键中断?
作者:互联网
当用户在程序中按nasm编写的特殊按键(如F1-12)时,我希望收到中断.我只需要在主要功能开始时等待功能击键.我知道使用BIOS的int 16h(返回扫描代码)是可能的.我如何在Linux下执行此操作?
解决方法:
所需的代码相当复杂.我最终想出了如何使用原始ioctl在C中检查F1,进行读取和写入的方法.如果您熟悉汇编和Linux系统调用,则将nasm的翻译应该很简单.
这并不是您想要的,因为它只检查F1,而不检查其余部分. F1的顺序为0x1b,0x4f,0x50.您可以使用od -t x1并按键找到其他序列.例如,F2是0x1b,0x4f,0x51.
基本思想是,我们获取当前的终端属性,将其更新为原始属性(cfmakeraw),然后将其重新设置. ioctl syscall用于此目的.
在原始模式的终端上,read()将获得用户键入的任何字符,这与“煮熟”模式不同,在“煮熟”模式下,内核使用退格键和control-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,这非常有帮助.
标签:assembly,nasm,system-calls,kernel,linux 来源: https://codeday.me/bug/20191121/2048131.html