1个键盘输入处理的例子:GetKeyState

<span style="font-family:Arial, Helvetica, sans-serif;">一个小例子,用于字符界面的键盘输入处理,在Windows7 ,dev-c++5.9.2下编译通过</span>
#include <Windows.h>
#include <iostream>
#pragma comment( lib, "user32.lib" )
using namespace std;
#define KEYDOWN(vk_code) ((GetKeyState(vk_code) & 0x8000)?1:0)

enum
{
    E_MENU_START,
    E_MENU_SETTING,
    E_MENU_EXIT,
};
enum
{
    E_GAME_MENU,
    E_GAME_START,
    E_GAME_EXIT,
};

int main(int argc, char* argv[])

{
	int iMenu=E_MENU_START;
	int iGame=E_GAME_MENU;
	while(true)
	{
		Sleep(100);
		system("cls");
		if(iGame==E_GAME_MENU)
		{
			if(KEYDOWN(VK_DOWN))
			{
				iMenu++;
				if(iMenu>E_MENU_EXIT)
					iMenu=E_MENU_START;
			}
			else if(KEYDOWN(VK_UP))
			{
				iMenu--;
				if(iMenu<E_MENU_START)
					iMenu=E_MENU_EXIT;
			}
			else if(KEYDOWN(VK_RETURN))
			{
				if(iMenu==E_MENU_EXIT)break;
					//iGame=E_GAME_MENU;
			}

			cout<<"##################################"<<endl;
			cout<<"##################################"<<endl;
			cout<<"##########     游戏     ##########"<<endl;
			if(iMenu==E_MENU_START)
			{
				cout<<"########## ->游戏开始   ##########"<<endl;
				cout<<"##########   游戏设置   ##########"<<endl;
				cout<<"##########   退出游戏   ##########"<<endl;
			}
			else if(iMenu==E_MENU_SETTING)
			{
				cout<<"##########   游戏开始   ##########"<<endl;
				cout<<"########## ->游戏设置   ##########"<<endl;
				cout<<"##########   退出游戏   ##########"<<endl;
			}
			else if(iMenu==E_MENU_EXIT)
			{
				cout<<"##########   游戏开始   ##########"<<endl;
				cout<<"##########   游戏设置   ##########"<<endl;
				cout<<"########## ->退出游戏   ##########"<<endl;
			}
			cout<<"##################################"<<endl;
			cout<<"##################################"<<endl;
			cout<<"##################################"<<endl;

		}

	}

	system("pause");

	return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值