Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Raspberry Pi / Simulating keystrokes with GPIO

Author
Message
JDforce
19
Years of Service
User Offline
Joined: 27th Jul 2004
Location: Sea of Tranquility
Posted: 18th Dec 2023 23:47
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:



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:



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
Yeshu777
4
Years of Service
User Offline
Joined: 26th Feb 2020
Location: Cymru
Posted: 19th Dec 2023 17:19 Edited at: 19th Dec 2023 17:20
Hi JD,

You could always use an arduino NANO, connect it serially - leaves your GPIO alone to use for what you want.

Poll the NANO every 100ms via the AppGameKit app, return a buffer of keystokes that occured during that time.

The NANO has Interrupts on PIN change, so doesn't miss much..

I use a similar system for detecting 'real coins' travelling through 16 different opto's at high velocity, the PI couldn't do it, as rendering the video will always take 16ms.

( additonal cost £3.28, even comes with the USB lead )

Yeshu777
JDforce
19
Years of Service
User Offline
Joined: 27th Jul 2004
Location: Sea of Tranquility
Posted: 20th Dec 2023 17:44
Hi Yhesu777,
Thanks for the advise. Is good to have something in common!
As a matter of fact that is one of the configutations I use with AppGameKit on a Ryzen minicomputer with linux or windows. The nano is polled to check for pushed buttons and other data.
The kiosk that uses the raspberry pi uses currently only gpio, no need of keyboard. Today it is a python with pygame system. I am migrating the kiosk app to use AppGameKit because the 3d features, add games and frankly because I like it more than python, in addition of multiplatform capabilities. It is still under development but it looks great.
It's funny that some 15 years ago I was cannibalizing actual usb keyboards and removing the pc board inside to create input devices, haha. Long time ago.

As for the coin detectors, we use actual coinmechs, that resulted in a more reliable overall device (and cheaper) solution. They can be programmed for any currency and are very accurate. Sadly the payment world is now more digital, and coins/bills are declining in use.
Let's go to Mars

Login to post a reply

Server time is: 2024-05-02 06:13:38
Your offset time is: 2024-05-02 06:13:38