键盘操作

一、GetAsyncKeyState

GetAsyncKeyState()可以异步获取按键输入,GetAsyncKeyState(某个键的虚拟键码) 可以得到这个键的状态,然后判断返回值最高位是不是1,如果是1表示该键处于按下状态。

异步输入函数GetAsyncKeyState(),其可以同时识别两个按键被按下的情况

例子:

复制代码
if (_kbhit()) // 当按键时
{
    if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A'))  // 按下A键或左方向键
    {
        player.x_left -= player.vx; // 向左移动
    }
    if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D')) // 按下D键或右方向键
    {
        player.x_left += player.vx; // 向右移动
    }
    if (GetAsyncKeyState(VK_UP) || GetAsyncKeyState('W'))    // 按下W键或上方向键
    {
        player.y_bottom -= player.vy; // 向上移动
    }
    if (GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState('S'))  // 按下S键或下方向键
    {
        player.y_bottom += player.vy; // 向下移动
    }
}
复制代码

以上

 

posted @   ImreW  阅读(93)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示