pynput icon indicating copy to clipboard operation
pynput copied to clipboard

Numpad KeyCode detection not working since updating to 1.7.1

Open emerysteele opened this issue 4 years ago • 6 comments

Since updating to 1.7.1, my script no longer detects key combinations with numpad keys. :(

Seems to only affect keys that are detected using the vk KeyCode parameter.

Here is some sample code

import threading
from pynput import keyboard
import time

mode = "Debug"

#Debug Code
def debug(text):
	if mode == "Debug":
		print(text)

debug("---------------------")

#Keyboard Shortcut Combinations
COMBINATIONS = [
	{keyboard.Key.alt_l, keyboard.KeyCode(vk=102)}, #NumPad 6
	{keyboard.Key.alt_l, keyboard.KeyCode(char='6')}, #NumRow 6
]

# The currently active modifiers
current = set()

def execute():
	if keyboard.KeyCode(vk=102) in current and keyboard.Key.alt_l in current:
		debug("Pressed AltL + NumPad6") #Never gets triggered
	if keyboard.KeyCode(char='6') in current and keyboard.Key.alt_l in current:
		debug("Pressed AltL + NumRow6") #Gets triggered normally

def on_press(key):
	if any([key in COMBO for COMBO in COMBINATIONS]):
		current.add(key)
		debug(current)
		if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
			execute()

def on_release(key):
	if any([key in COMBO for COMBO in COMBINATIONS]):
		try:
			current.remove(key)
		except KeyError:
			pass


def keyboardLoop():
	debug("Keyboard Loop Started")
	with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
		listener.join()

thread1 = threading.Thread(target=keyboardLoop)
thread1.start()

time.sleep(2)

emerysteele avatar Oct 10 '20 00:10 emerysteele

Verified this code works normally if I downgrade pynput to 1.6.8

emerysteele avatar Oct 10 '20 01:10 emerysteele

I have the same problem for the combinaison with numpad keys

gaetan1903 avatar Nov 30 '20 11:11 gaetan1903

I have the same problem with the key <255>, so it is confirmed for any keycode, works with 1.6.8

TheConfax avatar Dec 30 '20 19:12 TheConfax

Thank you for your reports.

On what platform is this? It appears that you are trying to implement hot keys; have you tested the global hot key helper included with this library?

moses-palmer avatar Dec 30 '20 21:12 moses-palmer

I'm on Windows 10, Python 3.9.0

I tried very briefly the hotkey helper, but my hotkey is unusual: ctrl left + ctrl right, and it didn't work (or maybe I gave up too soon) That other method worked under Pynput 1.6.8

TheConfax avatar Dec 30 '20 23:12 TheConfax

I want to note that the other "combination" method doesn't work for me for combinations that include chatacters (eg: including keyboard.KeyCode.from_char('k') in the dictionary) but only for strange combinations such as ctrl left + ctrl right, while hotkey helper works fine for usual hotkeys.

TheConfax avatar Dec 31 '20 00:12 TheConfax