【发布时间】:2014-11-17 17:03:01
【问题描述】:
如果我短按或长按键盘,我想执行 2 个不同的命令。如果我按住一个键,Windows 会向我发送多个 keyDown 和 KeyUp 事件。
现在。我这样做是为了处理“长按”
c++:
if (pMsg->message == WM_KEYDOWN)
{
return keyboardManager->Execute( (KeyboardCommand)pMsg->wParam, BOOL (HIWORD(pMsg->lParam) & KF_REPEAT) == 0 ) )
}
注意:pMsg 是一个 MSG 结构 (winuser.h),KeyboardCommand 是一个带有虚拟键代码值的枚举
c#:
public Boolean Execute( KeyboardCommand _command, Boolean _first )
{
switch(_command)
{
case (KeyboardCommand.myCommand):
TimeSpan timeLapse = DateTime.Now - m_TimeKeyDown;
if (_first)
{
m_TimeKeyDown = DateTime.Now;
m_LongCommandExecuted = false;
}
else if (!m_LongCommandExecuted && timeLapse.TotalMilliseconds > 500)
{
m_LongCommandExecuted = true;
handled = ExecuteAction();
}
break;
case (KeyboardCommand.otherCommand):
break;
}
return handled;
}
你知道如何处理“短按”吗?知道 KeyUp 是否是最后一个 keyUp(真正的 keyUp)可以解决我的问题。
【问题讨论】:
标签: c# c++ windows keyboard-events