What is the easiest way to detect keystrokes in python 3 on a linux machine?

Now I'm trying to make a small code with raspberry pi and make-up. Makey makey is a small board that acts like a USB keyboard when connecting certain contacts. My question is the easiest way to detect these keystrokes inside a python script. I understand that using GPIO contacts would be easier, but now I'm looking for this. I saw examples such as using getch () from msvcrt (from which I only understand windows), using pygame.key and using getKey. Which of these theses is easiest to use? Are there any that can detect a keystroke and release a key?

Pseudo-code code (... is this what he called?)

import whatever needs importing if the "W" key is pressed: print ("You pressed W") elif the "S" is pressed: print ("You pressed S") 

etc. Thanks.

+8
python linux raspberry-pi
source share
2 answers

This is a simple loop that puts stdin in raw mode (disabling buffering so as not to press the enter key) to get single characters. You have to do something smarter (like the with statement to disable it), but you get this idea here:

 import tty import sys import termios orig_settings = termios.tcgetattr(sys.stdin) tty.setraw(sys.stdin) x = 0 while x != chr(27): # ESC x=sys.stdin.read(1)[0] print("You pressed", x) termios.tcsetattr(sys.stdin, termios.TCSADRAIN, orig_settings) 

I think you will have to focus on detecting key releases in Python.

ETA some more explanations:

On Linux, entering your program will be with line buffering . This means that the operating system will buffer the input signal until it has a whole line, so your program will not even see anything that the user typed until the user also presses 'enter'. In other words, if your program expects the user to type β€œw,” and the user does, β€œw” will sit in the OS buffer until the user presses β€œenter”. At this point, the entire string is delivered to your program, so you will get the string "w \ n" as user input.

You can disable this by setting tty to raw mode . You do this with the Python function tty.setraw , which will call the linux tty driver to tell it to stop buffering. I passed him the sys.stdin argument to tell him which thread I wanted to disable buffering for 1 . Therefore, after calling tty.setraw above loop will give you the output for each key that the user presses.

However, the difficulty is that after the exit of your program, tty is still in raw mode. Usually you will find this dissatisfaction, because you do not get any power offered by modern terminal settings (for example, when you use escape or escape sequences). For example, note that you may have a problem exit from the program using ctrl-C . Therefore, after completing the input of input characters, you must return the terminal to the prepared mode . The termios.tcsetattr call simply says: "return the terminal as I found it." He knows how to do this by first typing termios.tcgetattr at the beginning of a program that says "tell me all the current terminal settings."

Once you understand all this, you can easily encapsulate functionality in a function that is suitable for your program.

1 stdin is a stream that introduces you from the user. Wikipedia can tell you more about standard streams .

+9
source share

Using a nice easy curtsies module, you can do something like this (taken from their examples / directory):

 from curtsies import Input def main(): with Input(keynames='curses') as input_generator: for e in input_generator: print(repr(e)) if __name__ == '__main__': main() 

So pressing the keys on the keyboard gives you something like this:

 'a' 's' 'KEY_F(1)' 'KEY_F(2)' 'KEY_F(3)' 'KEY_F(4)' 'KEY_F(5)' 'KEY_LEFT' 'KEY_DOWN' 'KEY_UP' 'KEY_RIGHT' 'KEY_NPAGE' '\n' 

curtsies is used by bpython as a low-level abstraction of terminal-related things.

The main problem with decoding input is that in different terminals and terminal emulators, such as xterm or gnome-terminals physically the same keys produce different key sequences. Therefore, you need to know what terminal parameters should be used to decode the input. Such a module helps to abstract from these details.

+8
source share

All Articles