Chanchai
Guest
Post count: 908

I use gpio pin to emulate keyboard.
I use python language for programing.

On RetroPie 1.9. I use python-uinput module. It work fine.
On RetroPie 2.x The python-uinput module is not work.

I solve this problem. I use python-evdev module. It work fine.

I think. uinput is not work on RetroPie 2.x.
Maybe libevdev is work on RetroPie 2.x.

Python-evdev Module
https://pythonhosted.org/evdev/

Source code
https://github.com/gvalkov/python-evdev

Example Code
I use gpio pin 24 for ESC Keyboard for exit game

#!/usr/bin/python
import time
from evdev import UInput, ecodes as e
import RPi.GPIO as GPIO

# GPIO PIN
pin_esc = 24

ui = UInput()

# Bools to keep track of movement
key_esc = False

GPIO.setmode(GPIO.BCM)

GPIO.setup(pin_esc, GPIO.IN, pull_up_down = GPIO.PUD_UP)
try:
	while True:
		if (not key_esc) and (not GPIO.input(pin_esc)):
			key_esc = True
			#print("Button Pressed");
			ui.write(e.EV_KEY, e.KEY_ESC, 1); # KEY_ESC down
			#ui.write(e.EV_KEY, e.KEY_F4, 1); # KEY_F4 down
			ui.syn()

		if (key_esc) and (GPIO.input(pin_esc)):
			key_esc = False
			#print("Button Released");
			ui.write(e.EV_KEY, e.KEY_ESC, 0); # KEY_ESC up
			#ui.write(e.EV_KEY, e.KEY_F4, 0); # KEY_F4 up
			ui.syn()

		time.sleep(0.3);
except KeyboardInterrupt:
	ui.close()
	GPIO.cleanup()