将 getch() 响应转换为 ascii 字符

发布于 2024-12-05 14:29:09 字数 854 浏览 1 评论 0原文

我在 Linux 上使用 ncurses。我使用 getch() 从输入流获取下一个按下的键,但它返回一个数字而不是字母。

在对谷歌进行研究后,我发现 getch() 不是标准的,所以我不知道该怎么做。

我需要键 0-9、tab、ctrl、p、v、m、l、a、b、c、d、e、f 和箭头键以及 0xff、0x4F00、0x4700、0x4800、0x5000、0x4D00 :, 0x4B00、0x4900、0x5100。这些是针对 getch() 返回值的 if 语句中使用的内容。

这是我尝试重新创建的程序的 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 ?

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 the result of getch() so i can get the actual key pressed, something like the .AsciiChar seen above ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戴着白色围巾的女孩 2024-12-12 14:29:09

主要编辑摆脱以前的例子,尝试这个大的例子。

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

参考

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文