获取当前pressed键用C? [英] Get currently pressed key in C?
问题描述
我需要能够获得当前的字符是没有完全停止程序pssed $ P $。理想情况下,我想程序继续运行,检查while循环的每次迭代后的关键。 残培()
就是这个原因问题。我想打出来时,Q键为pressed while循环,但对于while循环继续运行,直到然后。我使用Linux使用gcc作为我的编译器。
I need to be able to get the current character being pressed without stopping the program entirely. Ideally, I would like the program to continue running, checking for the key after every iteration of a while loop. getch()
is problematic for this reason. I want to break out of a while loop when the 'q' key is pressed, but for the while loop to continue running until then. I am using Linux with gcc as my compiler.
推荐答案
在实践中,你应该使用库并这取决于如果你正在编写一个终端应用程序或GUI之一。
In practice, you should use a library and it depends if you are writing a terminal application or a GUI one.
有关的终端应用的,阅读 TTY揭秘页面和使用< A HREF =https://www.gnu.org/software/ncurses/> ncurses的或的readline 一> 。注意,在烹调模式(这是默认值的情况下),一个 TTY 的具有内核管理行缓冲器,因此得到一个字符直通 GETC
是不可能的。另请参见的termios(3)和的 TTY(4)和的 tty_ioctl(4)(你可以采取的痛苦在原始模式-eg的设置TTY中的 http://shtrom.ssji.net/skb/getc.html 由 SJR 评论建议 - ,但我建议使用库)
For terminal applications, read the TTY demystified page and use ncurses or readline. Be aware that in cooked mode (which is the default case), a tty has a kernel-managed line buffer, hence getting a single character thru getc
is not possible. See also termios(3) and tty(4) and tty_ioctl(4) (you could take the pain the set the tty in raw mode -e.g. as in http://shtrom.ssji.net/skb/getc.html suggested in a comment by sjr-, but I recommend using a library).
有关的图形应用程序的桌面上运行,使用一些工具如 GTK 或<一个HREF =http://qt.io/> Qt的
For graphical applications running on a desktop, use some toolkit like GTK or Qt.
顺便说一句,在这两种情况下的编程模型是没有用天真的 GETC
功能更兼容。
BTW, in both cases the programming model is no more compatible with the naive getc
function.
阅读也高级Linux编程并意识到Linux有几个复用的系统调用(这些在系统调用(2)上市一>),尤其是调查(2)首位,而其可能使用的 ncurses的或的readline 或的Qt 或的Gtk 的库。
Read also Advanced Linux Programming and be aware that Linux has several multiplexing system calls (these are listed in syscalls(2)), notably poll(2) -which is probably used by ncurses or readline or Qt or Gtk libraries.
这篇关于获取当前pressed键用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!