读书人

关于#x27;↓#x27;键跟大P冲突

发布时间: 2013-09-29 11:07:08 作者: rapoo

关于'↓'键和大P冲突

//关于'↓'键和大P冲突

//大家有兴趣试试这段代码,当你按上下左右键的时候 程序的反应。
//↑ ↓ ← →

//pause.cpp

#include
#include
using namespace std;

int main()
{
char ch = ' ';
while (1)
{
ch = getch();
if (ch == 'P')
{
cout<<'大P冲突!!!!!'<<endl<<endl;
}
else
{
cout<<'-----换行-----'<<endl;
}
}
return 0;
}


求关于按键冲突现象解释一下原因啊。我自己单步调试了一翻,但是看不懂。 c ↓ 按键 冲突 ASCII
[解决办法]
int ch;
才对,char ch;
真有可能冲突。


[解决办法]
MSDN中的原话:
When reading a function key or an arrow key, _getch and _getche must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.
当读取功能键或者方向键时,_getch 和 _getche,必须调用两次;第一次,返回0或0xe0,第二次,返回真实的键值。
[解决办法]
//The _getch function reads a single character from the console without echoing.
//Function can not be used to read CTRL+Break.
//When reading a function key or an arrow key,
//_getch must be called twice; the first call returns 0 or 0xE0,
//and the second call returns the actual key code.
#include <conio.h>
#include <windows.h>
void main() {
unsigned short k;

while (1) {
Sleep(100);
k=getch();
if (27==k) break;//按Esc键退出
if (0==k
[解决办法]
0xe0==k) k
[解决办法]
=getch()<<8;//非字符键
cprintf("%04x pressed.\r\n",k);
}
}

读书人网 >C语言

热点推荐