检查一个键是否与Qt关闭 [英] Check if a Key is Down with Qt
问题描述
我在玩一些图形,我用箭头键实现了简单的相机移动。我的第一种方法是重写 keyPressEvent
,这样做:
I am playing around with some graphics, and I have implemented simple camera movement with the arrow keys. My first approach was to override keyPressEvent
to do something like this:
switch(key)
{
case up: MoveCameraForward(step); break;
case left: MoveCameraLeft(step); break;
...
}
将。当我按住例如前进键时,相机向前移动步进单位,然后停止一段时间,然后继续移动。我猜这是事件是如何生成的,以避免在有点长按键情况下的多个事件。
This doesn't work as I wish it would. When I press and hold, for example, the forward key, the camera moves forward "step" units, then halts for a while and then continues moving. I am guessing that this is how the event is generated, in order to avoid multiple events in case of a little bit long keypress.
因此,我需要轮询键盘在我的 Paint()
例程。我还没有找到如何做到与Qt。我想到有一个地图< Key,bool>
将在 keyPressEvent
和 keyReleaseEvent
并在 Paint()
中轮询映射。任何更好的想法?感谢您的任何见解。
So, I need to poll the keyboard in my Paint()
routine. I haven't found how to do it with Qt. I thought of having a map<Key, bool>
which would be updated in keyPressEvent
and keyReleaseEvent
and poll that map in Paint()
. Any better ideas? Thanks for any insights.
推荐答案
常规。我没有找到
如何做到与Qt。我想到有一张地图,
将更新在keyPressEvent和keyReleaseEvent和投票在
Paint()。
So, I need to poll the keyboard in my Paint() routine. I haven't found how to do it with Qt. I thought of having a map which would be updated in keyPressEvent and keyReleaseEvent and poll that map in Paint().
你的第二个方法是我会做的,除了我会使用一个连续的,周期性的QTimer事件来轮询键盘按下的地图,并调用QWidget :: Update()函数,当有效的显示窗口部件无效。强烈建议在Paint()内部执行非绘画操作,但我不知道如何解释。
Your second method is what I would have done, except that I would use a continuous, periodic QTimer event to poll the keyboard-pressed map and call QWidget::Update() function when necessary to invalidate the display widget instead. Performing non-painting operations inside Paint() is strongly discouraged for many reasons but I do not know how to explain that well.
这篇关于检查一个键是否与Qt关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!