PyUserInput
PyUserInput copied to clipboard
How to listen for key events via PyKeyboardEvent
Compared to PyMouseEvent, PyKeyboardEvent event handling is deficient in two ways:
- It uses two functions (key_press and key_release) instead of a single function with a toggle argument.
- The underlying event processing does not provide abstracted key information, making cross-platform key event handling difficult.
To address this I am proposing that key_press and key_release be combined into a single function like PyMouseEvent.click(), and I wish to modify the internal event handling to pass abstracted key information to the function. Essentially the user should be able to write a handler rule for something like "shift_left" or "k" and it should work on all systems. To do this the names of keys should probably be standardized (like the mouse buttons are in PyMouse) and passed instead of the keycodes (or probably alongside them). Some difficulty might be introduced however by overloaded keys which generate different characters or events depending on masks such as Shift/Alt/Fn/Lock. I am open to suggestions and I will begin testing potential solutions.
Is it possible to listen to the key pressed by the user? When i use the following code to check the key that is pressed i'm getting an error.
It would be really great if you could help me in this issue.
Code :
from pykeyboard import PyKeyboard, PyKeyboardEvent
class ClickKeyEventListener(PyKeyboardEvent):
def tap_key(self, character, n, interval):
print character
k = ClickKeyEventListener() k.start() x=1 while 1: x=x
Error :
Traceback (most recent call last): File "C:\Python27\lib\site-packages\pyHook\HookManager.py", line 351, in KeyboardSwitch return func(event) File "C:\Python27\lib\site-packages\pykeyboard\windows.py", line 251, in handler self._key_release(reply) File "C:\Python27\lib\site-packages\pykeyboard\windows.py", line 282, in _key_release self.key_release() TypeError: key_release() takes exactly 2 arguments (1 given)
A couple things to note. One is that your class should be slightly changed to reflect the differences in PyKeyboardEvent's interface, like so:
class ClickKeyEventListener(PyKeyboardEvent):
def tap(self, keycode, character, press): # press is boolean; True for press, False for release
print(character)
Another thing is that PyKeyboardEvent for windows is unfinished at the moment, and I'm not sure what character results you will get. I think you should be okay with basic characters, and pressing modifier keys (such as Shift key) should yield something like 'Shift', 'LShift', etc. I haven't had a chance to test the code in its current state, so let me know how it works.
@ranji2612 I have done some updating on the windows implementation of PyKeyboardEvent. The latest version should correctly handle lowercase ascii which it was not doing before. In the future, I expect to enable modifier state tracking within PyKeyboardEvent as well as draw up a scheme for standard keys across platforms, so some keynames may change in future versions.
@SavinaRoja I just now checked, its working fine for windows.. Thanks a lot..