-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_keyboard.py
executable file
·75 lines (57 loc) · 2.34 KB
/
detect_keyboard.py
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
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
Detect which keyboard to use, and saves in keyboard.cfg
Copyright (C) 2016 Thomaz de Oliveira dos Reis <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from __future__ import print_function
import evdev
import sys
import argparse
import os
from time import sleep
devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
open_devices = [evdev.InputDevice(device.fn) for device in devices]
def clean_up():
print('Please wait... Do NOT press any key right now, cleaning up inputs...')
sleep(1)
for num, device in enumerate(open_devices):
max_count = 5
while device.read_one():
max_count -= 1
if not max_count:
print('Device', open_devices[num], 'seems too busy, removing!')
del open_devices[num]
break
print('Thanks.')
def detect_key():
print('Now, press any key on the correct keyboard')
while True:
for num, device in enumerate(open_devices):
if device.read_one():
return device
def parse_args(args):
keyboardcfg = os.path.join(os.path.dirname(__file__), 'keyboard.cfg')
parser = argparse.ArgumentParser(description='Detect keyboard device.')
parser.add_argument('-o', '--file-output', dest='keyboard_config_file', type=str, default=keyboardcfg,
help='Keyboard Config File (Default: keyboard.cfg)')
return parser.parse_args(args)
def main():
args = parse_args(sys.argv[1:])
clean_up()
device = detect_key()
with open(args.keyboard_config_file, 'w') as outfile:
outfile.write(device.fn + '\n')
print('Done, correct device is:', device)
if __name__ == '__main__':
main()