0

I was wondering if there was a way to detect a pressed key with python independently of the window that is open. I tried with ord(getch()) but it works only if you have the cmd open. I would like it to work even if I was, for instance, browsing on Google Chrome.

  • Sounds like your making a key logger. Search for `python keylogger` – notorious.no May 20 '17 at 00:08
  • This seems a duplicate to me. I believe [pynput](http://stackoverflow.com/questions/11918999/key-listeners-in-python/43106497#43106497) is what you like to use. – Xiangrui Li May 20 '17 at 00:52

1 Answers1

0

Three years later... But here goes... Installing the package pip install keyboard and use the is_pressed method. I've tested this on Windows, can't confirm whether or not it works on Linux/MacOS.

import keyboard
import time
while True:
    time.sleep(0.01)
    if keyboard.is_pressed('a'):
        print('a')
polortiz40
  • 391
  • 4
  • 13