From 080c4f77c3a15883a5e6ef705e55a7c1588eb7bd Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 20 Dec 2020 16:29:12 +0100 Subject: [PATCH] Platform/Unix/X11Keyboard: only update state and signal key down if we have a focused window. --- source/Platform/Unix/X11Keyboard.cpp | 23 ++++++++++++++++++++--- source/Platform/Unix/X11Keyboard.h | 1 + 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/source/Platform/Unix/X11Keyboard.cpp b/source/Platform/Unix/X11Keyboard.cpp index 5a7b059..5843f94 100644 --- a/source/Platform/Unix/X11Keyboard.cpp +++ b/source/Platform/Unix/X11Keyboard.cpp @@ -1,6 +1,9 @@ +#include // FIXME: Weird name clashes with Xlib.h + #include #include "X11Keyboard.h" +#include "X11Display.h" #include "X11SharedDisplay.h" #include #include @@ -202,7 +205,8 @@ static Keyboard::Key KeySymToKeyboardKey(KeySym key) { } X11Keyboard::X11Keyboard() : -m_disp(NULL) +m_disp(NULL), +m_win(0) { memset(m_key_state, 0, sizeof(m_key_state) / sizeof(m_key_state[0])); } @@ -223,6 +227,12 @@ bool X11Keyboard::isKeyDown(Keyboard::Key key) { KeySym sym; + // Only signal that a button is down + // if we have focus on a window. + if (!m_win) { + return false; + } + switch(key) { // Letters case Keyboard::Key::A : sym = XK_a; break; @@ -350,8 +360,15 @@ bool X11Keyboard::handleMessage(XKeyEvent* xkeyevent, Event& event) void X11Keyboard::update(InputModule *input) { - // Query keyboard state. - ::XQueryKeymap(m_disp, m_key_state); + X11Display *focus = X11Display::getFocused(); + m_win = focus ? (::Window) focus->getHandle() : 0; + + // If we have focus. + if (m_win) { + + // Query keyboard state. + ::XQueryKeymap(m_disp, m_key_state); + } } } // namespace sp diff --git a/source/Platform/Unix/X11Keyboard.h b/source/Platform/Unix/X11Keyboard.h index 4684bba..3140160 100644 --- a/source/Platform/Unix/X11Keyboard.h +++ b/source/Platform/Unix/X11Keyboard.h @@ -27,6 +27,7 @@ protected : private : ::Display* m_disp; + ::Window m_win; // Focused window // Cached keyboard state. char m_key_state[32];