Skip to content

Commit 7c746e1

Browse files
committed
Ignore keypad key presses when Mouse Keys is enabled.
1 parent f5e4f95 commit 7c746e1

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/keyboard.cpp

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,43 @@ bool slop::Keyboard::anyKeyDown() {
2222
return keyDown;
2323
}
2424

25+
void slop::Keyboard::zeroKey( char keys[32], KeySym key ) {
26+
KeyCode keycode = XKeysymToKeycode( x11->display, key );
27+
keys[ keycode / 8 ] = keys[ keycode / 8 ] & ~( 1 << ( keycode % 8 ) );
28+
}
29+
2530
void slop::Keyboard::update() {
2631
char keys[32];
2732
XQueryKeymap( x11->display, keys );
2833
// We first delete the arrow key buttons from the mapping.
2934
// This allows the user to press the arrow keys without triggering anyKeyDown
30-
KeyCode keycode = XKeysymToKeycode( x11->display, XK_Left );
31-
keys[ keycode / 8 ] = keys[ keycode / 8 ] & ~( 1 << ( keycode % 8 ) );
32-
keycode = XKeysymToKeycode( x11->display, XK_Right );
33-
keys[ keycode / 8 ] = keys[ keycode / 8 ] & ~( 1 << ( keycode % 8 ) );
34-
keycode = XKeysymToKeycode( x11->display, XK_Up );
35-
keys[ keycode / 8 ] = keys[ keycode / 8 ] & ~( 1 << ( keycode % 8 ) );
36-
keycode = XKeysymToKeycode( x11->display, XK_Down );
37-
keys[ keycode / 8 ] = keys[ keycode / 8 ] & ~( 1 << ( keycode % 8 ) );
35+
zeroKey(keys, XK_Left);
36+
zeroKey(keys, XK_Right);
37+
zeroKey(keys, XK_Up);
38+
zeroKey(keys, XK_Down);
3839
// Also deleting Space for move operation
39-
keycode = XKeysymToKeycode( x11->display, XK_space );
40-
keys[ keycode / 8 ] = keys[ keycode / 8 ] & ~( 1 << ( keycode % 8 ) );
40+
zeroKey(keys, XK_space);
41+
42+
if ( mouseKeys ) {
43+
zeroKey(keys, XK_KP_0);
44+
zeroKey(keys, XK_KP_1);
45+
zeroKey(keys, XK_KP_2);
46+
zeroKey(keys, XK_KP_3);
47+
zeroKey(keys, XK_KP_4);
48+
zeroKey(keys, XK_KP_5);
49+
zeroKey(keys, XK_KP_6);
50+
zeroKey(keys, XK_KP_7);
51+
zeroKey(keys, XK_KP_8);
52+
zeroKey(keys, XK_KP_9);
53+
zeroKey(keys, XK_KP_Add);
54+
zeroKey(keys, XK_KP_Subtract);
55+
zeroKey(keys, XK_KP_Multiply);
56+
zeroKey(keys, XK_KP_Divide);
57+
zeroKey(keys, XK_KP_Insert);
58+
zeroKey(keys, XK_KP_Delete);
59+
zeroKey(keys, XK_KP_Decimal);
60+
}
61+
4162
keyDown = false;
4263
for ( int i=0;i<32;i++ ) {
4364
if ( deltaState[i] == keys[i] ) {
@@ -69,6 +90,18 @@ slop::Keyboard::Keyboard( X11* x11 ) {
6990
if ( err != GrabSuccess ) {
7091
//throw std::runtime_error( "Couldn't grab the keyboard after 10 tries." );
7192
}
93+
94+
// Ignore any failures while checking for Mouse Keys
95+
XkbDescPtr xkb = XkbGetKeyboard( x11->display, XkbControlsMask, XkbUseCoreKbd );
96+
if ( xkb != 0 ) {
97+
Status status = XkbGetControls( x11->display, XkbAllControlsMask, xkb );
98+
if ( status == Success ) {
99+
mouseKeys = xkb->ctrls->enabled_ctrls & XkbMouseKeysMask;
100+
XkbFreeControls( xkb, XkbAllControlsMask, true );
101+
}
102+
XkbFreeKeyboard( xkb, XkbControlsMask, true );
103+
}
104+
72105
XQueryKeymap( x11->display, deltaState );
73106
keyDown = false;
74107
}

src/keyboard.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define XK_MISCELLANY
2525
#define XK_LATIN1 // for XK_space
2626
#include <X11/keysymdef.h>
27+
#include <X11/XKBlib.h>
2728

2829
#include "x.hpp"
2930

@@ -34,6 +35,8 @@ class Keyboard {
3435
char deltaState[32];
3536
X11* x11;
3637
bool keyDown;
38+
bool mouseKeys;
39+
void zeroKey( char keys[32], KeySym key );
3740
public:
3841
Keyboard( X11* x11 );
3942
~Keyboard();

0 commit comments

Comments
 (0)