Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
  • matthewjosephcordova
    Post count: 22

    I’ve came up with a method to be able to have an external pushbutton that is used to exit an emulator back to emulationstation. The way it works is by either sending a sigterm signal to the emulator, and if that doesn’t work, to try simulating a keypress (ESC). Some emulators don’t accept keypress from python’s uinput, so sigterm must be used. Some emulators don’t accept sigterm, so uinput must be used.

    This is a python script, and uinput must be installed. You can test if uinput is properly installed by going to a command prompt and typing in
    Then when you are in python idle,
    import uinput
    If it comes back with >>> , everything is working fine. If it says ImportError: No module named uinput, then it isn’t installed. On my image I downloaded from here, uinput was not installed correctly. To install uinput, try

    pip install python-uinput
    If that doesn’t work, try
    easy_install python-uinput

    RPi.GPIO must also be installed. In a command window, try
    sudo apt-get install python-dev python-rpi.gpio

    Now the script must be created. Save the following script as in /home/pi/

    from time import sleep
    import os
    import RPi.GPIO as GPIO
    import uinput
    key_events=( uinput.KEY_ESC, )
    GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    def exitEmulator(channel):
        pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
        for pid in pids:
                commandpath = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read()
                if commandpath[0:24] == '/opt/retropie/emulators/':
                    os.system('kill -TERM %s' % pid)
                    print('kill -TERM %s' % pid)
            except IOError:
    GPIO.add_event_detect(3, GPIO.RISING, callback=exitEmulator, bouncetime=500)
    while True:

    Now create another file, in /home/pi/

    cd /
    cd /home/pi
    sudo python
    cd /

    Now, cron must be edited. Enter in a command terminal,
    sudo crontab -e
    Add the following line to the end,
    @reboot sh /home/pi/

    Some of the code above has been gathered from around the web. –this link– shows how to exit all emulators too, however if done by this method, it is a hard exit because sigkill is used. This can cause all sorts of headaches like SRM’s not being saved, high scores not being saved using mame, etc… So I modified it to use sigterm. However, mame4all doesn’t accept sigterm signals and I certainly didn’t want to use a sigkill, so then I added uinput KEY_ESC to exit out of mame4all and it works.

    The button works for me as intended. I set up a pushbutton attached to GPIO 3 on a Raspberry Pi 2 and it exits the emulator cleanly to emulationstation. Because I have it set to GPIO3, it also boots my Pi when it is shut off.

    If there are any other questions, I’ll do my best to answer them.

    Post count: 56

    thanks for the info and can you take a pic of how to add the button to the GPIO .

    Post count: 22

    It’s simple, just connect from GPIO3 to ground using a push button type button. Look at your rpi documentation for the correct pins. On the rpi2 b+ they are right next to each other

    Post count: 3

    thanks for sharing this.

    i do not understand why gpio boots up your pi…what am i missing here?

    Post count: 22

    It’s a largely undocumented feature using GPIO3 to boot a Pi. See
    ‘Wake from halt’

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.