Pi-top[4] miniscreen buttons as input

I’ve looked around in the docs and also example code and not finding anything yet. I’m wondering if there would be a way to use the buttons on the pi-top[4] as input for Python code.

For example, the input() method waits for user input from a keyboard.

Ideas on how the up button or down button to send response to as input.
Thanks in advance.

@Tom-B could you just use the when_pressed function of the miniscreen buttons and in the callback send a character to the terminal?

I also found this which might be useful:

https://pypi.org/project/python-uinput/

I’m sure I’m just over-thinking it in general. Python’s input() waits for a keyboard input but I hadn’t found a way for the when_pressed function to wait like Python’s input() does. I will check out the python-uinput. Thank you.

This will help you, the info you need is in the SDK docs

https://pi-top-pi-top-python-sdk.readthedocs-hosted.com/en/latest/api_pitop_device.html#class-reference-pi-top-4-miniscreen-button

from pitop.miniscreen import Miniscreen
from signal import pause

def do_up_thing():
    print("Up button was pressed")

miniscreen = Miniscreen()
miniscreen.up_button.when_pressed = do_up_thing

pause()

Something like this?

Thank you all for the assistance.

So, for example, I have code in my Python script:

direction_choice = input("Which direction: U (up) or D (down): ")

User input “U” or “D” from the keyboard would proceed. – However, I’m looking for a way to have the miniscreen up or down button process this input instead.

I’m showing off my fairly amateur skill at Python code here. LOL

User input “U” or “D” from the keyboard would proceed.

This is not correct - input() will capture any string from the user, which could be any length! I don’t think you have understood this function quite right. It is capturing user input and storing it as a variable. This is nothing to do with buttons.

However, I’m looking for a way to have the miniscreen up or down button process this input instead.

This also does not make sense, unfortunately. A button cannot “process input” - it is an input!

What you are actually trying to achieve? Perhaps then we can more easily provide you with a solution.

Thank you. I am sure I’m not understanding the miniscreen buttons well enough. Sorry for asking something clearly by someone still trying to learn.

I’m looking for a way to have the up button (for example) update a variable “direction_choice” to be equal to “u” as if the user typed “u” on their keyboard. I know I can do that in an if block. I’m confident my approach is way off and I’ll keep working at it.

I am sure times like this is what makes or breaks someone who is still learning Python. :smiley:

Thanks again.

@Tom-B send us a GitHub repo to look at and we can offer some more advice on how to structure your code to work best with the miniscreen buttons :+1: