2

I'm trying to read a character in python so that to perform an action based on the input. I understood there is no easy way to read key strokes in python with the help of Google. I finally found 'readchar' package which is promising but I was not able to make it work in a simple program. Any help is greatly appreciated.



    import readchar

    def keyprint():
        while True:
            print "Press 'A' to Start the recording"
            print "      'Z' to Stop the recording"
            print "      'Enter' to quit the program..."

            # Read a key
            key = readchar.readkey()
            if(key == 'A'):
                print "Started Recording..."
            elif(key == 'Z'):
                print "Stopped Recording..."
            elif(key == '\r'):
                print "Exiting..."
                break
            else:
                print "Please Use only allowed keys: A, Z, Enter!" 

    if __name__ == "__main__":
        keyprint()

EDIT: Output Error

File "/home/inblueswithu/Documents/LM_DataCollection/keystroke_test.py", line 22, in <module>
  keyprint()
File "/home/inblueswithu/Documents/LM_DataCollection/keystroke_test.py", line 10, in keyprint
  key = readchar.readkey()
File "/home/inblueswithu/.local/lib/python2.7/site-packages/readchar/readchar.py", line 20, in readkey
  c1 = getchar()
File "/home/inblueswithu/.local/lib/python2.7/site-packages/readchar/readchar_linux.py", line 12, in readchar
  old_settings = termios.tcgetattr(fd)

termios.error: (25, 'Inappropriate ioctl for device')

Thanks, inblueswithu

inblueswithu
  • 953
  • 2
  • 18
  • 29
  • what are you getting as your key? – R Nar Oct 23 '15 at 18:56
  • @RNar Sorry, forgot to add the original error message! Now edited the question. – inblueswithu Oct 23 '15 at 19:31
  • 1
    that doesn't look like a problem with your code, that traceback looks like a problem with the library... also, why dont you just use `raw_input`? – R Nar Oct 23 '15 at 19:35
  • @RNar Thanks a lot for the workaround. 1. I'm new to python and I didn't know the `raw_input` method. 2. I tested it, it works very close to what I wanted but I need to press 'Enter' key after any input. I need it to be instantaneous, like soon after pressing the key. 3. Yes, the trace actually goes back to the readchar package here : https://github.com/magmax/python-readchar I opened an issue in there as well – inblueswithu Oct 23 '15 at 19:48
  • 1
    ah in that case, there is this http://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user that uses` Getch` – R Nar Oct 23 '15 at 19:51
  • Infact I found readchar package from that page in this post: http://stackoverflow.com/a/25342814/815539 . This looked like the easiest of them all. Probably I should try the other methods.. – inblueswithu Oct 23 '15 at 19:56

1 Answers1

3

After discussing this issue on github with the creator of the project - magmax (here), I understood that readchar package works only if you try to run it from a terminal but not from any IDE or other non terminal executions. The error is caused because it tries to get the terminal settings which is non existent in this case.

I have been trying to run it from an Wing IDE. The program works great if you try to run it from a terminal.

P.S: magmax suggested to use readchar.keys.ENTER instead of \r . And suggested to have a look at https://github.com/magmax/python-inquirer & its examples

inblueswithu
  • 953
  • 2
  • 18
  • 29