Turbo C中的getch()返回什么?我用它来初始化程序的箭头键,getch()返回的值是77,80,72和75,这些是字母表的ASCII值,这清楚地表明它们不是ASCII值。如果它们不是ASCII值,那么它们是什么?
答案 0 :(得分:0)
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
箭头键的问题在于它们不是单字节字符。要处理箭头键,您必须处理多字节代码。您获得的数字只是密钥代码的两个字节之一。
有关阅读代码的示例,请参阅(How to read arrow keys) Cheat code in C programming (when inputted from the keyboard)
答案 1 :(得分:0)
getch()函数返回箭头键(和其他一些特殊键)的两个键码,首先返回0(0x00)或224(0xE0),然后返回一个代码,用于标识按下的键。
对于箭头键,它首先返回224,然后是72(向上),80(向下),75(向左)和77(向右)。如果按下数字键盘箭头键(关闭NumLock),则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()没有任何标准化,这些代码可能因编译器而异。