Shutdown RetroPie/Pi using OSMC Remote Control Button
-
Hello,
This is my first post.
I am trying to restart Retropie using an OSMC remote home button to calls a bash script (not using the emulation station menu option). I think the home button issues a standard keyboard ”Escape” keystroke if I am not mistaken. I have been trying to do this for the past 3 days but to no avail.
I managed to do a similar thing easily in Raspbian by editing “Openbox” keyboard shortcut file but retropie doesn’t seem to have Openbox. I even tried to install openbox but it didn’t work. I know retropie is built on raspbian lite but I don’t want to go through the process of installing pixel desktop, openbox and such.
I tried several solution listed below and I know one of them should work but I couldn’t manage to do it and need help.
- Bash & Python
I managed to read keystrokes but only in Terminal and not in Retropie GUI - Xbindkeys
Seems to need X windows or such - Tkinter
Like Bash & Python Worked in Terminal - PyGame
Worked on raspbian but required a window to be drawn and this will cause issues in retropie - Finally tried /dev/input/by-path approach it seemed crude and should work but it didn’t for some reason
I think one of the above hold the solution but I couldn’t nail it. I would appreciate a working code samples that will let me accomplish this task.
Thanks in advance
- Bash & Python
-
Touch question I reckon.
I managed to solve the issue using the /dev/input/by-path approach.
I have found a script online and adapted it.I will post it shortly. -
Here is the script I came up with. I used a base script for the internet and adapted it to my needs. I also added ability to find keyboards rather than having to input them manually into script and also added threading so you can monitor all keyboards in background.
-
# code block #!/usr/bin/env python import string import os import sys import time import threading # pip install evdev from evdev import InputDevice from select import select # Set function to be called for each keyboard def keyDetectFunc( fullpath ): # look for a /dev/input/by-id/usb...kbd or something similar #DEVICE = "/dev/input/by-id/usb-USB_USB_Keyboard-event-kbd" DEVICE = fullpath dev = InputDevice(DEVICE) while True: # wait for keypad command r, w, x = select([dev], [], []) # read keypad for event in dev.read(): if event.type==1 and event.value==1: # do something with this key press print ("KEY CODE: ", event.code) if event.code == 1: print ("KEY CODE: ", event.code) os.system('sudo shutdown -r now') return; # Return list of all connected input devices path = "/dev/input/by-id/" dirs = os.listdir(path) # returns list # This would get all files in dir and filter out Keyboards only for file in dirs: if ("Keyboard" in file) or ("kbd" in file): print file keyfile = file fullpath = path + file #Call detection funtion in background keydetect_thread = threading.Thread(target=keyDetectFunc, args=(fullpath,)) keydetect_thread.start()
-
Also you need to install the following for code to work:
sudo apt-get install python-dev python-pip gcc
sudo apt-get install linux-headers-$(uname -r)
sudo pip install evdevNote it worked without installing the header i.e 2nd satement
Then you can call it on start up via a cron such as:
@reboot python /home/pi/scripts/keyreboot.py &Remember to give the file execution permisions
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.