0

when I use Chrome, pynput works very good, it tells me which key have I pressed. But when I use some other programes, such as Taskmgr.exe, games, It failed! What should I do? my code:

from pynput import keyboard

last_event_time=time.time()
def on_press(key):
    global last_event_time
    last_event_time=time.time()
    try:
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))

def on_release(key):
    global last_event_time
    last_event_time=time.time()
    print('{0} released'.format(
        key))
    if key == keyboard.Key.esc:
        # Stop listener
        return False
listener2 = keyboard.Listener(
    on_press=on_press)
listener2.start()
AInseven
  • 1
  • 2

1 Answers1

0

I had this particular problem for literally a year in my own code before finding the reason. If a program is launched as elevated, then a non elevated program such as your script cannot read the keyboard.

If it's important that you get it working, I knocked up some basic code a while ago to relaunch my script with admin rights. The file is here, and its run a bit like this:

if __name__ == '__main__':
    console.elevate(visible=not start_minimised)
    # Do your main code here
Peter
  • 3,186
  • 3
  • 26
  • 59