NCURSES printw获取关键codeS的正确输出RETURN和空间 [英] NCURSES printw Getting correct output of keycodes for RETURN and SPACE

查看:322
本文介绍了NCURSES printw获取关键codeS的正确输出RETURN和空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了code,我下面的工作与打印出正确的信息之外的按键恢复和空间的伟大工程。
我已经尝试了许多方法,但是这一次似乎最接近的一次。

Okay the code I'm working with below works great with the exception of printing out the correct information for the keys RETURN and SPACE. I have tried many approaches but this one seems the closest.

更新:按建议由托马斯·迪基,

UPDATE: as per recommend by Thomas Dickey,

#include <ncurses.h>

int main()
{
    initscr();
    cbreak(); /* as per recommend Thomas Dickey */
    noecho(); /* as per recommend Thomas Dickey */

    int c, SPACE=32, RETURN=10; /* i know this is wrong but..? */

/* with changes return key is 10 , yet name for keys SPACE && RETURN
    do not display, which is the gist of my question. 
    What am i missing? */

    printw("Write something (ESC to escape): ");
    move(2,0);
    while((c=getch())!=27)
    {
        //move(2,0);
        printw("Keycode: %d, and the character: %c\n", c, c);       
        refresh();
    }
    endwin();
    return 0;
}

这是终端输出:更新后

Write something (ESC to escape):

Keycode: 32, and the character: <--spacebar,- no label
Keycode: 10, and the character: <--enter/return key - no label

Keycode: 121, and the character: y
Keycode: 97, and the character: a
Keycode: 109, and the character: m
Keycode: 115, and the character: s

我山药的还是在亏损的xD ....

I yam still at a loss.... xD.

我下面本教程从YouTube用户thecplusplusguy虽然它实际上不是C ++。

I am following this tutorial from youtube user thecplusplusguy though it isn't actually C++.

需要明确的是,上面的输出是我想要的,与空格键丢失的标签外和返回键。

感谢您。

这是所有导致一个基于终端阅读器的古腾堡计划版本。我需要做一些新的东西。 Android是烦人。

This is all leading to a terminal based reader for project gutenberg releases. I needed to do something new. Android is annoying.

推荐答案

这行

while((c=getchar())!=27)

使用标准I / O输入功能 的getchar 。相应的诅咒的功能是 残培

is using the standard I/O input function getchar. The corresponding curses function is getch.

此外,通过ncurses手册页初始化 部分注意事项:

Additionally, the ncurses manual page Initialization section notes:

要获得字符在-A-时间输入而不回显(最
  互动,屏幕取向方案希望此),按照以下顺序,应使用

To get character-at-a-time input without echoing (most interactive, screen oriented programs want this), the following sequence should be used:

initscr的(); CBREAK(); NOECHO();

借助 printw 功能最终使用的 addch ,其中(见手册页):

The printw function ultimately uses addch, which (see manual page):

回车将光标移动到窗口左侧
  保证金在当前行。

Carriage return moves the cursor to the window left margin on the current line.

问题指的是返回为31,但没有说明原因。给出的例子似乎显示OP输入其他文字读取输入行之前。

The question refers to RETURN as "31", but does not explain why. The given example appears to show OP entering other text before the input line was read.

引用的教程缺少初始化单字符模式下运行。

The cited tutorial is missing the initialization to run in single-character mode.

OP的程序将不会从没有这个答案的建议的修复工作。

OP's program will not work without the suggested fixes from this answer.

OP有兴趣如何渲染<大骨节病>空格和<大骨节病>返回这样的东西会在示例程序显示出来。如果没有一些特殊处理,这些字符将没有任何明显的文本呈现:<大骨节病>空格将是一片空白。你可以看到更好的通过改变printw调用是这样的:

OP is interested in how to render space and return so that something will show up in the example program. Without some special treatment, those characters will be rendered without any noticeable text: space will be a blank. You can see that better by changing the printw call to something like this:

printw("Keycode: %d, and the character: \"%c\"\n", c, c);

的<大骨节病>收益是一个不同的问题。首先,它通常是从ASCII <大骨节病>控制翻译 <大骨节病> M ^ M ),以一个换行(实际上是一个ASCII 道^ J )。对于的的人物,诅咒功能 键名 (返回一个指向字符串)显示打印形式这些字符。所以,你可以使用这个调用来代替:

The return is a different problem. First, it is normally translated from an ASCII controlM (^M) to a "newline" (actually an ASCII ^J). For most characters, the curses function keyname (which returns a pointer to a string) shows these characters in printable form. So you could use this call instead:

printw("Keycode: %d, and the character: \"%s\"\n", c, keyname(c));

由此产生的程序会告诉你,<大骨节病>返回读作道^ J 。如果你希望它是 ^ M ,你会叫的 RAW() 而非 CBREAK()(但那么你可以不使用停止程序^ C )。

The resulting program would show you that return is read as ^J. If you want it to be ^M, you would call raw() rather than cbreak() (but then you could not stop the program using a ^C).

这篇关于NCURSES printw获取关键codeS的正确输出RETURN和空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆