如何检测相同的键盘键只按一次

how to detect same keyboard key press only once

本文关键字:一次 键盘 何检测 检测      更新时间:2023-10-16

我正在设计一个键盘类,它只能检测一次键盘按键,但我仍然无法找到方法。我的目标只是在按住或按住相同的键时检查并仅执行一次操作,而同时按下 2 个操作键时不执行任何操作。例如,当我按住键 A 时,操作 1 只执行一次。然后我一直按住或按住另一个键B,动作2也执行一次。如果我同时按下 A 键和 B 键,我无法执行任何操作。

KeyboardClass header和cpp文件中有两个类,即KeyboardClientClass和KeyboardServerClass。

////////////////////////////////////////////////////////////////////////////////
// Filename: KeyboardClass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _KEYBOARDCLASS_H_
#define _KEYBOARDCLASS_H_
////////////////////////////////////////////////////////////////////////////////
// Class prototype
////////////////////////////////////////////////////////////////////////////////
class KeyboardServerClass;

////////////////////////////////////////////////////////////////////////////////
// Class name: KeyboardClientClass
////////////////////////////////////////////////////////////////////////////////
class KeyboardClientClass
{
public:
    KeyboardClientClass( const KeyboardServerClass& KeyboardServer );
    ~KeyboardClientClass();
    bool KeyIsPressed( unsigned char keycode ) const;
    bool KeyIsPressedOnce( unsigned char keycode );
private:
    unsigned char tempKeyCode;
    const KeyboardServerClass& server;
};
class KeyboardServerClass
{
    friend KeyboardClientClass;
public:
    KeyboardServerClass();
    ~KeyboardServerClass();
    void OnKeyPressed( unsigned char keycode );
    void OnKeyReleased( unsigned char keycode );
   private:
        static const int nKeys = 256;
        bool keystates[ nKeys ];
        bool isKeyDown;
    };
    #endif

////////////////////////////////////////////////////////////////////////////////
// Filename: KeyboardClass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "KeyboardClass.h"
KeyboardClientClass::KeyboardClientClass( const KeyboardServerClass& KeyboardServer ) 
: server ( KeyboardServer )
{
    tempKeyCode = 257;
}

KeyboardClientClass::~KeyboardClientClass()
{}

bool KeyboardClientClass::KeyIsPressed( unsigned char keycode ) const
{
    return server.keystates[ keycode ];
}

bool KeyboardClientClass::KeyIsPressedOnce( unsigned char keycode )
{
    if ( tempKeyCode != keycode )
    {
        tempKeyCode = keycode;
        return server.keystates[ keycode ];
    }
    else
    {
        return false;
    }
}

KeyboardServerClass::KeyboardServerClass()
{
    for ( int x = 0; x < nKeys; x++ )
    {
        keystates[ x ] = false;
    }
}

KeyboardServerClass::~KeyboardServerClass()
{
    isKeyDown = true;
}

void KeyboardServerClass::OnKeyPressed( unsigned char keycode )
{
    keystates [ keycode ] = true;
    isKeyDown = false;
}

void KeyboardServerClass::OnKeyReleased( unsigned char keycode )
{
    keystates [ keycode ] = false;
    isKeyDown = true;
}

首先,我创建一个键盘服务器对象来跟踪来自 Windows 过程的键盘消息。

LRESULT CALLBACK SystemClass::MessageHandler( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch ( msg )
    {
        //************ KEYBOARD MESSAGES ************ //
        // Check if a key has been pressed on the keyboard
        case WM_KEYDOWN:
        {
            if ( wParam == VK_ESCAPE )
                PostQuitMessage( 0 );
            // If a key is pressed send it to the KeyboardServer object so it can record the state
            m_KeyboardServer.OnKeyPressed( wParam );
            break;
        }
        // Check if a key has been released on the keyboard
        case WM_KEYUP:
        {
            // If a key is released then send it to the KeyboardServer object so it can unset the state for that key
            m_KeyboardServer.OnKeyReleased( wParam );
            break;
        }
        // ************ END KEYBOARD MESSAGES ************ //
}

然后,我在游戏类中创建一个键盘客户端对象来检查是否已按下某个键,并根据按下的键执行操作。

if ( m_Keyboard.KeyIsPressed( KEY_B ) )
    // Do action A
else if ( m_Keyboard.KeyIsPressed( KEY_N ) )
    // Do action B

WM_KEYDOWN 消息的lParam值的第 30 位指示生成消息时密钥之前是否关闭。您可以使用它来区分实际按键和任何后续按键重复。

case WM_KEYDOWN:
    if (lParam & (1 << 30))
    {
        // this is a repeat
    }
    else
    {
        // first press
    }
    break;

但是,如果您尝试实时检查哪些键已关闭,则可以使用 GetAsyncKeyState() 函数执行此操作,而不是依赖于通过消息循环跟踪密钥状态。