2011-09-21 5 views
0

리눅스에서 ncurses를 사용하고 있습니다. getch()를 사용하여 입력 스트림에서 누른 다음 키를 가져 오지만 문자가 아닌 숫자를 반환합니다.아스키 문자에 대한 getch() 응답

Google에서 연구를 수행 한 후 getch()가 표준이 아니므로해야 할 일을 잃었습니다.

0xff, 0x4F00, 0x4700, 0x4800뿐만 아니라 0-9, 탭, ctrl, p, v, m, l, a, b, c, d, e, f 및 화살표 키가 필요합니다. , 0x5000, 0x4D00 :, 0x4B00, 0x4900, 0x5100. 이것들은 getch()의 반환 된 valus에 대한 구문 인 경우에 사용됩니다.

이것은 프로그램의 Windows 버전에서 코드를 다시 만들려고합니다.

unsigned long nr; 
if(GetNumberOfConsoleInputEvents(ConH,&nr)) 
{ 
    if(nr > 0) 
    { 
     INPUT_RECORD ipr; 
     ReadConsoleInput(ConH,&ipr,1,&nr); 
     if(ipr.EventType == KEY_EVENT && ipr.Event.KeyEvent.bKeyDown) 
     { 
      int key = ipr.Event.KeyEvent.uChar.AsciiChar; 
      if(key == 0) key = ipr.Event.KeyEvent.wVirtualScanCode<<8; 
      return key; 
     } 
    } 
} 
return 0; 

내가 getch의 결과에 사용할 수있는 기능이 있나요() 그래서 내가 위에서 본 .AsciiChar 같은 실제 누른 키, 뭔가를 얻을 수 있나요?

+0

값을 char에 할당하고 출력했는지 확인 했습니까? – PlasmaHH

+0

@PlasmaHH 예, 글자, 숫자 및 탭 키는 단일 숫자로 인쇄되고, 화살표 키는 세 개의 숫자로 인쇄됩니다. – Skeith

+0

그런 다음 int를 출력합니다. 그것들을 char 변수에 할당하고 char을 char처럼 출력하십시오 :'char c = getch(); std :: cout << c;' – PlasmaHH

답변

5

전공 수정 이전 예제를 없애고이 큰 것을 시도하십시오.

getch()의 반환 값은 ASCII 문자이거나 일부 특수 키의 curses 이름입니다.

#include <ncurses.h> 
#include <cctype> 

int main(int ac, char **av) 
{ 
    WINDOW* mainWin(initscr()); 
    cbreak(); 
    noecho(); 

    // Invoke keypad(x, true) to ensure that arrow keys produce KEY_UP, et al, 
    // and not multiple keystrokes. 
    keypad(mainWin, true); 

    mvprintw(0, 0, "press a key: "); 
    int ch; 

    // Note that getch() returns, among other things, the ASCII code of any key 
    // that is pressed. Notice that comparing the return from getch with 'q' 
    // works, since getch() returns the ASCII code 'q' if the users presses that key. 
    while((ch = getch()) != 'q') { 
     erase(); 
     move(0,0); 
     if(isascii(ch)) { 
     if(isprint(ch)) { 
      // Notice how the return code (if it is ascii) can be printed either 
      // as a character or as a numeric value. 
      printw("You pressed a printable ascii key: %c with value %d\n", ch, ch); 
     } else { 
      printw("You pressed an unprintable ascii key: %d\n", ch); 
     } 
     } 

     // Again, getch result compared against an ASCII value: '\t', a.k.a. 9 
     if(ch == '\t') { 
     printw("You pressed tab.\n"); 
     } 

     // For non-ASCII values, use the #define-s from <curses.h> 
     switch(ch) { 
     case KEY_UP: 
     printw("You pressed KEY_UP\n"); 
     break; 
     case KEY_DOWN: 
     printw("You pressed KEY_DOWN\n"); 
     break; 
     case KEY_LEFT: 
     printw("You pressed KEY_LEFT\n"); 
     break; 
     case KEY_RIGHT: 
     printw("You pressed KEY_RIGHT\n"); 
     break; 
     } 
     printw("Press another key, or 'q' to quit\n"); 
     refresh(); 
    } 

    endwin(); 
} 

참조 :

+0

탭은 9를 반환하고, 화살표 키는 3 개의 숫자를 반환하며, 예를 들어 27, 91, 65이며, 아스키 코드를 문자로 변환하는 방법에 대한 질문에는 대답하지 않습니다 . 나는 그 열쇠가 14 인을 g로 바꾸고 싶다고 알고 싶지 않다. – Skeith

+0

죄송합니다, 제가 게시 한 코드가 모든 것을 명확하게 만들었다 고 생각했습니다. 탭 키는 9를 반환하는데, 이는 '\ t'로도 알려져 있습니다. 그래서'if (ch == '\ t') return ch;는'if (ch == 9) return '\ t';'와 같습니다. 비슷하게,'if (isascii (ch)) return ch;'는 ascii 코드를 문자로 변환합니다. 화살표에 관해서는,'getch()'가 3 문자의 이스케이프 시퀀스를 수집하고 그것을'KEY_UP'으로 변환했다고 생각했습니다. 왜 당신의 프로그램이 그런 식으로 행동하지 않는지 나는 확신하지 못합니다. –

+0

@Skeith : C++에서는 "ascii 코드를 문자로 변환"하지 않습니다. char는 정수형이므로 char "g"를 원할 때는'char c = 'g';를 사용합니다. 그러나 ascii의 경우 'char c = 103;'(와 같이'std :: cout << + 'g';'와 같은 문자를 출력하면 반대의 결과를 얻을 수 있습니다. – PlasmaHH

관련 문제