1
0
Fork 0

Platform/Unix/X11Keyboard: only update state and signal key down if we have a focused window.

This commit is contained in:
Henrik Hautakoski 2020-12-20 16:29:12 +01:00
parent 6c96a3d9a1
commit 080c4f77c3
2 changed files with 21 additions and 3 deletions

View file

@ -1,6 +1,9 @@
#include <Spectre/Display/DisplayDescription.h> // FIXME: Weird name clashes with Xlib.h
#include <string.h> #include <string.h>
#include "X11Keyboard.h" #include "X11Keyboard.h"
#include "X11Display.h"
#include "X11SharedDisplay.h" #include "X11SharedDisplay.h"
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/keysym.h> #include <X11/keysym.h>
@ -202,7 +205,8 @@ static Keyboard::Key KeySymToKeyboardKey(KeySym key) {
} }
X11Keyboard::X11Keyboard() : 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])); 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; KeySym sym;
// Only signal that a button is down
// if we have focus on a window.
if (!m_win) {
return false;
}
switch(key) { switch(key) {
// Letters // Letters
case Keyboard::Key::A : sym = XK_a; break; case Keyboard::Key::A : sym = XK_a; break;
@ -350,8 +360,15 @@ bool X11Keyboard::handleMessage(XKeyEvent* xkeyevent, Event& event)
void X11Keyboard::update(InputModule *input) void X11Keyboard::update(InputModule *input)
{ {
X11Display *focus = X11Display::getFocused();
m_win = focus ? (::Window) focus->getHandle() : 0;
// If we have focus.
if (m_win) {
// Query keyboard state. // Query keyboard state.
::XQueryKeymap(m_disp, m_key_state); ::XQueryKeymap(m_disp, m_key_state);
} }
}
} // namespace sp } // namespace sp

View file

@ -27,6 +27,7 @@ protected :
private : private :
::Display* m_disp; ::Display* m_disp;
::Window m_win; // Focused window
// Cached keyboard state. // Cached keyboard state.
char m_key_state[32]; char m_key_state[32];