brickPi+ Python Keyboard Input Question

BrickPi/Robotic_Arm.py at master · DexterInd/BrickPi · GitHub

Hello. I am following the robotic arm example and its working but I cant figure out how to make the robot arm move in tiny increments on each key press instead of moving indefinitely until another key is pressed per the example code.

Hope this makes sense. It currently moves until another key is pressed to stop it. I want to code it so it stops moving on key up. what do I change in the python example code to achieve this? Thank you

2 Likes

Please give a link to the example so we can have a look

1 Like

Hello. The link is at the top of my post.

BrickPi/Robotic_Arm.py at master · DexterInd/BrickPi · GitHub

2 Likes

Seems like this is tougher than expected.

Is a module which appears to offer key press and key release, but you will have to figure out exactly which example will work for you

from pynput import keyboard

def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))

def on_release(key):
    print('{0} released'.format(
        key))
    if key == keyboard.Key.esc:
        # Stop listener
        return False

# Collect events until released
with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

# ...or, in a non-blocking fashion:
listener = keyboard.Listener(
    on_press=on_press,
    on_release=on_release)
listener.start()

1 Like

Caveat:
I am not an expert at Python.

In the past, when using other languages, key press usually means press-and-release.

To detect the actual push of a key, the action I have had to use was the event key_down, and the key_up event detects when the key is released.

  • Note that the equivalent mouse events are [left|right] button_pressed and the corresponding button_down and button_up events.

This may be modified by other things that determine if the key will repeat if held down.  (i.e.  Will the key send repeated key_down messages or not.)

Additional research will be needed as detecting key motion as opposed to key presses is not trivial.