forked from KenT2/pipresents-gapless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pp_kbddriver.py
69 lines (53 loc) · 2.41 KB
/
pp_kbddriver.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
import os
import ConfigParser
from pp_utils import Monitor
class KbdDriver(object):
config=None
def __init__(self):
self.mon=Monitor()
# sets up tkinter keyboard events such that any key press
# does a callback to 'callback' with the event object and a symbolic name.
def bind_keys(self,widget,callback):
for option in KbdDriver.config.items('keys'):
condition=option[0]
symbolic_name=option[1]
# print condition,symbolic_name
widget.bind(condition, lambda event, name=symbolic_name: self.specific_key(callback,name))
# bind all the normal keys that return a printing character such that x produces pp-key-x
widget.bind("<Key>", lambda event : self.normal_key(callback,event))
def specific_key(self,callback,name):
callback(name,'KBD')
# alphanumeric keys- convert to symbolic by adding pp-key-
def normal_key(self,callback,event):
key=event.char
if key != '':
callback('pp-key-'+key,'KBD')
# read the key bindings from keys.cfg
def read(self,pp_dir,pp_home,pp_profile):
if KbdDriver.config is None:
# try inside profile
tryfile=pp_profile+os.sep+'pp_io_config'+os.sep+'keys.cfg'
if os.path.exists(tryfile):
self.mon.log(self,"Found keys.cfg in profile at: "+ tryfile)
filename=tryfile
else:
# try inside pipresents
tryfile=pp_dir+os.sep+'pp_config'+os.sep+"keys.cfg"
if os.path.exists(tryfile):
filename=tryfile
self.mon.log(self,"Fallback keys.cfg found at "+ tryfile)
else:
self.mon.log(self,"keys.cfg not found at "+ tryfile)
self.mon.err(self,"keys.cfg not found in profile or fallback in Pi Presents")
return False
KbdDriver.config = ConfigParser.ConfigParser()
KbdDriver.config.optionxform=str
KbdDriver.config.read(filename)
self.mon.log(self,"keys.cfg read from "+ filename)
if KbdDriver.config.has_section('keys') is False:
self.mon.err(self,"no [keys] section in keys.cfg")
return False
return True
def has_section(self,section):
if KbdDriver.config.has_section(section) is False:
return False