2

I know its possible to assign a Hotkey to run a Python script by configuring the openbox/lxde-pi-rc.xml. However this Hotkey then only works under X11 GUI. What I would need is a hotkey that works even if the system is not booted into the GUI but running a different Program.

I tried a number of ways (most of which I don't remember anymore now) but the big problem always seemed to be that the Program I am running in the Foreground (XCSoar, a glider navigation program) seems to prevent letting keystrokes be passed "through" it to the system in the background.
I imagine the same Problem would occur with most other Programs.

Does anyone know if it is possible to assign a Key (Preferably F12) to always run a python script no matter what other Program might be running at that time?

Boooing
  • 23
  • 4

1 Answers1

1

esekeyd is an alternative to using keyboard shortcuts in Openbox, which will work even if X.Org isn't running. As noted in the Debian repositories:

ESE Key Daemon is a multimedia keyboard daemon for Linux. With the 2.6 kernel series it can also handle remote controls, as they are presented as keyboards. No kernel patch is required. It is a userspace program that pools /dev/input/event? interfaces for incoming keyboard key presses.

It is rather poorly documented, unfortunately, but I gave it a try and it does indeed work well.

It can be installed with:

sudo apt-get install esekeyd

You will then need to figure out which device file is your keyboard. There are several ways to do this, as explained on Stack Overflow, but I found the brute force method to be the best. Run:

sudo keytest /dev/input/event0 

... and press a few keys. If you press your left Alt, and the program prints LEFTALT, good. If it just prints a strange character combination, or nothing at all, press Ctrl+C, change event0 to event1 and repeat. Keep going (replace event1 with event2 and so forth, if that doesn't work) until you find the input that does work. Once you do find it, remember which device file you need.

Then run:

sudo learnkeys /etc/esekeyd.conf /dev/input/eventX

... and press F12.

Now, esekeyd will create the configuration file you need. You then need to edit it with your editor of choice (mine's nano, so I'd run sudo nano /etc/esekeyd.conf).

You'll see a line like this:

#F12(press):

Delete the #, and add your desired command directly after the : (no spaces!). Your new line might look something like this:

F12(press):/home/user/test_script.sh

Then, save (it's Ctrl+O then Enter for nano).

After that, you simply need to run esekeyd with the configuration file you've just created:

sudo esekeyd /etc/esekeyd.conf /dev/input/eventX

It won't automatically start at boot, so you'll need to set that up yourself, if you need it. It's up to you how, and any of those methods should work just fine.

Aurora0001
  • 6,308
  • 3
  • 23
  • 38
  • Hello and thank you very much for your help and the thorough description! esekeyd seems to be exactly what I was looking for. However I couldn't get it to work yet..

    The learnkeys worked and it added F12 to the esekeyd.conf file.
    Then I edited the exekeyd.conf and removed hastag and added the command so the line now says:
    F12(press):python /home/pi/test.py
    Then I rebooted and tried it out and it only shows the ~ in the command-line when I press F12. Also tried it with other buttons and commands..
    Any idea what could still be wrong?

    – Boooing Jan 08 '18 at 08:41
  • Ahh it kind of works now.. but I have weird issue - will report back once I figure out whats happening :) – Boooing Jan 08 '18 at 08:56
  • Ok so I edited the file as such:
    F10(press):reboot
    F11(press):xcsoar
    F12(press):python /home/pi/test4.py

    reboot and running xcsoar work but F12 does nothing but display a ~
    Any ideas?

    – Boooing Jan 08 '18 at 09:09
  • @Boooing Puzzling; since F10 and F11 work, it makes me think that esekeyd is fine and that's not the problem. A couple of things to test: 1. Does running python /home/pi/test4.py work normally in a command prompt? 2. Does running a different command for F12(press) work (e.g. run xcsoar on F12 press). If running a different command works, then the issue is probably your script. If it doesn't, then the issue is something to do with esekeyd's F12 binding. If that's the case, running your Python script on F10 or F11 might work. – Aurora0001 Jan 08 '18 at 16:12
  • Thank you for your suggestions! I guess it was a script thing because I got it to work now (with the actual script "camera.py" which displays 5 seconds of webcam feed).
    My new problem is: From the Terminal I press F12 and it runs camera.py just fine. However when I start XCSoar and then press F12 nothing happens - instead it only displays the 5-second webcam feed after I closed XCSoar. For example I press F12 three times while XCSoar is running and nothing happens. But once I close XCSoar the 5-second webcam feed pops up three times.. Any idea what could cause this?
    – Boooing Jan 09 '18 at 08:56
  • Edit: IT WORKS NOW!! :D I dont know why.. dont think I changed anything..weird.. THANK YOU!!! – Boooing Jan 09 '18 at 09:37