Take User Input With a Keyboard Widget in PiStorms!
Did you ever need to take textual input with your PiStorms? This tutorial explains how to use the new input class to take input with an on-screen keyboard.
The Input class shows a simple keyboard on the TFT LCD, and allows to take user input using touch screen. It may be useful when creating interactive applications. The characters can also be replaced with asterisks to input passwords.
Start with the import statements.
Create an instance of the PiStorms class to interact with the touchscreen as well as an instance of the PiStormsInput class to allow textual input. This will not show the keyboard just yet.
Now, you are ready to take user input! Call the getInput() method on the object. Setting the “hide” parameter to True, will hide the input on the screen.
The on-screen keyboard shows 4 characters at a time. Use the arrows to navigate through the character set or use the buttons on the top to select the characters.
- Shft – Shift, switch between uppercase and lowercase
- Abc – English alphabet
- ?-! – special characters and space (space looks like an empty button)
- 123 – numbers
- Clr – clears the input box
- Bsp – backspace (deletes the last character)
Pressing either “Cancel” or “Submit” will submit the text.
The getInput() method returns a Python dictionary with two keys: “submitted” and “response”
“submitted” is a Boolean which identifies whether or not the user submitted (True) or cancelled (False) the operation.
“response” returns the user entry at the time the “Cancel” or “Submit” button was clicked. Even if the user clicks “Cancel”, this attribute will still have a value.
For example, in the following output, the user submitted the form and typed “aBc! 123”
Pressing “GO” on the shield will yield the same result as pressing “Cancel”
Next, you can parse the input as applicable in your project. The following code checks whether or not the user cancelled the form, and prints the response.
By default, the program adds a 20 pixel margin on the left. You can hide it by setting left_padding to False in the call to the constructor.
Below is the full sample code: