如何在C语言中监听箭头按键?

3

我想监听上下箭头键。我尝试使用conio.h,但是手动安装后仍然无法正常工作,我遇到了一个错误。我正在使用Kali Linux。

我的代码:

#include <stdio.h>
#include <conio.h>
#include <curses.h>
int main() {

int ch = getch();

return 0;
}

错误:

/usr/bin/ld:/tmp/cckPLPKN.o:在函数“main”中:
test.c:(.text+0x9):对“getch”的引用未定义
collect2: error: ld返回了1个退出状态
注:这是一个与IT技术相关的错误信息。

4
根据getch(3)手册页面,你需要包含 <curses.h> 头文件并链接ncurses 库。需要使用链接选项 -lncurses。不确定你是否已经使用了它,请提供编译和链接命令。 - jww
2
conio是特定于旧版Microsoft系统(DOS和Windows)的库 - 它不适用于Linux。你只需要使用ncurses - John Bode
2个回答

4
这应该能解决问题:
对于Windows系统:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(){
   int ch;

   printf("Press # to Exit\n");

   while ((ch = getch()) != '#'){
      if (ch == 0 || ch == 224) {
         switch (getch()) {
            case 72:
               printf("\nUp Arrow");
               break;
            case 80:
               printf("\nDown Arrow");
               break;
            case 77:
               printf("\nRight Arrow");
               break;
            case 75:
               printf("\nLeft Arrow");
               break;
         }
      }
   }

   return 0;
}

对于Linux:

#include <curses.h>

int main() {
  
    int ch;
    
    /* Curses Initialisations */ 
    initscr();                  /* curses initialization */
    keypad(stdscr, TRUE);       /* enable KEY_UP/KEY_DOWN/KEY_RIGHT/KEY_LEFT */
    noecho();                   /* prevent displaying if other keys */
    
    printw("Press # to Exit\n");
 
    while ((ch = getch ()) != '#') {
        switch (ch) {
            case KEY_UP:
                printw ("\nUp Arrow");
                break;
            case KEY_DOWN:
                printw ("\nDown Arrow");
                break;
            case KEY_RIGHT:
                printw ("\nLeft Arrow");
                break;
            case KEY_LEFT:
                printw ("\nRight Arrow");
                break;
        }
    }
 
    return 0;
}

你会有:

enter image description here


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;
}

使用curses库编译:

gcc -Wall main.c -lcurses -ocurses-test

就是这样。


上述给定的代码按预期工作,但有一件事我想从用户那里获取输入,同时我也想监听箭头键,就像Linux shell一样,如果按下向上箭头键或向下箭头键。但是当我尝试在while循环中获取输入时,它变得混乱了。 - Osama Xäwãñz
如何在Windows上做同样的事情? - Scott

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接