当前位置:首页 > 科技新闻 > 微信平台 > 正文

提供个小程序,得到按键值的,做小游戏啥的用
2021-08-05 12:38:03

#include <conio.h>
//#include <stdio.h>
#include<iostream>
using namespace std;
short int getkey()//得到按键值
{
    union key
    {
        unsigned short int value;
        unsigned char  ch[2];
    } key1;
    key1.value =0;
    while(kbhit())    //清空缓冲区
        getch();
    while(!kbhit());   //等待按键
    if (kbhit())
    {
        key1.ch[0]=getch();
        if (kbhit())
            key1.ch[1]=getch();
    }
    return key1.value;
}
int main()
{
	while(1)
	{
		short int key;
		key=getkey();
		cout<<key<<endl;
	}
	return 0;
}

本文摘自 :https://blog.51cto.com/u