Sending Virtual Keystrokes
-
Im in deep, Im trying to build a script that will mock out keystrokes. I have two use cases (maybe there is a better way). Both of these are because I dont want to have a dedicated keyboard.
I already have a Joystick button listener that will trigger a script when buttons are pressed. I use this as a safe shutdown, as I had odd results of unwanted shutdowns using the GPIO script
-
I have a gamepad that only has 1 button. It doesnt really work well since I dont have start/select. I'd like to set up one of the button triggers to kick off a script that will simulate keypresses (start/select). This way I mock key strokes with my other buttons
-
If that works, I'd love to then make a EXIT game button that can be used to send the hotkey/exit, as again this single controller doesnt have it - and sometimes other emulators dont like to honor the exit (one of the MAMEs, dont recall which but I have to use the mame menu to exit)
My current rabbit hole is leading me to think that I need to hook into the SDL library? But A) Im not sure if this is true B) Im not finding much on how to send those key commands. Maybe one of the awesome devs has an insight into this?
Thoughts? Am I way off base?
-
-
side note - Ive also successfully mocked keystrokes via Python. From an SSH session, I can see these mocked keystrokes on the emulation station console (when in the F4 console). However, I can not get these to be recognized outside the console.
There appears to be some others trying to do something similar with similar results
https://github.com/tuomasjjrasanen/python-uinput/issues/28 -
Using jslisten and python-uinput I think Ive solved this.
I can define button combos and call a script that sends the proper key combos.START and EXIT EMULATION are working from a gamepad outside of the player 1
-
Some more info and links to scripts are found here:
https://www.reddit.com/r/RetroPie/comments/eij3jy/custom_hotkey_button_combos/Button combos currently:
- Safe Shutdown
- Exit emulation
- Start Button (for my Atari style single button gamepad)
-
Playing around with this some more Ive also gotten it to
- put the monitor into sleep or wake it up: vcgencmd display_mode
- mute/unmute: sudo amixer cset numid=2
So now its a nice simple way to turn the monitor off ...
Also thought about creating a volume up/down command which could come in handy -
hey @icebear81!
I'm facing a similar problem. As RetroPie/Emulationstation do not rely on X11, most libraries to emulate a virtual keyboard input don't work.
I'm trying to send key presses to a running game. The following python3 script works in the emulationstation menu, but doesn't in game (
lr-gambatte
).Do you have any idea how I can send virtual keystrokes (via a python script, started on an SSH session) to a running game?
import time import uinput def main(): events = (uinput.KEY_LEFT, uinput.KEY_DOWN, uinput.KEY_D) with uinput.Device(events) as device: time.sleep(1) device.emit_click(uinput.KEY_LEFT) time.sleep(0.2) device.emit_click(uinput.KEY_D) time.sleep(0.2) device.emit_click(uinput.KEY_D) time.sleep(0.2) device.emit_click(uinput.KEY_DOWN) time.sleep(0.2) if __name__ == "__main__": main()
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.