raspberry pi keyboard python input 1

raspberry pi keyboard python input

import tty, sys, termios

filedescriptors = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin)
x = 0
while 1:
  x=sys.stdin.read(1)[0]
  print("You pressed", x)
  if x == "r":
    print("If condition is met")
termios.tcsetattr(sys.stdin, termios.TCSADRAIN,filedescriptors)

Here is what the above code is Doing:
1. It’s getting the file descriptor for the terminal.
2. It’s setting the terminal to cbreak mode.
3. It’s reading a single character from the terminal.
4. It’s printing the character.
5. It’s checking if the character is “r”.
6. If the character is “r”, it prints “If condition is met”.
7. It’s setting the terminal back to its original state.

Similar Posts