2013-09-28 64 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