Python中的多媒体键(Linux)

Ter*_*den 8 python keyboard keyboard-shortcuts key-bindings

我想XF86Launch1用Python 检测键盘上按键的时间.

我有一个带蓝牙连接键盘的无头服务器.我想在按下特定多媒体键时启动命令行程序.

目前,我正在使用:

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()
Run Code Online (Sandbox Code Playgroud)

但它不会检测多媒体键.按下它们时没有打印出来.

有没有办法在无头Ubuntu盒子上检测这些键 - 或者是在按键上启动程序的更好方法?

小智 -2

xinput test <id>尝试循环读取标准输出并捕获您需要的事件。

下面是 Bash 中的一些示例:

#!/bin/bash

Keyboard_id=9 # 使用 xinput 查找你的键盘 ID

xinput 测试 $keyboard_id | 读取行时;做
    案例 $line in
        "按键 44") echo -e "\n == j 按下 ==" ;;
        "按键 45") echo -e "\n == k 按下 ==" ;;
    埃萨克
完毕