0

So I am not very good with keyboard event handling in Python. I have read the Python Curses tutorial on their official website and looked for answers on SO but it still does not make sense to me. So for starters I want to make a simple program that when it takes a key press such as Escape it prints something simple like You pressed escape!. Sorry I have no code, this is because I do not really know where to start. I think it might have something to do with using functions but I am not sure. You guys do not even have to give me the exact answer but just hints to where I should start!

Heres the code I tried:

from msvcrt import getch

while True:
    key = ord(getch())
    if key == 27: #ESC
        print'You pressed ESC'

but it didnt work? Any ideas?

user3678184
  • 85
  • 1
  • 9

1 Answers1

0

I dunno what you are trying to do, but AutoHotkey Scripts(Windows) might help you in running the appropriate python script with the input taken from it.

paradox
  • 377
  • 3
  • 12
  • I want to make a simple program that runs under the Python shell so that if the user presses `Control+E` something simple like `Hello world` is print... – user3678184 Jun 10 '14 at 06:23
  • Not sure if this works for you though.If you have some programming experience this might help [link](http://stackoverflow.com/questions/12175964/python-method-for-reading-keypress). I think you might have already gone through this.. Try to use this and experiment to see what output you get with each keypress. When you find what you want, create a conditional in your code checking for this and do whatever you wanted to do for this combination. – paradox Jun 10 '14 at 06:31
  • I will edit my question and look at the code I tried. I ran it without getting any errors but it still didn't work...any ideas – user3678184 Jun 10 '14 at 17:11
  • it worked perfectly for me. It returns 27 for `ESC` and 3 for `Ctrl + C` . Maybe you are running it in an interactive environment when it always put `nbsp` on to the input buffer. Try running it from the command line instead of IDLE or any other interactive python shell. `from msvcrt import getch while True: key = ord(getch()) print key if key == 27: #ESC print'You pressed ESC'` i added the extra `print key` so that you will understand that it always prints 255 into the IDLE shell. – paradox Jun 11 '14 at 06:22
  • I tried you code but because it is a while loop the console continuously prints 225. So I omitted the `print key` part and ran it but still nothing? – user3678184 Jun 13 '14 at 18:53