短按和长按处理

时间:2014-11-17 17:03:02

标签: c# c++ windows keyboard-events

如果我在短时间内或长时间按下键盘,我想做两个不同的命令。如果我按住某个键,Windows会向我发送多个keyDown和KeyUp事件。

现在。我这样做是为了处理" Long press"

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)可以解决我的问题。

2 个答案:

答案 0 :(得分:2)

您可以尝试以下内容。此示例只是挂钩表单的KeyDown和KeyUp事件,因此您需要对其进行修改以满足您的需求。

//consider keys held less than one second a short keypress event
const double longThresholdMs = 1000.0;
Dictionary<Keys, DateTime> keyDownTimes = new Dictionary<Keys, DateTime>();

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (!keyDownTimes.ContainsKey(e.KeyCode))
    {
        keyDownTimes[e.KeyCode] = DateTime.Now;
    }
}

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (keyDownTimes.ContainsKey(e.KeyCode))
    {
        if (DateTime.Now.Subtract(keyDownTimes[e.KeyCode]).TotalMilliseconds > longThresholdMs)
        {
            Console.Out.WriteLine(e.KeyCode + " long press");
        }
        else
        {
            Console.Out.WriteLine(e.KeyCode + " short press");
        }

        keyDownTimes.Remove(e.KeyCode);
    }
}

答案 1 :(得分:0)

我找到了答案。

C ++:

if (pMsg->message == WM_KEYUP || pMsg->message == WM_KEYDOWN)
  {
    return keyboardManager->Execute( (KeyboardCommand)pMsg->wParam, BOOL (HIWORD(pMsg->lParam) & KF_REPEAT) == 0,  (HIWORD(pMsg->lParam) & KF_UP) == KF_UP, pMsg->message == WM_KEYDOWN  ) 
  }

C#:

public Boolean Execute( KeyboardCommand _command, Boolean _first, Boolean _last, Boolean _keyDown )
    {
      if (_keyDown)
          {
            switch (_command)
            {
              case (KeyboardCommand.otherCommand):
                handled = ExecuteCommand();
                break;
            }
          }
      switch (_command)//Short press and long press events
          {
            case (KeyboardCommand.mycommand):
                if (_first)
                {
                  m_TimeKeyDown = DateTime.Now;
                  m_LongCommandExecuted = false;
                }
                else
                {
                  TimeSpan timeLapse = DateTime.Now - m_TimeKeyDown;
                  if (!m_LongCommandExecuted && timeLapse.TotalMilliseconds > 500)//long press
                  {
                    m_LongCommandExecuted = true;
                    handled = myLongcommand();
                  }

                  if (!m_LongCommandExecuted && _last)//short press
                  {
                    m_LongCommandExecuted = true;
                    handled = myShortcommand();
                  }
                }
                break;
          } 

   //some other jazz         

}