1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#!/bin/env python
# switch k780 logitech keyboard Fn keys
import sys
import time
import usb.core
import atexit
DEVINDEX=2 # index of the device in unifying receiver
# 0xff for the receiver
class HIDpp:
def __init__(self, req_type, request, value, index):
self.__rt = req_type
self.__r = request
self.__v = value
self.__i = index
self.dev = None
atexit.register(self.clean)
def clean(self):
self.dev.reset()
if self.reattach:
self.dev.attach_kernel_driver(self.__i)
def set_device(self, device):
if self.dev is not None:
self.clean()
self.dev = device
cfg = dev[0]
self.iface = cfg[(self.__i,0)]
self.reattach = self.dev.is_kernel_driver_active(self.__i)
if self.reattach:
dev.detach_kernel_driver(self.__i)
def send_short(self, subid, *args):
"""
subid: 0-7f = reports & notifications // 80-ff = registers
"""
report_id = 0x10 # short, 0x11 = long
data = [report_id, DEVINDEX, subid] + list(args)
self.dev.ctrl_transfer(self.__rt, self.__r, self.__v, self.__i, data)
def read(self):
return self.iface[0].read(1000, 10000)
activate = len(sys.argv) == 1
# find unifying receiver
dev = usb.core.find(idVendor=0x046d, idProduct=0xc52b)
if dev is None:
raise ValueError('Keyboard not found')
kbd = HIDpp(0x21, 0x9, 0x0210, 2)
kbd.set_device(dev)
if activate:
kbd.send_short(0x0c, 0x1e, 0, 0, 0)
else:
kbd.send_short(0x0c, 0x1e, 1, 0, 0)
kbd.read()
time.sleep(1)
|