RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login
    Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

    Script keypresses in game

    Scheduled Pinned Locked Moved Help and Support
    keyboardscriptpythonautomationbotdev
    3 Posts 2 Posters 532 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • emma-makesE
      emma-makes
      last edited by

      I'm trying to automate a number of moves with a python script to control my character in game (lr-gambatte). As RetroPie/Emulationstation do not rely on X11, most libraries to emulate a virtual keyboard input don't work.

      The following python3 script (using uinput, https://github.com/tuomasjjrasanen/python-uinput) works in the emulationstation menu, but doesn't in game (lr-gambatte).

      Does anyone have an idea, how to send/emulate keystrokes via a python script that is started via SSH? I'm not opposed to switching the script language.

      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_DOWN)
              time.sleep(0.2)
      
      if __name__ == "__main__":
          main()
      
      1 Reply Last reply Reply Quote 0
      • mituM
        mitu Global Moderator
        last edited by

        Your script is simulating a virtual keypad/gamepad, but RetroArch may be ignoring the device if it's not properly registered with udev as a keyboard.

        Run udevadm info -q all /dev/input/eventX, where /dev/input/eventX is your device's path, and see if it has ID_INPUT_KEY=1 or ID_INPUT_KEYBOARD=1.

        You can take a look at https://pypi.org/project/ubox360/ to see a more fleshed out example of simulating an Xbox 360 controller.

        emma-makesE 1 Reply Last reply Reply Quote 1
        • emma-makesE
          emma-makes @mitu
          last edited by

          @mitu Interesting! Thank you very much for your feedback! I'll check how the library emulates the keyboard!

          My fallback is a microcontroller to emulate the keyboard, if the software portion is too complex.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post

          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.