本文介绍了捕捉Qt修饰键释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Qt的新手,但是我正在尝试在Qt应用程序中实现基本上相当于视频游戏风格的输入循环(我知道这很疯狂,但是看看您能否提供帮助).我需要准确的,一对一的按键处理键释放事件,无论您多么奇怪地为键盘弹起,对于所有键(包括修饰符),都如此.

I'm a newcomer to Qt, but I'm trying to implement what basically amounts to a video-game-esque input loop in a Qt application (crazy, I know, but see if you can help). I need accurate, one-to-one event handling for key presses and key releases, for all keys, including modifiers, no matter how weirdly you chord-up the keyboard.

当然,您可以通过QKeyEvent对主要事件进行主要访问.但让我们说发生以下情况:

Of course, your main access to key events is through QKeyEvent. But let's say the following happens:

  • 用户按住
  • 用户按住
  • 用户同时释放和
  • user presses and holds
  • user presses and holds
  • user releases and simultaneously

据我所知,我从Qt得到的是:

As far as I can tell, what I get from Qt is:

  • QKeyEvent,用于单独按下(Qt::Key_Ctrl)
  • QKeyEvent,用于单独按下(Qt::Key_Up)
  • QKeyEvent,用于释放,其中key() == Qt::Key_Up和Ctrl位反映在修饰符更改中.
  • QKeyEvent for the pressing of , by itself (Qt::Key_Ctrl)
  • QKeyEvent for the pressing of , by itself (Qt::Key_Up)
  • QKeyEvent for the releasing of , with key() == Qt::Key_Up and the Ctrl bit reflected in a modifier change.

这可能不完全准确,但是对于问题的过多调试,这是我的最佳猜测.无论如何,涉及修饰符的关键 release 事件难以置信不可靠.

This may be not exactly accurate, but it's my best guess as to what's going on from way too much debugging of the issue. In any event, the key release events when modifiers are involved are incredibly unreliable.

最后的Ctrl + Up序列是问题所在.现在,我知道在e->modifiers()中获取修饰符状态,并且在e->key()中获取按键.我可以做一些复杂的技巧,试图在内部记住修改器的状态,以检测用户何时释放了修改器.但是随后,Qt文档就e->modifiers()告诉我:

The Ctrl+Up sequence there at the end is the problem. Now, I know I'm getting modifier state in e->modifiers(), and I'm getting the key press in e->key(). I could do some complicated hacks, trying to remember the modifier state internally, to detect when the user's released the modifier. But then, the Qt docs inform me, speaking of e->modifiers(), that:

这正是我要避免的情况.

This is exactly the case I'm trying to avoid.

在Qt中,对于正常键和修饰键,是否有任何可靠的方法来跟踪一对一的按键按下和释放?如果没有,您能得到的最接近的是什么?

Is there any reliable way to keep track of one-to-one key presses and releases, for both normal and modifier keys, in Qt? If not, what's the closest you can get?

编辑:我可以对此进行一些改进.看来,如果您在Mac上按住,按下几个键(例如字母键),松开它们,然后松开,您不要'不会获得字母键释放的释放事件.我将尝试隔离一个小示例,看看这是否真的是Qt错误.

I can refine this a little bit. It seems that if you hold down on a Mac, press a few keys (letter keys, say), release them, then release , you don't get release events for the letter key releases. I'm going to try to isolate a small example and see if this is actually a Qt bug.

推荐答案

我认为,如果您对键盘的要求非常明确,您将离开Qt并获得特定于操作系统的信息,或者您需要处理任何过滤发生之前的Qt事件.

I think if you are getting very specific with the keyboard, you are going to have leave Qt and get something that is OS specific, or you need to handle the Qt events before any filtering happens.

Qt中的加速器查找并等待Alt + __组合,您可以设置Ctrl + __组合以供QAction监听.

Accelerators in Qt look for and wait on Alt+__ combos and you can set up Ctrl+__ combos to be listened to by QAction.

QApplication和常规GUI环境中内置的这两种类型的对象,可能会中断您收到的消息,并且给您的收益少于预期.

Both of these types of Objects built into QApplication and the general GUI environment, might be interrupting the messages you are getting, and giving you less than what you are expecting.

Qt文档:事件系统 ...这部分内容链接到此...

Qt Documentation: The Event System ... this part has a link to this...

QCoreApplication :: notify() ...告诉最高层可以使用Qt API编写Qt应用程序以处理输入:

QCoreApplication::notify() ... which tells the highest level that a Qt Application can be written to handle input using the Qt API:

操作系统特定的键盘处理替代方式

如果从以上述级别安装的Qt事件过滤器查看调试语句所产生的结果与您在问题中提到的结果相同,那么您将需要转到特定于操作系统的键盘输入内容.例如,在Windows中,您需要扫描键盘或查看整个键盘的VK状态并对其进行某些操作(例如 GetKeyboardState() ).

OS Specific Keyboard Handling Alternative

If looking at debug statements from a Qt event filter installed at the level mentioned above yields the same results as what you mentioned in your question, then you will need to go to the OS specific keyboard input stuff. For example in Windows you would need to scan the keyboard and or look at the VK state of the whole keyboard and do something with it (with something like GetKeyboardState() ).

这篇关于捕捉Qt修饰键释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:27
查看更多