Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

key 5 on the numeric pad does not print as 5 #530

Open
mglt opened this issue Jan 8, 2023 · 5 comments
Open

key 5 on the numeric pad does not print as 5 #530

mglt opened this issue Jan 8, 2023 · 5 comments

Comments

@mglt
Copy link

mglt commented Jan 8, 2023

Description
Pressing the key 5 on the num pad returns the following object:
{'vk': 65437, 'char': None, 'is_dead': False, 'combining': None, '_symbol': None}

I would have expected an object with its char set to '5'. I do not know if this is a bug or something I am not using correctly.

Other keys works fine for example typing 1 returns the following object:
{'vk': None, 'char': '1', 'is_dead': False, 'combining': None, '_symbol': None}

Platform and pynput version
Linux 5.4.0-136-generic #153-Ubuntu,
pynput Version: 1.7.6 as well as the one of the git

To Reproduce
Running keyboard-test.py results in:

$ python3 keyboard-test.py
Press esc to exit
=< Key.enter
=> <65437>5
=< <65437>
=> '1'
1=< '1'

@AwesomeCronk
Copy link

AwesomeCronk commented Jan 13, 2023

Can confirm I see this too. Numlock is enabled. I have debug script in one of my projects and noticed that it produces the output <65437> for numpad 5:

# This script just prints what each key is called

import sys, termios, time

from pynput import keyboard


run = True


def _onPress(key):
    print(key)

def _onRelease(key):
    global run
    if key == keyboard.Key.esc:
        run = False
        return False

# Modified version of `enable_echo` found at https://blog.hartwork.org/posts/disabling-terminal-echo-in-python/
def setEcho(value):
    iflag, oflag, cflag, lflag, ispeed, ospeed, cc = termios.tcgetattr(sys.stdin)

    if value: lflag |= termios.ECHO
    else: lflag &= ~termios.ECHO

    termios.tcsetattr(
        sys.stdin,
        termios.TCSANOW,
        [iflag, oflag, cflag, lflag, ispeed, ospeed, cc]
    )

def clearStdin():
    termios.tcflush(sys.stdin, termios.TCIFLUSH)
    # sys.stdin.flush()   # Doesn't seem to do anything


if __name__ == '__main__':
    print('Press keys to see what pynput shows them as. Hit Esc to exit.')
    try:
        setEcho(False)
        listener = keyboard.Listener(on_press=_onPress, on_release=_onRelease)
        listener.start()
        while run: time.sleep(0.1)
    finally:
        listener.stop()
        print('Listener stopped')
        clearStdin()
        setEcho(True)
        print('Echo reenabled')

I am on Ubuntu 20.04 x86_64, using pynput 1.7.6 with the xorg backend. All other numpad numbers work fine. If it's relevant my keyboard layout is standard US QWERTY.

@Edios
Copy link

Edios commented Jan 21, 2023

Same problem on my side (With Manjaro). Saw similar post on stack overflow with same behavior: LINK

@AwesomeCronk
Copy link

I wonder does anyone see this on Windows or is it a keycode/map issue specific to Linux?

@mglt
Copy link
Author

mglt commented Mar 13, 2023 via email

@moses-palmer
Copy link
Owner

Thank you for your report.

I can confirm that I observe this behaviour as well. It appears that display.keycode_to_keysym returns KP_Begin for numpad 5. There is code to correct these values when numlock is active, but KP_Begin was not present in the list of codes to translate. By applying the fix below I can get numpad 5 to work on my system.

diff --git a/lib/pynput/keyboard/_xorg.py b/lib/pynput/keyboard/_xorg.py
index cfbc457..bbab3dd 100644
--- a/lib/pynput/keyboard/_xorg.py
+++ b/lib/pynput/keyboard/_xorg.py
@@ -528,6 +527,7 @@ class Listener(ListenerMixin, _base.Listener):
         KEYPAD_KEYS['KP_8']: KeyCode.from_char('8'),
         KEYPAD_KEYS['KP_9']: KeyCode.from_char('9'),
         KEYPAD_KEYS['KP_Add']: KeyCode.from_char('+'),
+        KEYPAD_KEYS['KP_Begin']: None,
         KEYPAD_KEYS['KP_Decimal']: KeyCode.from_char(','),
         KEYPAD_KEYS['KP_Delete']: Key.delete,
         KEYPAD_KEYS['KP_Divide']: KeyCode.from_char('/'),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants