C语言如何捕获按下方向键

版权声明:转载请注明出处! https://blog.csdn.net/ZouHuiDong/article/details/89811382

最近做一个小游戏,用方向键来操控物体,于是分享一下C语言中如何捕获方向键的按下。
因为方向键上下左右对应的ASCII码是72,80,75,77,所以我们可以以此判断按下的是不是方向键。

void move()//定义一个判断按下按键的函数
{
	char chInput;//定义一个char型变量存储按下按键的值
	if(kbhit())//如果有按键按下
	{
		chInput = getch();//获取按下的按键
		if(chInput == 72) 
			printf("按下了上键");
		if(chInput == 80) 
			printf("按下了下键");		
		if(chInput == 75) 
			printf("按下了左键");		
		if(chInput == 77) 
			printf("按下了右键");	
	}
}

猜你喜欢

转载自blog.csdn.net/ZouHuiDong/article/details/89811382
今日推荐