我正在尝试熟悉ncurses,我遇到了一个问题 - KEY_LEFT之类的常量似乎是错误的。
例如,我正试图抓住键盘箭头。应该像
一样简单while (ch = getch()){
if (ch == KEY_LEFT)
foo();
}
但这不起作用。我写了ch
并说出来了 - 左箭头是68,右箭头是67,高达65,低于66.
这不会是一个问题,但是当试图抓住鼠标事件时,它会变坏。左键单击终端给出的值为33到742,最少点击左上角,大多数点击右下角。怎么了?
这是我的主要内容,以防万一
int main(void){
initscr();
start_color();
init_pair(1, COLOR_YELLOW, COLOR_WHITE);
init_pair(2, COLOR_RED, COLOR_RED);
cbreak();
//printw("Hai!\n");
noecho();
int width;
int height;
getmaxyx(stdscr, height, width);
int posx = 30;
int posy = 30;
const char* str = " ";
const char* hint = "ch = %d";
curs_set(0);
mousemask(BUTTON1_CLICKED, NULL);
unsigned int ch = 0;
while (ch = getch()) {
attron(COLOR_PAIR(1));
mvprintw(0, 0, hint, ch);
mvdelch(posy,posx);
mvdelch(posy, posx);
mvdelch(posy, posx);
switch (ch) {
case 68:
if (posx > 0) posx--;
//mvprintw(1,0,"LEFT");
break;
case 67:
if (posx < width) posx++;
break;
case 65:
if (posy > 0) posy--;
break;
case 66:
if (posy < height) posy++;
break;
case KEY_MOUSE:
MEVENT event;
if (getmouse(&event)==OK){
posx = event.x;
posy = event.y;
}
}
attron(COLOR_PAIR(2));
mvprintw(posy, posx, str);
refresh();
}
endwin();
return 0;
}
答案 0 :(得分:1)
正如(几乎)总是看一下手册有帮助。看看man getch
功能键 getch可能会返回curses.h中定义的以下功能键 如果键盘已启用。请注意,并非所有这些都必须支持 - 在任何特定终端上编辑。
好像你错过了
keypad (stdscr, TRUE);
在您的计划中。至少它不会出现在您发布的代码段中。