and that OP's sample program did not use cbreak (or raw). The manual page for cbreak says最初,终端可能处于或不处于cbreak模式,因为该模式是 遗传;因此,程序应显式调用 cbreak 或 nocbreak . 大多数使用curses的交互式程序都会设置cbreak模式. 请注意, cbreak 会覆盖 raw . (有关讨论,请参见 curs_getch(3x) 这些例程如何与 echo 和 noecho 进行交互.)Initially the terminal may or may not be in cbreak mode, as the mode is inherited; therefore, a program should call cbreak or nocbreak explicitly. Most interactive programs using curses set the cbreak mode. Note that cbreak overrides raw. (See curs_getch(3x) for a discussion of how these routines interact with echo and noecho.)此外,在 curs_getch 您可以阅读Also, in curs_getch you may read 预定义的功能键在<curses.h>中作为宏列出 值在8位字符范围之外.他们的名字以 KEY_ 开头. The predefined function keys are listed in <curses.h> as macros with values outside the range of 8-bit characters. Their names begin with KEY_.也就是说,如果程序调用keypad,则curses仅返回KEY_ENTER:That is, curses will only return KEY_ENTER if the program calls keypad:keypad(stdscr, TRUE);为了便于讨论,下面是一个示例,该示例修复了截至5月17日的示例程序中的一些问题:For the sake of discussion, here is an example fixing some of the problems with your sample program as of May 17:#include <stdio.h>#include <ncurses.h>#define ctrl(x) ((x) & 0x1f)intmain(void){ int c; initscr(); keypad(stdscr, TRUE); cbreak(); noecho(); nonl(); c = getch(); switch (c) { case KEY_ENTER: printw("\nkey_enter: %d", c); break; case ctrl('j'): printw("\nkey: ctrl j"); break; default: printw("\nkeyname: %d = %s\n", c, keyname(c)); break; } printw("\nnow press a key to end"); getch(); endwin(); return 0;}也就是说,您必须在getch之前调用keypad,并且 KEY_ENTER 返回的值不是字符(不能用%c打印).That is, you have to call keypad before getch, and the value returned for KEY_ENTER is not a character (it cannot be printed with %c).在具有常规终端说明的Linux控制台上运行,您将仅看到数字小键盘 Enter 的回车,因为该说明不使用 application模式. Linux控制台确实支持应用程序模式,并且可以编写相应的描述.快速检查(有差异...),您可以设置TERM=vt100以查看 KEY_ENTER .Running on the Linux console with the usual terminal description, you will see only carriage return for the numeric keypad Enter, because that description does not use application mode. Linux console does support application mode, and a corresponding description could be written. As a quick check (there are differences...) you could set TERM=vt100 to see the KEY_ENTER. 这篇关于在ncurses中捕获Control + Key的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-14 15:29