/* Initialize new terminal I/O settings */ static struct termios old,new1; void initTermios(int echo) { tcgetattr(0,&old); /* grab old terminal I/O settings */ new1 = old; /* make new settings same as old settings */ new1.c_lflag &= ~ICANON; /* disable buffered I/O */ new1.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */ tcsetattr(0,TCSANow,&new1); /* use these new terminal I/O settings Now */ } /* Restore old terminal I/O settings */ void resetTermios(void) { tcsetattr(0,&old); }
我怎样才能得到箭头键作为input(可能作为一个单一的字符),目前的代码适用于所有我需要的其他东西…请不要解决scheme与ncurses
净运行时间错误:.NET运行时2.0错误 – 事件ID:1000崩溃我的程序。 是什么造成的?
在Makefile教程中混淆Sed One-Liner
符号绑定如何在linux中的共享库中工作
基于证书的login
如何检测我的程序是否在Windows中运行?
你不能在标准的C ++ / C中做到这一点,你需要非标准的conio.h文件和getch()扩展名。 然后你可以调用getch()两次来获得箭头的关键代码
#include <conio.h> using namespace std; int main() { cout << "press up arrow;" << endl; int control = getch(); //gets the escape character int keycode = getch(); //gets the code cout << control << endl; cout << keycode << endl; }
你有没有试过read功能?
这适用于我与cygwin g ++,没有linux方便测试:
#include <unistd.h> #include <termios.h> #include <stdio.h> /* Initialize new terminal I/O settings */ static struct termios old,&old); } int main(void) { char c; initTermios(0); while (1) { read(0,&c,1); printf("%dn",c); } }
正如@Fiktik指出的那样,回声设置被破坏了,但是我没有改变地使用问题中的代码。
作为单个字符,这是不可能的(输入是一个字符序列)。 你可以做的是通过实现你自己的输入功能,使它看起来像一个字符。 就像是:
struct key_data { enum key_type kt; /* ARROW,FUNCTION,REGULAR,etc */ unsigned char key; /* depends on kt */ }; int get_key_press (struct key_data * kd) { int c = getc(stdin); switch (c) { /* If c is a control character,process further characters to see what needs to happen */ } return 0; } /* later,in main */ struct key_data kd; get_key_press (&kd); if (kd.kt == ARROW_KEY) { switch (kd.key) { case ARROW_RIGHT: printf("Right Arrow pressedn"); break; /* and so on */
所以在实现之后,你可以使用get_key_press来表现得像一个单一的字符。
虽然这项工作已经在别处完成了, 为什么厌恶ncurses ?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。