Fix functionality sometimes getting stuck if window loses focus

This commit is contained in:
Ryan Huffman 2018-08-15 13:32:58 -07:00
parent 83718ca06d
commit 5d44877f56
2 changed files with 15 additions and 7 deletions

View file

@ -3705,7 +3705,10 @@ static bool _altPressed{ false };
void Application::keyPressEvent(QKeyEvent* event) { void Application::keyPressEvent(QKeyEvent* event) {
_altPressed = event->key() == Qt::Key_Alt; _altPressed = event->key() == Qt::Key_Alt;
_keysPressed.insert(event->key());
if (!event->isAutoRepeat()) {
_keysPressed.insert(event->key(), *event);
}
_controllerScriptingInterface->emitKeyPressEvent(event); // send events to any registered scripts _controllerScriptingInterface->emitKeyPressEvent(event); // send events to any registered scripts
// if one of our scripts have asked to capture this event, then stop processing it // if one of our scripts have asked to capture this event, then stop processing it
@ -3916,7 +3919,9 @@ void Application::keyPressEvent(QKeyEvent* event) {
} }
void Application::keyReleaseEvent(QKeyEvent* event) { void Application::keyReleaseEvent(QKeyEvent* event) {
_keysPressed.remove(event->key()); if (!event->isAutoRepeat()) {
_keysPressed.remove(event->key());
}
#if defined(Q_OS_ANDROID) #if defined(Q_OS_ANDROID)
if (event->key() == Qt::Key_Back) { if (event->key() == Qt::Key_Back) {
@ -3952,11 +3957,14 @@ void Application::focusOutEvent(QFocusEvent* event) {
#endif #endif
// synthesize events for keys currently pressed, since we may not get their release events // synthesize events for keys currently pressed, since we may not get their release events
foreach (int key, _keysPressed) { // Because our key event handlers may manipulate _keysPressed, lets swap the keys pressed into a local copy,
QKeyEvent keyEvent(QEvent::KeyRelease, key, Qt::NoModifier); // clearing the existing list.
keyReleaseEvent(&keyEvent); QHash<int, QKeyEvent> keysPressed;
std::swap(keysPressed, _keysPressed);
for (auto& ev : keysPressed) {
QKeyEvent synthesizedEvent { QKeyEvent::KeyRelease, ev.key(), Qt::NoModifier, ev.text() };
keyReleaseEvent(&synthesizedEvent);
} }
_keysPressed.clear();
} }
void Application::maybeToggleMenuVisible(QMouseEvent* event) const { void Application::maybeToggleMenuVisible(QMouseEvent* event) const {

View file

@ -621,7 +621,7 @@ private:
float _mirrorYawOffset; float _mirrorYawOffset;
float _raiseMirror; float _raiseMirror;
QSet<int> _keysPressed; QHash<int, QKeyEvent> _keysPressed;
bool _enableProcessOctreeThread; bool _enableProcessOctreeThread;