Reputation: 193
I'm using python to create an empty set(), every keystroke i press adds to that set, which works very well!
But when i for example press "s" and try to iterate over the set, it always returns false. What am i doing wrong? Thanks!
from pynput import keyboard
pressed = set()
def on_press(key):
pressed.add(key)
# THIS RETURNS FALSE EVERYTIME!!! ???
print('s' in pressed)
def on_release(key):
if key in pressed:
pressed.remove(key)
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
Upvotes: 0
Views: 214
Reputation: 39354
You are just storing instances of pynput.keyboard.<platform>.KeyCode
. You meant to do this:
def on_press(key):
pressed.add(key.char)
print('s' in pressed)
Upvotes: 2
Reputation: 11060
The object you are adding to the set is not the string s
but instead a 'pynput.keyboard._xorg.KeyCode'
class instance. So your in
test will fail as these items are not equal.
To get the key's character, you need to do:
pressed.add(key.char)
Upvotes: 4