Multimedia Keys in Python (Linux)

I want to determine when the XF86Launch1 key is XF86Launch1 on my keyboard using Python.

I have a headless server with a bluetooth keyboard. I would like to run a command line program whenever a specific multimedia key is pressed.

At the moment I am using:

 import sys import tty, termios def getch(): fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(fd) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch print getch() 

But he will not detect multimedia keys. When I click on them, nothing is printed.

Is there a way to detect these keys in a Ubuntu headless field - or is there a better way to run a program when a key is pressed?

+8
python keyboard-shortcuts keyboard key-bindings
source share
4 answers

Instead of reading stdin tty, you can use the linux api input device to read them.

In linux, all input devices are displayed as a file under /dev/input (for example, /dev/input/event0 ), and when one of the read()s from these files, structured data is returned that indicates when the keys are pressed and released.

In C, the libevdev library provides a wrapper around them. In python, you can use the python-evdev library . It should also be possible to read directly from the input device (although you may need to carefully read the kernel documentation and source in order to process them correctly).

+2
source share

I think your problem is that the multimedia keys are not displayed on the terminal input.

Perhaps you could make progress by running xev to catch the key and xmodmap to map the key to another input.

Alternatively, use something like TKinter and see if the graphics program collects keystrokes.

 from Tkinter import * root = Tk() def key(event): print "pressed", repr(event.char) def callback(event): frame.focus_set() frame = Frame(root, width=100, height=100) frame.bind("<Key>", key) frame.bind("<Button-1>", callback) frame.pack() root.mainloop() 

Another possibility is to map the key F instead of the multimedia key. (i.e. F9)


Edit: Further research on this led to the following two links:

Additional keyboard keys

Additional keyboard keys in the console

The console itself does not support multimedia keys. But it does support user keys F. The F30-F246 is always free. Instead of displaying XF86Launch1, go to F70. Then map the F70 to the keyboard input in your layout, or use the Python script you wrote to handle it.

+1
source share

pycopia may be an option. I use it with the this button and it seems to work pretty well. I'm still working on getting it connected to the button again when the button falls asleep and then returns. Here is the part of the script I'm using:
keyboard.py:

 from pycopia.OS.Linux import Input from pycopia.OS.Linux import event class Satechi(Input.EventDevice): DEVNAME = 'Satechi' def register_callback(self, cb): self._callback = cb def poll(self): while 1: ev = self.read() if ev.evtype == event.EV_KEY: self._callback(ev) read_handler = poll 

button.py

 from keyboard import Satechi def callback(event): pass #Do something fun if __name__ == '__main__': pm = Satechi() pm.find() pm.register_callback(callback) while 1: try: pm.poll() except OSError: pm = Satechi() while True: try: pm.find() pm.register_callback(callback) break except IOError: pass pm.close() 

Where DEVNAME is the device name in /proc/bus/input/devices .
You can print event in the callback to find out what code and value for the button you are looking for

+1
source share

Try reading xinput test <id> stdout in a loop and catch the events you need.

Here is an example in Bash:

  #! / bin / bash

 keyboard_id = 9 # use xinput to find your keyboard id

 xinput test $ keyboard_id |  while read line;  do
     case $ line in
         "key press 44") echo -e "\ n == j pressed ==" ;;
         "key press 45") echo -e "\ n == k pressed ==" ;;
     esac
 done
-2
source share

All Articles