1

I tried this

from msvcrt import getch

while True:
    key = ord(getch())
    if key == 27: #ESC
        print("You pressed ESC")
    elif key == 13: #Enter
        print("You pressed key ENTER")

but it works only in terminal, i want to run a function whenever user presses the key, even his curser in not in terminal, Please help... Thank you!

Himanshu Kawale
  • 389
  • 2
  • 11
  • Does this answer your question? [Key Listeners in python?](https://stackoverflow.com/questions/11918999/key-listeners-in-python) – Filipe Rodrigues Aug 04 '20 at 16:29
  • Unfortunately No, Actually, i want to run a function whenever user presses a key, for example, on windows when we press "window" key it opens th start menu even if the user is using any software on the computer – Himanshu Kawale Aug 05 '20 at 08:48

1 Answers1

0

What you're trying to do is not the easiest thing if you want to get it working correctly. If you have a window/widget in focus, using Qt and a QKeyEvent would be the easiest solution. However, if you want your input to be read globally/in other applications and not just from the terminal, you'll need to do something a bit more involved:

How to generate keyboard events in Python?

Jacob Kaniuk
  • 175
  • 6