keyboard icon indicating copy to clipboard operation
keyboard copied to clipboard

Maybe you know why your library stops listening to keys after building with Pyinstaller?

Open kivdev opened this issue 6 years ago • 2 comments

import keyboard keyboard.add_hotkey("ctrl+shift+a", callback=lambda:print(1)) input()

While it is .py, it works fine, but after I build it in .exe, it does not respond to key presses.

kivdev avatar Feb 27 '19 14:02 kivdev

I have the same problem:

I wrote an application in Python 2.7-3.8 on Windows 10 using the (keyboard) module. It works perfectly when I run it on Python and captures all keystrokes. However, when I convert it to an exe using Nuitka, sometimes it captures all keystrokes and sometime it does not work.

When it works, it works perfectly from the beginning (the exe is launched) until the end (until the exe is closed). When it does not work , it does not capture anything no matter what or how many keys I press since the exe is launched. In this case I have found a workaround:

		keyboard.hook(self.__key_on)
		keyboard.start_recording()
		win32api.keybd_event(160, 0, win32con.KEYEVENTF_EXTENDEDKEY | win32con.KEYEVENTF_KEYUP, 0)
		ev_list = keyboard.stop_recording()
		if not ev_list:
			print("Couldn't set keyboard hooks. Trying once again...\n")
			time.sleep(2)
			dir_path = os.path.dirname(os.path.realpath(__file__))
			os.system(dir_path + r"\programr.exe")
			sys.exit(1)

Maybe it's an initialization problem?

I have found the same question on stackoverflow with a an application coded in Python and converted to an exe using Pyinstaller: https://stackoverflow.com/questions/52403967/python-keyboard-module-not-capturing-keystrokes-when-coverted-to-exe-using-pyins

beuaaa avatar May 22 '20 14:05 beuaaa

Same issue here.

There are hints that in pyinstaller you need to use the hidden includes flag, but this guidance if for a different lib to here. Not sure if it is helpful or not but might be a starting point to fix?

howroyd avatar Aug 22 '22 12:08 howroyd