turbo c的getch()返回什么?我用它来初始化程序的箭头键,GetCh()返回的值是77,80,72和75,其是字母表的ASCII值,其清楚地表明它们不是ASCII值。如果它们不是ASCII值,那么它们是什么?

有帮助吗?

解决方案

非标准getch()中提供的conio.h返回一个int:

#include <conio.h>

int     getch(void);
.

从参考:

A single character from the predefined standard input handle is read and returned. 
The input is not buffered. If there is a character pending from ungetch 
(see section ungetch), it is returned instead. The character is not echoed to the 
screen. This function doesn't check for special characters like Ctrl-C.

If the standard input handle is connected to the console, any pending output in the 
stdout and stderr streams is flushed before reading the input, if these streams are 
connected to the console.

Return Value

    The character.

Portability

    ANSI/ISO C  No
    POSIX       No 
.

箭头键的问题是它们不是单字节字符。为了处理箭头键,您必须处理 multi-byte 代码。您获得的数字只是关键代码的两个字节之一。

是读取代码的示例,请参阅 (如何读取箭头键)C程序中的作弊码(从键盘输入时)

其他提示

getch()函数返回箭头键(以及一些其他特殊键)的两个keycodes,它首先返回0(0x00)或224(0xe0),然后返回标识按下的密钥的代码。

对于箭头键,它首先返回224,然后返回72(向上),80(向下),75(左)和77(右)。如果按下NUM-PAD箭头键(带NUMLOCK OFF),则Getch()首先返回0而不是224。

所以,你可以做一些这样的事情:

char ch = getch ();
if (ch == 0 || ch == 224)
{
    switch (getch ())
    {
    case 72:
        /* Code for up arrow handling */
        break;

    case 80:
        /* Code for down arrow handling */
        break;

    /* ... etc ... */
    }
}
.

请注意,getch()不以任何方式标准化,这些代码可能因编译器而异。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top