读书人

关于C++的一个获取i键盘按键的疑义

发布时间: 2013-11-29 13:49:33 作者: rapoo

关于C++的一个获取i键盘按键的疑问

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
void draw(int x,int y);
int main()
{
int x=640;
int y=480;
initgraph(640,480);
draw(x,y);
while(1)
{
switch(kbhit())
{
case 18432:x-=1;draw(x,y);break;
case 20480:x+=1;draw(x,y);break;
case 9064:y-=1;draw(x,y);break;
case 6512:y+=1;draw(x,y);break;
}
}
getch();
closegraph();
}
void draw(int x,int y)
{
cleardevice();
RECT r = {0, 0, x, y};
drawtext(_T("@_@"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
代码中的switch语句中的函数体我想做的事 如果键盘按下了上方向键 进行y—=1。 我应该怎么写?!
[解决办法]
//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语言

热点推荐