开发者

convert getch() response to ascii character

I am using ncurses on linux. I use getch() to get the next key pressed from the input stream but it returns a number not a letter.

After doing research on google I have found that getch() is not standard so I am at a loss of what to do.

I need the keys 0-9, tab, ctrl, p,v,m,l,a,b,c,d,e,f, and the arrow keys as well as 0xff, 0x4F00, 0x4700, 0x4800, 0x5000, 0x4D00:, 0x4B00, 0x4900, 0x5100. These are what are uses in if statments against the returned valus of getch().

this is the code in the windows version of the program am trying to recreate.

    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;

Is there a function i can use on 开发者_Go百科the result of getch() so i can get the actual key pressed, something like the .AsciiChar seen above ?


MAJOR EDIT Get rid of previous examples, try this large one.

The return value from getch() is either an ASCII character, or the curses name for some special key.

Here is a program that might make the point clear:

#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();
}

References:

  • http://linux.die.net/man/3/getch
  • http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
  • http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/keys.html
  • http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/keys.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜