linux - 如何在 Linux 上使用系统调用等待击键中断?

标签 linux assembly kernel nasm system-calls

我想在我的程序中,当用户按下像 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/

相关文章:

C++ 函数名称分解 : What does this name suffix mean?

linux - 在 Linux 内核模块中自定义 .mod.c MODULE_ALIAS

linux - Linux 中的共享 IRQ

c - 如何构建和部署 Linux 驱动程序?

linux - 是否可以在 Raspbian 操作系统上安装 pcsc-lite 1.8.13?

c++ - 如何循环 Blowfish Crypto++

linux - 如何为 Raspberry Pi Zero W 交叉编译 Rust 代码

assembly - Intel x86 汇编 - lt、gt、eq

linux - 在 GDB 中删除用户定义的命令

file - 什么是文件句柄以及它对程序员有用的地方?