Win32键盘事件
Win32键盘事件
用枚举区分键盘状态:
1.Press,按下
2.Release,松开
3.Invaild,无效
记录按键的char值;
定义键盘事件Event;
使用bites表存所有key的状态,使用bites原因一共键位个数不超过256个,所以足够了;
使用两个队列Queue分别记录键盘事件和char值;
检测到VM_KEYDOWN的系统消息时,调用按下的回调事件,将keystate更改为true,push事件;
检测到VM_KEYUP和VM_CHAR抬起一样;
tips:在窗口不为焦点窗口时需要清空两个队列;
在WindowsProc函数下拦截消息:
/*****************键盘消息******************/
case WM_KILLFOCUS: //焦点窗口
kbd.ClearState();
break;
case WM_KEYDOWN: //这里由于alt,f系列键位不会被检测,所以添加syskeyDown
case WM_SYSKEYDOWN:
//按下后是否重复调用事件,autorepeat为false按下只调用一次
if (!(lParam & 0x40000000) || kbd.AutorepeatIsEnabled())
{
kbd.OnKeyPressed(static_cast<unsigned char>(wParam));
}
break;
case WM_KEYUP:
case WM_SYSKEYUP:
kbd.OnKeyReleased(static_cast<unsigned char>(wParam));
break;
case WM_CHAR: //获取输入字符,输入文字时用到
kbd.OnChar(static_cast<unsigned char>(wParam));
break;
/*****************键盘消息******************/
完整代码:
//.h
class Keyboard
{
friend class Window;
public:
class Event
{
public:
enum class Type
{
Press,
Release,
Invaild
};
private:
Type type;
unsigned char code;
public:
Event() noexcept
:
type( Type::Invaild),
code( 0u )
{}
Event(Type type, unsigned char code) noexcept
:
type( type ),
code( code )
{}
bool IsPress() const noexcept
{
return type == Type::Press;
}
bool IsRelease() const noexcept
{
return type == Type::Release;
}
bool IsInvaild() const noexcept
{
return type != Type::Invaild;
}
unsigned char GetCode() const noexcept
{
return code;
}
};
public:
Keyboard() = default;
Keyboard( const Keyboard& ) = delete;
Keyboard& operator=( const Keyboard& ) = delete;
// key event stuff
bool KeyIsPressed( unsigned char keycode ) const noexcept;
std::optional<Event> ReadKey() noexcept;
bool KeyIsEmpty() const noexcept;
void FlushKey() noexcept;
// char event stuff
std::optional<char> ReadChar() noexcept;
bool CharIsEmpty() const noexcept;
void FlushChar() noexcept;
void Flush() noexcept;
// autorepeat control
void EnableAutorepeat() noexcept;
void DisableAutorepeat() noexcept;
bool AutorepeatIsEnabled() const noexcept;
private:
void OnKeyPressed( unsigned char keycode ) noexcept;
void OnKeyReleased( unsigned char keycode ) noexcept;
void OnChar( char character ) noexcept;
void ClearState() noexcept;
//清空队列模板
template<typename T>
static void TrimBuffer( std::queue<T>& buffer ) noexcept;
private:
static constexpr unsigned int nKeys = 256u;
static constexpr unsigned int bufferSize = 16u;
bool autorepeatEnabled = false;
std::bitset<nKeys> keystates;
std::queue<Event> keybuffer;
std::queue<char> charbuffer;
};
//.cpp
bool Keyboard::KeyIsPressed( unsigned char keycode ) const noexcept
{
return keystates[keycode];
}
std::optional<Keyboard::Event> Keyboard::ReadKey() noexcept
{
if( keybuffer.size() > 0u )
{
Keyboard::Event e = keybuffer.front();
keybuffer.pop();
return e;
}
return Keyboard::Event();
}
bool Keyboard::KeyIsEmpty() const noexcept
{
return keybuffer.empty();
}
std::optional<char> Keyboard::ReadChar() noexcept
{
if( charbuffer.size() > 0u )
{
unsigned char charcode = charbuffer.front();
charbuffer.pop();
return charcode;
}
return 0;
}
bool Keyboard::CharIsEmpty() const noexcept
{
return charbuffer.empty();
}
void Keyboard::FlushKey() noexcept
{
keybuffer = std::queue<Event>();
}
void Keyboard::FlushChar() noexcept
{
charbuffer = std::queue<char>();
}
void Keyboard::Flush() noexcept
{
FlushKey();
FlushChar();
}
void Keyboard::EnableAutorepeat() noexcept
{
autorepeatEnabled = true;
}
void Keyboard::DisableAutorepeat() noexcept
{
autorepeatEnabled = false;
}
bool Keyboard::AutorepeatIsEnabled() const noexcept
{
return autorepeatEnabled;
}
void Keyboard::OnKeyPressed( unsigned char keycode ) noexcept
{
keystates[keycode] = true;
keybuffer.push( Keyboard::Event( Keyboard::Event::Type::Press,keycode ) );
TrimBuffer( keybuffer );
}
void Keyboard::OnKeyReleased( unsigned char keycode ) noexcept
{
keystates[keycode] = false;
keybuffer.push( Keyboard::Event( Keyboard::Event::Type::Release,keycode ) );
TrimBuffer( keybuffer );
}
void Keyboard::OnChar( char character ) noexcept
{
charbuffer.push( character );
TrimBuffer( charbuffer );
}
void Keyboard::ClearState() noexcept
{
keystates.reset();
}
template<typename T>
void Keyboard::TrimBuffer( std::queue<T>& buffer ) noexcept
{
while( buffer.size() > bufferSize )
{
buffer.pop();
}
}
Life is too short for so much sorrow.
本博客所有文章除特别声明外,均采用
CC BY-NC-SA 4.0 许可协议。转载请注明来自 小紫苏!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)