【问题标题】:How to catch Ctrl+C key event with Qt when Ctrl is released before 'C'?在“C”之前释放 Ctrl 时,如何使用 Qt 捕获 Ctrl+C 键事件?
【发布时间】:2010-11-11 20:32:29
【问题描述】:

我想在用户释放Ctrl+C时调用一些自定义复制代码。当 CCtrl 之前释放时,Qt 发送一个与QKeySequence::Copy 匹配的键事件。当CtrlC之前释放时,释放事件不匹配。

当按键释放事件伴随Ctrl进来时,有没有办法查看C是否仍然被按住?

当我不处理 Ctrl 被首先释放时,该事件被传递并执行常规复制,这正是我不希望发生的事情。

bool
MyWidget::eventFilter(QObject* object, QEvent* event)
{
   // the text edit box filters its events through here
   if (object == m_text_edit_box)
   {
      if (event->type() == QEvent::KeyPress)
      {
         QKeyEvent *key_event = static_cast<QKeyEvent*>(event);

         if (key_event->matches(QKeySequence::Copy))
         {
            // don't do anything and don't pass along event
            return true;
         }
      }
      else if (event->type() == QEvent::KeyRelease)
      {
         QKeyEvent *key_event = static_cast<QKeyEvent*>(event);

         if (key_event->matches(QKeySequence::Copy))
         {
            // we only get in here if 'c' is released before ctrl
            callCustomCopy();
            return true;
         }
      }
   }

   // pass along event
   return false;
}

【问题讨论】:

    标签: c++ qt events keyboard copy-paste


    【解决方案1】:

    您可以专门查询字母“C”和元键 Ctrl,而不依赖于 key_even->matches()。您当然可以在 keydown 事件上放置 eventfilter 的对象中存储 keydown 序列是否匹配副本的事实。

    这(未经测试)可能对您有用,请注意静态变量应该是包含 this 的类的成员变量,这在本示例的上下文中似乎更清楚。您想要完成的确切逻辑可能需要在事件之间传递更多状态信息。

    bool MyWidget::eventFilter(QObject* object, QEvent* event)
    {
      // Remember state between events
      static foundCopy = false;
      // the text edit box filters its events through here
      if (object == m_text_edit_box)
      {
        if (event->type() == QEvent::KeyPress)
        {
        QKeyEvent *key_event = static_cast<QKeyEvent*>(event);
          if (key_event->matches(QKeySequence::Copy))
          {
            foundCopy = true;
            // don't do anything and don't pass along event
            return true;
          }
          else
          {
            foundCopy = false;
            // This is another sequence, ignore and pass event
            // Note that this will trigger with ctrl+c+a and others
    
          }
        }
        else if (event->type() == QEvent::KeyRelease)
        {
          QKeyEvent *key_event = static_cast<QKeyEvent*>(event);
          if (foundCopy)
          {
            callCustomCopy();
            foundCopy = false;
            return true;
          }
          // This should keep the system copy from triggering
          if (key_event->matches(QKeySequence::Copy))
          {
            return true;
          }
        }
      }
    
       // pass along event
     return false;
    }
    

    另一种方法是收集当前按下的所有键的实际状态,然后在释放一个键时查看哪些键仍然被按下。

    从 UI 的角度来看,请记住,所有键盘操作都是在按下时执行的(例如键入、Windows 粘贴),通常在释放时执行操作可能会使用户感到困惑,尤其是当行动。我无法从您的示例中看出您要完成什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-11
      • 2018-10-13
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 2013-11-04
      相关资源
      最近更新 更多