getch()问题

来源:百度知道 编辑:UC知道 时间:2024/05/31 06:16:26
#include <stdio.h>
#include <conio.h>
void main()
{
char key;
key=getch();
if (key=='\024') //我查的ASCII码向上的键是24,可是不行,是不是我哪写错了.
printf("1111111111");
else
printf("2222222222");
}

要怎样修正才能得到正切的判断输出效果呢?

楼主你肯定没有注意看帮助文档。
getch functions read a single character from the console without echoing the character. Cant'b be used to read CTRL+C.When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.

如果看不懂,可以给你翻译一下。

getch函数从控制台读取未编码的单个字符。不能读取到ctrl+c组合键。在读取一个功能键或者箭头(方向)键盘时,函数会返回两次,第一次调用返回0或者0xE0,第二次调用返回实际的键值。

正确代码如下

void main()
{
unsigned char key;
key = getch();
if( key == 0 || key == 0xE0 )
{
key = getch();
if( key == 0x48 ) //向上键
{
printf( "111111" );
}
}

}

//另外,键盘向上的值,并不是24。

用错了函数,把 key=getch()改为
key=getchar();
getch()是指输入任意键然后程序继续往下进行;
getchar()是接受输入字符并储存。

学习可不能毛躁。

把getch();改成gets();

直接等于24