1

How to keep track of alphanumeric characters and non special characters (space bar, delete, tab, etc) in pynput. I am using the below code. This code outputs only the special characters and not any other characters input through keyboard.

from pynput.keyboard import Key, Listener

#defining function to print when key is pressed
def on_press(key):
    print('{0} pressed'.format(key))

#defining function to print when key is released
def on_release(key):
    print('{0} release'.format(key))
    if key == Key.esc:
        # Stop listener
        return False

# Collect events until released
with Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()
BoarGules
  • 16,440
  • 2
  • 27
  • 44
NightMan
  • 11
  • 2
  • its working in my pc – Nimish Bansal Mar 31 '18 at 22:10
  • I think I don't understand the question. You say "this code outputs only the special characters and not any other characters input through keyboard". But if I try it, it reports *all* keypresses and releases, for example `'t' pressed`. I tried it in IDLE and in PyCharm and can't reproduce the behaviour you report. – BoarGules Mar 31 '18 at 23:01
  • @BoarGules I am not getting the above mentioned output... Is it because of the editor I am using? – NightMan Apr 01 '18 at 08:00
  • Might be. What are you using? Have you tried running this code in `python.exe`? – BoarGules Apr 01 '18 at 08:01
  • Sublime.. I'll try and report back – NightMan Apr 01 '18 at 08:07
  • @BoarGules No change in output even when running in `python.exe`. When I press any alphanumeric character it just doesn't respond. Are you just typing the character or giving utf16 encoding? – NightMan Apr 01 '18 at 08:57
  • I'm just typing the character, working with exactly the code that you posted, except that I had to fix up the indentation errors. It looks like you're not running the code you think you are running. – BoarGules Apr 01 '18 at 08:59
  • I ran the same code on widows PC and its running fine. – NightMan Apr 08 '18 at 10:43
  • Having the same issue on MasOS. – 0xTomato Oct 25 '21 at 01:35

0 Answers0