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

    Virtual keyboardpress via python-uinput or evdev not working in games

    Scheduled Pinned Locked Moved Help and Support
    retropiegpiocontrollerkeyboard encode
    1 Posts 1 Posters 1.6k 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.
    • R
      Robbie
      last edited by

      Hi All,

      I know similar topics have been posted, but none of these helped me fix my issue.

      I use USB arcade joysticks and buttons, which work fine, except I like to have a bunch of not use specific buttons, such as load, save, menu, pause, exit emulator. Because the buttons fysically don't fit on the USB controller (not enough inputs) I resorted to a PCF8575, which connected over I2C gives me 16 extra inputs.

      I use a python script to 'convert' the button presses into keyboard presses. Which seems to work fine on the console and in the RetroArch config: Can even map the pressed buttons. But they won't work in games. I've disabled the hotkey key, so just pressing f4 should load a game. Tested the working using a real usb keyboard. But it won't listen or maybe hear, my custom buttons.
      Press escape in a running game with a real keyboard, game/emulator exits.
      Press button which is linked to escape, doesn't seem to register in the game/emulator.
      Press button which is linked to escape in any of the non GUI menus or RetroArch config, it registers.

      As a second 'symptom': In the Input Hotkey Binds in RetroArch config, the binding is not done to just the button, but it adds Auto: # (N/A) (Key: <My key>).

      Hotkeys are disabled in /opt/retropie/configs/all/retroarch.cfg & /opt/retropie/configs/snes/retroarch.cfg and testing with SNES game. (Also tried Gameboy)

      $ cat /opt/retropie/configs/all/retroarch.cfg |grep -i "input_enable"
      input_enable_hotkey = "nul"
      input_enable_hotkey_btn = "nul"
      input_enable_hotkey_axis = "nul"
      
      $ cat /opt/retropie/configs/all/retroarch.cfg |grep -i "input_enable"
      input_enable_hotkey = "nul"
      input_enable_hotkey_btn = "nul"
      input_enable_hotkey_axis = "nul"
      

      Excerpt from Python script:
      The PCF8575 registers a button press as 0, so normal state is 16 bit on (0x1111111111111111), button press makes pin go 0 (0x1111111111111110) if pin 1 is used, every pin higher toggles a bit to the left. So unpressed is 65535 (decimal), pin 1 button pressed, is 65534 decimal, etc

      #!/usr/bin/python
      import smbus
      import time
      from evdev import uinput, ecodes as e
      
      i2c = smbus.SMBus(1)
      i2c_addr = 0x20
      
      def btn00():
              print 'Button 00 / "Return" Pressed!'
              print 'emulating button ESCAPE'
              with uinput.UInput() as ui:
                      ui.write(e.EV_KEY, e.KEY_ESC, 1)
                      ui.write(e.EV_KEY, e.KEY_ESC, 0)
                      ui.syn()
      #Contains a functions for all 16 single possiblities, removed for readability
      
      buttons = {
              65534 : btn00
             #Contains a list of all 16 single possiblities, removed for readability
      }
      
      while True:
              input_state = i2c.read_word_data ( i2c_addr, 0 )
              if input_state != 65535:
                      if (input_state) in buttons:
                              try:
                                      print 'Pressing Button...'
                                      buttons[input_state]()
                              except:
                                      print "Button is known, but sending has failed..."
                      else:
                              print "An unknown button or combination of buttons is pressed", bin(input_state)
                      time.sleep(0.2)
      

      Current script uses evdev, previous version used python-uinput, but the latter didn't even register in retroarch-config or non gui menus.

      • Pi Model or other hardware: RPi 3
      • Power Supply used: Wicked Chili 5V,3A MicroUSB
      • Version Used: commit e1e2d8daa0f09311254df6f81cb633146da6068f / Date: Sat Jul 1 16:21:48 2017 +0100
      • Built From: Pre made SD Image on RetroPie website, updated with buildin scripts
      • USB Devices connected:
        • DragonRise joysticks (x2)
        • generic SNES gamepad
        • generic USB keyboard
        • usb stick (ROMS)
        • usb soundadapter
      • Controller used: Python evdev
      • Error messages received: None
      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.