C语言中的关键事件

Key events in the C language

如果你使用getc()或scanf(),你只知道用户按下回车后按下了什么按钮。 C语言有没有直接键盘输入的方法?

此外,我在 Linux (Raspberry pi) 上使用 gcc。

它是 OS 特定的(C99 或 C11 标准不知道键盘)。在 Linux(我在回答中重点关注)和 Unix 上,terminals are -for legacy and historical reasons- complex beasts (the kernel is implementing the line discipline), see tty demystified page. For gory details, see also termios(3) (the raw and noncanonical modes will give you a headache) & tty(4) & stty(1)

(你可能会花几个月的时间来学习血淋淋的细节;我忘记了大部分)

强烈推荐使用ncurses or readline (or termcap这样的库来编码,如果你够自虐的话)

如果使用 GUI,最好使用一些工具包,例如 Qt

要轮询一组文件描述符,请使用 poll(2)

另见 syscalls(2) and Advanced Linux Programming