2013-09-28 65 views
0

我正在写一个函数,获取用户输入并根据用户输入的内容执行操作。我正在使用一个case语句来检查用户输入的内容。C++键输入检查

我很难看到用户是否按住home,end,ins和del。默认变量如HOME,INSERT,DELETE和END似乎没有效果,但箭头键使用LEFT和RIGHT。我在GNU/Linux中这样做。我不确定我做错了什么。

任何帮助将不胜感激。

+2

如果你表现出你有这将是人们更容易地帮助你,你怎么刚从的getchar例如读取 – lijat

+0

用户输入的代码()函数 –

回答

0

这里是问题:

#include <stdio.h> 

//Compiled on GNU/Linux 
//By: Saulius Grybas 


int main() 
{ 
     int key; 
     bool done = false; 

    while (!done) 
    { 

     key = getchar(); 

     switch (key){ 
       case HOME: 
         //Home key is pressed/perform action 
         done = true; 
         break; 
       case END: 
         //END key is pressed/perform action 
         done = true; 
         break; 
       case DEL: 
         //DEL key is presed/perform action 
         done = true; 
         break; 
       case BACKSPACE: 
         //backspace is pressed/perform action 
         done = true; 
         break; 
       default: 
         done = false; 
         break; 
     } 
       printf ("%d%s\n", key, " - Integer of key is pressed!"); 
    } 

    return 0; 
} 
+0

您的常量HOME END DEL和BACKSPACE似乎没有被定义,是您的问题。对我来说,发布的代码不会编译,它是为你编译的吗? – lijat

+0

我知道。我正在寻找代表这些击键的关键代码,因此我可以用关键代码替换“keystroke”。例如:案例0x10://是输入,我相信,但我似乎无法得到上述字符。另外如果我输入'\ b'是退格。 –

+0

你可以运行 int c = getchar(); printf(“code%d \ n”,c); 并看看你得到什么?或者当你按下这些键时,你的问题getchar不会返回? – lijat

0

这些定义用于按键的扫描代码(IBM PC)。所有数字都是十进制的。

#define PAGE_UP  73 
#define HOME  71 
#define END   79 
#define PAGE_DOWN 81 
#define UP_ARROW 72 
#define LEFT_ARROW 75 
#define DOWN_ARROW 80 
#define RIGHT_ARROW 77 
#define F1   59 
#define F2   60 
#define F3   61 
#define F4   62 
#define F5   63 
#define F6   64 
#define F7   65 
#define F8   66 
#define F9   67 
#define F10   68 

#include <iostream> 
#include <conio.h> 

这里有Linux的扫描码:http://www.comptechdoc.org/os/linux/howlinuxworks/linux_hlkeycodes.html