如何在C中收听箭头键的按下
我正在尝试收听上下箭头键,我尝试通过手动安装来使用conio.h,但也不起作用,我收到了一个错误。我正在使用Kali Linux 我的代码:如何在C中收听箭头键的按下,c,linux,C,Linux,我正在尝试收听上下箭头键,我尝试通过手动安装来使用conio.h,但也不起作用,我收到了一个错误。我正在使用Kali Linux 我的代码: #include <stdio.h> #include <conio.h> #include <curses.h> int main() { int ch = getch(); return 0; } #包括 #包括 #包括 int main(){ int ch=getch(); 返回0; } Erorr: /u
#include <stdio.h>
#include <conio.h>
#include <curses.h>
int main() {
int ch = getch();
return 0;
}
#包括
#包括
#包括
int main(){
int ch=getch();
返回0;
}
Erorr:
/usr/bin/ld:/tmp/cckPLPKN.o:在函数main':test.c:(.text+0x9):
未定义对getch'collect2的引用:错误:ld返回1个退出
地位
以下是读取循环中按下的字符并显示其代码的代码:
/* we use curses library */
#include <curses.h>
int main(void)
{
/* curses initialization */
initscr();
/* enable pressing arrow key to generate KEY_xxx codes */
keypad(stdscr, TRUE);
/* make getch wait sometime before returning ERR when no key is pressed */
timeout(100);
/* do not display pressed key */
noecho();
while (1)
{
/* read last pressed key */
int ch = getch();
/* if no key was waiting, ignore */
if (ERR == ch)
continue;
/* if UP is pressed... */
if (KEY_UP == ch)
{
/* ...do something with that key */
/* and wait next key pressed */
continue;
}
/* if Q is pressed, exit the loop */
if ('q' == ch)
break;
/* else, display the key code */
mvprintw(2,2,"code get: 0x%x\n", ch);
/* and some refresh can be useful*/
refresh();
}
/* before quitting, disable curses mode */
endwin();
return 0;
}
仅此而已。根据,您需要
标题和ncurses
库。您应该使用-lncurses
链接器选项。现在还不清楚你是否用过它。请显示编译和链接命令。conio
特定于较旧的Microsoft系统(DOS和Windows)-它不适用于Linux。您只需要ncurses
。上面给出的代码按预期工作,但有一件事我想从用户那里获取输入,同时我想听听箭头键,如果像linux shell一样按下或按下向上箭头键,但当我尝试在while循环中获取输入时,它会变得一团糟。
gcc -Wall main.c -lcurses -ocurses-test