3

I'm currently working on project where I ssh to a raspberry pi from my laptop to control some motors. I have written some code in Python that allows you to enter a letter and depending on the letter it moves forwards or backwards. However you have to press enter after every letter for the code to execute. Is there a way that the interface detects letters without the need to press enter. I know you can bind key presses in tkinter but I can not do that through ssh. Thanks in advance

george
  • 188
  • 12

2 Answers2

3

You could use the curses library for that.

You can grab the key that was pressed using the screen.getch() function. It will return the decimal code of the key (see ASCII Table).

An example:

import curses


screen = curses.initscr()
curses.cbreak()
screen.keypad(1)

key = ''

while key != ord('q'):  # press <Q> to exit the program
    key = screen.getch()  # get the key
    screen.addch(0, 0, key)  # display it on the screen
    screen.refresh()

    # the same, but for <Up> and <Down> keys:
    if key == curses.KEY_UP:
        screen.addstr(0, 0, "Up")
    elif key == curses.KEY_DOWN:
        screen.addstr(0, 0, "Down")

curses.endwin()
Igor Hatarist
  • 5,234
  • 2
  • 32
  • 45
  • Okay that's the kinda thing I need, thanks. If I wanted to print out what was entered do you just do print(key). I'm not sure what the actual input is – george Feb 06 '15 at 22:45
  • @JoranBeasley although I am running it from a raspberry pi I don't use Linux coding. I just run some Python script. – george Feb 06 '15 at 22:47
  • that is python ... but that code only works on linux (ie raspberrry pi) – Joran Beasley Feb 06 '15 at 22:48
  • Okay thanks that is clear now. I will implement this in my code. And it should work. :) – george Feb 06 '15 at 22:54
  • @JoranBeasley okay. I hadn't seen those terms within Python before so assumed they were bash or something else. Cheers though – george Feb 06 '15 at 22:55
  • Does this only work in Python 2.7 I'm running 3. Something and apparently the is not attributes of the module. ie it can not find curses.initscr or cbreak or any others – george Mar 04 '15 at 16:49
  • @user3811719 It should certainly work on Python 3... https://docs.python.org/3/howto/curses.html – Igor Hatarist Mar 04 '15 at 17:18
  • 2
    Using curses might be an overkill as it is mainly used for drawing full screen cli applications. See my answer for just bind events to keypresses – ilon Oct 27 '21 at 15:22
2

Another option is sshkeyboard library. Simply pip install sshkeyboard, and then use the following code to detect key presses over SSH:

from sshkeyboard import listen_keyboard

def press(key):
    print(f"'{key}' pressed")

def release(key):
    print(f"'{key}' released")

listen_keyboard(
    on_press=press,
    on_release=release,
)

Inside def press you could have some logic to react to specific keys:

def press(key):
    if key == "up":
        print("up pressed")
    elif key == "down":
        print("down pressed")
    elif key == "left":
        print("left pressed")
    elif key == "right":
        print("right pressed")
ilon
  • 171
  • 1
  • 5