Skip to first unread message

Sergey Vdovin

unread,
Mar 16, 2019, 6:49:46 AM3/16/19
to Kivy users support
Hello.
I'm trying to bind hotkeys:

def keyboard_on_key_down(self, window, keycode, text, modifiers):

if keycode[1] == "1" or keycode[1] == "numpad1":
print("\n", modifiers)
if modifiers == []:
print("Topic", "1")

elif modifiers == ["shift"]:
print("Topic", "11")

else:
print(keycode, modifiers)
The goal is to print "1" if single digit is pressed and to print "11" if shift+digit is pressed regardless location of the key (ordinary numbers or numpad).

When numlock is off, everything works as expected (prints "11"). When numlock is on, the output is "1" as if 'shift' isn't pressed...

For example, numlock is off.
1. Press simple "1", output:
 []
Topic 1
(49, '1') []
2. Press shift+1, output:
 ['shift']
Topic 11
(49, '1') ['shift']
3. Press numpad1:
 []
Topic 1
(257, 'numpad1') []
4. Press shift+numpad1:
 ['shift']
Topic 11
(257, 'numpad1') ['shift']

Now, numlock is on:
1. Press numpad1:
 []
Topic 1
(257, 'numpad1') []
2. Press shift+numpad1:
 []
Topic 1
(257, 'numpad1') []
(304, 'shift') ['shift']

How to solve this issue?
Thank you in advance

ZenCODE

unread,
Mar 16, 2019, 3:35:31 PM3/16/19
to Kivy users support
You can track the state of the numlock key by monitoring when it is pressed down and when it is released (using the "on_key_up" method). When 1 is pressed, your app can then respond appropriately as it will know the state of the numpad key?

Sergey Vdovin

unread,
Mar 17, 2019, 10:21:56 AM3/17/19
to Kivy users support
Numlock can be on even before the app is started so on_key_up doesn't work in all cases. There's an easier way to know numlock state, I've found it on stackoverflow (see below), but it's not the comprehensive solution. I'll explain.

from win32api import GetKeyState
from win32con import VK_NUMLOCK

def keyboard_on_key_down(self, window, keycode, text, modifiers):    
    if keycode[1] == "1":

print("\n", modifiers)
if modifiers == []:
print("Topic", "1")
elif modifiers == ["shift"]:
print("Topic", "11")
    if keycode[1] == "numpad1" and GetKeyState(VK_NUMLOCK) == 0:

print("\n", modifiers)
if modifiers == []:
print("Topic", "1")
elif modifiers == ["shift"]:
print("Topic", "11")

With this code, the app prints "1" and "11" as I expect but if numlock is on the app does nothing. I want the app to print proper outputs regardless state of numlock. The problem is that Kivy doesn't see modifier 'shift' with pressed numpad digit if numlock is on whereas the code of a numpad button remains the same ('numpad1' is for both numlock ON and OFF).

Examples again 
(keycode[0], keycode[1]) [modifier]

1. Numlock is OFF:

Pressed just numpad1:
(257, 'numpad1') []

Pressed shift+numpad1:
(257, 'numpad1') ['shift']

2. Numlock is ON:

Pressed just numpad1:
(257, 'numpad1') []

Pressed shift+numpad1:
(257, 'numpad1') []
(304, 'shift') ['shift']


суббота, 16 марта 2019 г., 22:35:31 UTC+3 пользователь ZenCODE написал:

Sergey Vdovin

unread,
Mar 17, 2019, 10:42:03 AM3/17/19
to Kivy users support
By the way, numpad5 changes its keycode when numlock changes.
numlock OFF - (1073741980, '')
numlock ON - (261, 'numpad5')

It looks like Kivy's bug that other numpad digits have the same codes for both numlock states

воскресенье, 17 марта 2019 г., 17:21:56 UTC+3 пользователь Sergey Vdovin написал:

ZenCODE

unread,
Mar 19, 2019, 10:13:41 AM3/19/19
to Kivy users support
Yes, using the Win32api gives you much more control if you don't need cross platform...

And thanks for posting the feedback...
Reply all
Reply to author
Forward
0 new messages