Sometimes it is useful to simulate keystrokes by using the GPIO pins. In this way, by connecting a switch to the GPIO pin, your application will think it is a keyboard stroke.
It could be useful for a game where you don't want or you can't use a keyboard, but can use switches or sensors with the GPIO pins.
NOTE: Enabling a key stroke simulation to a given GPIO pin will prevent the pin from being used normally in AppGameKit, Python or any other way. The application will receive the keystroke as keyboard input.
I found this before Tboy shared a compiled PiPlayer for the raspberry pi that actually works with GPIO inputs and outputs.
But any way this approach can be useful, so here it goes:
Have a GPIO pin diagram handy, like the one here:
https://www.raspberrypi.com/documentation/computers/raspberry-pi.html
Using the nano editor, open the config file on the raspberry pi:
sudo nano /boot/config.txt
Nano is a text only terminal editor, but you can use text editor on the graphical interface, visual studio code local or remote or any equivalent.
Then add the following lines at the bottom of the file:
# Enable keystroke simulation via GPIO
# Add one line of dtoverlays to /boot/config.txt per keystroke to simulate.
# up with gpio3
dtoverlay=gpio-key,gpio=3,active_low=1,gpio_pull=up,keycode=103
# down with gpio 4
dtoverlay=gpio-key,gpio=4,active_low=1,gpio_pull=up,keycode=108
# left with gpio 17
dtoverlay=gpio-key,gpio=17,active_low=1,gpio_pull=up,keycode=105
# right with gpio 27
dtoverlay=gpio-key,gpio=27,active_low=1,gpio_pull=up,keycode=106
# space (fire) with gpio 22
dtoverlay=gpio-key,gpio=22,active_low=1,gpio_pull=up,keycode=57
# letter Q with gpio 23
dtoverlay=gpio-key,gpio=23,active_low=1,gpio_pull=up,keycode=16
The above lines effectively add a driver for each pin, to tell the system that when the gpio pin configured is activated, a keystroke will be simulated for the whole system.
Note: GPIO numbers are not equal to physical pins.
Gpio 3 is physical pin 5. keycode 103 is UP arrow
Gpio 4 is physical pin 7. keycode 108 is DOWN arrow, and so on.
When you are done, CTRL-X, Y will save the file and you have to reboot with
Of course you can map any valid keycode to the Gpio pin you want.
See input-events.h for the complete list of keycodes, at
https://github.com/torvalds/linux/blob/v4.12/include/uapi/linux/input-event-codes.h#L64)
Let's go to Mars