File tree Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Original file line number Diff line number Diff line change 9
9
- Add functionality to query device properties. See ``InputDevice.input_props ``
10
10
and the ``input_props `` argument to ``Uinput ``.
11
11
12
+ - ``KeyEvent `` received an ``allow_unknown `` constructor argument, which
13
+ determines what will happen when an event code cannot be mapped to a keycode.
14
+ The default and behavior so far has been to raise ``KeyError ``. If set to
15
+ ``True ``, the keycode will be set to the event code formatted as a hex number.
16
+
12
17
13
18
1.2.0 (Apr 7, 2019)
14
19
====================
Original file line number Diff line number Diff line change @@ -86,18 +86,29 @@ class KeyEvent(object):
86
86
87
87
__slots__ = 'scancode' , 'keycode' , 'keystate' , 'event'
88
88
89
- def __init__ (self , event ):
89
+ def __init__ (self , event , allow_unknown = False ):
90
+ '''
91
+ The ``allow_unknown`` argument determines what to do in the event of a event code
92
+ for which a key code cannot be found. If ``False`` a ``KeyError`` will be raised.
93
+ If ``True`` the keycode will be set to the hex value of the event code.
94
+ '''
95
+
96
+ self .scancode = event .code
97
+
90
98
if event .value == 0 :
91
99
self .keystate = KeyEvent .key_up
92
100
elif event .value == 2 :
93
101
self .keystate = KeyEvent .key_hold
94
102
elif event .value == 1 :
95
103
self .keystate = KeyEvent .key_down
96
104
97
- if not (event .code in keys ):
98
- keys [event .code ] = '' .join ('{:02X}' .format (event .code ))
99
- self .keycode = keys [event .code ]
100
- self .scancode = event .code
105
+ try :
106
+ self .keycode = keys [event .code ]
107
+ except KeyError :
108
+ if allow_unknown :
109
+ self .keycode = '0x{:02X}' .format (event .code )
110
+ else :
111
+ raise
101
112
102
113
#: Reference to an :class:`InputEvent` instance.
103
114
self .event = event
You can’t perform that action at this time.
0 commit comments