内核升级后:缺少键盘事件

时间:2018-01-13 09:05:44

标签: linux keyboard mouse udev

内核升级(3.19到4.4)后,键盘鼠标按键的键映射部分断开。

当其他人仍然按预期工作时,中间按钮只是在按键释放后触发(然后是keydown和keyup)。

测试:

sudo cat /dev/input/event22
sudo evtest /dev/input/event22
sudo showkey

连连呢?有没有办法比“/ dev / input / event22”更深层?

1 个答案:

答案 0 :(得分:0)

最后我得到了一个有效的解决方案:

import os
import time
import pyautogui as pa

DEVPATH = "/dev/usb/hiddev0"
BTNHEX = "F200A0FF00000000" # first/unique hex value of ThinkPad middle button
NEWKEY = "ctrlleft" # mapping target

def byteToHex(byteStr):
  return "".join(["%02X" % ord(b) for b in byteStr]).strip()

def waitForPathExists(DEVPATH):
  while not os.path.exists(DEVPATH): time.sleep(0.5)

def watchHandleDev(dev, isPressed):
  byteStr = os.read(dev, 8)
  currentHex = byteToHex(byteStr[:8])
  if currentHex == BTNHEX:
    pa.keyUp(NEWKEY) if isPressed else pa.keyDown(NEWKEY)
    return not isPressed
  return isPressed

def handleDevUnavailable(dev):
  print "device '%s' not readable, waiting" % DEVPATH
  os.close(dev)
  waitForPathExists(DEVPATH)
  print "device '%s' found, reopening" % DEVPATH
  return os.open(DEVPATH, os.O_RDONLY)

def main():
  dev = os.open(DEVPATH, os.O_RDONLY)
  isPressed = False
  while True:
    try:
      isPressed = watchHandleDev(dev, isPressed)
    except OSError, err:
      print "err", err
      if err.errno == 5: dev = handleDevUnavailable(dev)

确实捕获了keydown / keypress事件!

我没有深入了解hiddev API,但识别了计数器前缀并且每行重复后缀值。由于它们在keydown和keyup之间没有区别,我在python中实现了一个toggle函数(需要对/ dev / usb / hiddev0的读取权限):

var content = $('.example').clone().find(".active").remove().end().html()

console.log(content)