如何检测相同的键盘键只按一次
how to detect same keyboard key press only once
我正在设计一个键盘类,它只能检测一次键盘按键,但我仍然无法找到方法。我的目标只是在按住或按住相同的键时检查并仅执行一次操作,而同时按下 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()
函数执行此操作,而不是依赖于通过消息循环跟踪密钥状态。
相关文章:
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 在C++中一次将矢量值写入多个文件
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么 zlib 放气初始化调用一次不起作用?
- 在一次迭代中从 txt 文件中读取多行
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 高级选择排序 - 在一次迭代中搜索两个元素
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 如何仅使用一次固定<<设置精度(2)?或者至少恢复到默认行为?
- 什么是仅调用一次并调用参数的控制台应用
- 如何确保用户在一行上提示输入一次时输入名字和姓氏?
- (C++)虽然循环一次不起作用,但我引入了多个变量
- 为什么我的信号处理程序只执行一次?
- 如何使用C++一次读取整个二进制文件
- 花括号使循环运行一次?
- 如何检测相同的键盘键只按一次
- C++ 从键盘读取一次一个字符,直到用户输入字母("A.."Z"或"a".."z')