【问题标题】:Short Press and long press handling短按和长按处理
【发布时间】: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


    【解决方案1】:

    您可以尝试以下方法。此示例仅挂钩到表单的 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);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我找到了答案。

      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         
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多