1
0

10-copilot-key-override.lua 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. -- SPDX-License-Identifier: MIT
  2. --
  3. -- This is an example libinput plugin
  4. --
  5. -- This plugin detects the Copilot key on the keyboard with
  6. -- the given VID/PID and replaces it with a different key (sequence).
  7. -- UNCOMMENT THIS LINE TO ACTIVATE THE PLUGIN
  8. -- libinput:register({1})
  9. -- Replace this with your keyboard's VID/PID
  10. KEYBOARD_VID = 0x046d
  11. KEYBOARD_PID = 0x4088
  12. meta_is_down = false
  13. shift_is_down = false
  14. -- shift-A, because you can never have enough screaming
  15. replacement_sequence = { evdev.KEY_LEFTSHIFT, evdev.KEY_A }
  16. function frame(device, frame, _)
  17. for _, v in ipairs(frame) do
  18. if v.value ~= 2 then -- ignore key repeats
  19. if v.usage == evdev.KEY_LEFTMETA then
  20. meta_is_down = v.value == 1
  21. elseif v.usage == evdev.KEY_LEFTSHIFT then
  22. shift_is_down = v.value == 1
  23. elseif v.usage == evdev.KEY_F23 and meta_is_down and shift_is_down then
  24. -- We know from the MS requirements that F23 for copilot is
  25. -- either last key (on press) or the first key (on release)
  26. -- of the three-key sequence, and no other keys are
  27. -- within this frame.
  28. if v.value == 1 then
  29. -- Release our modifiers first
  30. device:prepend_frame({
  31. { usage = evdev.KEY_LEFTSHIFT, value = 0 },
  32. { usage = evdev.KEY_LEFTMETA, value = 0 },
  33. })
  34. -- Insert our replacement press sequence
  35. local replacement_frame = {}
  36. for _, rv in ipairs(replacement_sequence) do
  37. table.insert(replacement_frame, { usage = rv, value = 1 })
  38. end
  39. device:append_frame(replacement_frame)
  40. else
  41. -- Insert our replacement release sequence
  42. local replacement_frame = {}
  43. for idx = #replacement_sequence, 1, -1 do
  44. table.insert(replacement_frame, { usage = replacement_sequence[idx], value = 0 })
  45. end
  46. device:append_frame(replacement_frame)
  47. -- we don't care about re-pressing shift/meta because the
  48. -- rest of the stack will filter the release for an
  49. -- unpressed key anyway.
  50. end
  51. return {} -- discard this frame
  52. end
  53. end
  54. end
  55. end
  56. function device_new(device)
  57. local info = device:info()
  58. if info.vid == KEYBOARD_VID and info.pid == KEYBOARD_PID then
  59. device:connect("evdev-frame", frame)
  60. end
  61. end
  62. libinput:connect("new-evdev-device", device_new)