1 |
|
# Orca |
2 |
|
# |
3 |
|
# Copyright 2005-2006 Sun Microsystems Inc. |
4 |
|
# |
5 |
|
# This library is free software; you can redistribute it and/or |
6 |
|
# modify it under the terms of the GNU Library General Public |
7 |
|
# License as published by the Free Software Foundation; either |
8 |
|
# version 2 of the License, or (at your option) any later version. |
9 |
|
# |
10 |
|
# This library is distributed in the hope that it will be useful, |
11 |
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
|
# Library General Public License for more details. |
14 |
|
# |
15 |
|
# You should have received a copy of the GNU Library General Public |
16 |
|
# License along with this library; if not, write to the |
17 |
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 |
|
# Boston, MA 02111-1307, USA. |
19 |
|
|
20 |
|
"""Provides support for handling input events. This provides several classes |
21 |
|
to define input events (InputEvent, KeyboardEvent, BrailleEvent, |
22 |
|
MouseButtonEvent, MouseMotionEvent, and SpeechEvent), and also provides a |
23 |
|
InputEventHandler class. It is intended that instances of InputEventHandler |
24 |
1 |
will be used which should be used to handle all input events.""" |
25 |
|
|
26 |
1 |
__id__ = "$Id: input_event.py 1529 2006-10-05 16:31:04Z wwalker $" |
27 |
1 |
__version__ = "$Revision: 1529 $" |
28 |
1 |
__date__ = "$Date: 2006-10-05 09:31:04 -0700 (Thu, 05 Oct 2006) $" |
29 |
1 |
__copyright__ = "Copyright (c) 2005-2006 Sun Microsystems Inc." |
30 |
1 |
__license__ = "LGPL" |
31 |
|
|
32 |
1 |
import atspi |
33 |
1 |
import debug |
34 |
1 |
import settings |
35 |
1 |
import time |
36 |
|
|
37 |
1 |
KEYBOARD_EVENT = "keyboard" |
38 |
1 |
BRAILLE_EVENT = "braille" |
39 |
1 |
MOUSE_BUTTON_EVENT = "mouse:button" |
40 |
1 |
MOUSE_MOTION_EVENT = "mouse:motion" |
41 |
1 |
SPEECH_EVENT = "speech" |
42 |
|
|
43 |
2 |
class InputEvent: |
44 |
|
|
45 |
1 |
def __init__(self, type): |
46 |
|
"""Creates a new input event of the given type. |
47 |
|
|
48 |
|
Arguments: |
49 |
|
- type: the input event type (one of KEYBOARD_EVENT, BRAILLE_EVENT, |
50 |
|
MOUSE_BUTTON_EVENT, MOUSE_MOTION_EVENT, SPEECH_EVENT). |
51 |
|
""" |
52 |
|
|
53 |
3261 |
self.type = type |
54 |
|
|
55 |
2 |
class KeyboardEvent(InputEvent): |
56 |
|
|
57 |
1 |
def __init__(self, event): |
58 |
|
"""Creates a new InputEvent of type KEYBOARD_EVENT. |
59 |
|
|
60 |
|
Arguments: |
61 |
|
- event: the AT-SPI keyboard event |
62 |
|
""" |
63 |
|
|
64 |
|
# Control characters come through as control characters, so we |
65 |
|
# just turn them into their ASCII equivalent. NOTE that the |
66 |
|
# upper case ASCII characters will be used (e.g., ctrl+a will |
67 |
|
# be turned into the string "A"). All these checks here are |
68 |
|
# to just do some sanity checking before doing the |
69 |
|
# conversion. [[[WDW - this is making assumptions about |
70 |
|
# mapping ASCII control characters to UTF-8.]]] |
71 |
|
# |
72 |
3257 |
event_string = event.event_string |
73 |
3257 |
if (event.modifiers & (1 << atspi.Accessibility.MODIFIER_CONTROL)) \ |
74 |
|
and (not event.is_text) and (len(event_string) == 1): |
75 |
1 |
value = ord(event.event_string[0]) |
76 |
1 |
if value < 32: |
77 |
1 |
event_string = chr(value + 0x40) |
78 |
|
|
79 |
3257 |
InputEvent.__init__(self, KEYBOARD_EVENT) |
80 |
3257 |
self.type = event.type |
81 |
3257 |
self.hw_code = event.hw_code |
82 |
3257 |
self.modifiers = event.modifiers |
83 |
3257 |
self.event_string = event_string |
84 |
3257 |
self.is_text = event.is_text |
85 |
3257 |
self.time = time.time() |
86 |
|
|
87 |
2 |
class BrailleEvent(InputEvent): |
88 |
|
|
89 |
1 |
def __init__(self, event): |
90 |
|
"""Creates a new InputEvent of type BRAILLE_EVENT. |
91 |
|
|
92 |
|
Arguments: |
93 |
|
- event: the integer BrlTTY command for this event. |
94 |
|
""" |
95 |
0 |
InputEvent.__init__(self, BRAILLE_EVENT) |
96 |
0 |
self.event = event |
97 |
|
|
98 |
2 |
class MouseButtonEvent(InputEvent): |
99 |
|
|
100 |
1 |
def __init__(self, event): |
101 |
|
"""Creates a new InputEvent of type MOUSE_BUTTON_EVENT. |
102 |
|
""" |
103 |
4 |
InputEvent.__init__(self, MOUSE_BUTTON_EVENT) |
104 |
4 |
self.x = event.detail1 |
105 |
4 |
self.y = event.detail2 |
106 |
4 |
self.pressed = event.type.endswith('p') |
107 |
4 |
self.button = event.type[len("mouse:button:"):-1] |
108 |
4 |
self.time = time.time() |
109 |
|
|
110 |
2 |
class MouseMotionEvent(InputEvent): |
111 |
|
|
112 |
1 |
def __init__(self, event): |
113 |
|
"""[[[TODO: WDW - undefined at the moment.]]] |
114 |
|
""" |
115 |
0 |
InputEvent.__init__(self, MOUSE_MOTION_EVENT) |
116 |
0 |
self.event = event |
117 |
|
|
118 |
2 |
class SpeechEvent(InputEvent): |
119 |
|
|
120 |
1 |
def __init__(self, event): |
121 |
|
"""[[[TODO: WDW - undefined at the moment.]]] |
122 |
|
""" |
123 |
0 |
InputEvent.__init__(self, SPEECH_EVENT) |
124 |
0 |
self.event = event |
125 |
|
|
126 |
2 |
class InputEventHandler: |
127 |
|
|
128 |
1 |
def __init__(self, function, description, learnModeEnabled=True): |
129 |
|
"""Creates a new InputEventHandler instance. All bindings |
130 |
|
(e.g., key bindings and braille bindings) will be handled |
131 |
|
by an instance of an InputEventHandler. |
132 |
|
|
133 |
|
Arguments: |
134 |
|
- function: the function to call with an InputEvent instance as its |
135 |
|
sole argument. The function is expected to return True |
136 |
|
if it consumes the event; otherwise it should return |
137 |
|
False |
138 |
|
- description: a localized string describing what this InputEvent |
139 |
|
does |
140 |
|
- learnModeEnabled: if True, the description will be spoken and |
141 |
|
brailled if learn mode is enabled. If False, |
142 |
|
the function will be called no matter what. |
143 |
|
""" |
144 |
|
|
145 |
3031 |
self._function = function |
146 |
3031 |
self._description = description |
147 |
3031 |
self._learnModeEnabled = learnModeEnabled |
148 |
|
|
149 |
1 |
def processInputEvent(self, script, inputEvent): |
150 |
|
"""Processes an input event. If settings.learnModeEnabled is True, |
151 |
|
this will merely report the description of the input event to braille |
152 |
|
and speech. If settings.learnModeEnabled is False, this will call the |
153 |
|
function bound to this InputEventHandler instance, passing the |
154 |
|
inputEvent as the sole argument to the function. |
155 |
|
|
156 |
|
This function is expected to return True if it consumes the |
157 |
|
event; otherwise it is expected to return False. |
158 |
|
|
159 |
|
Arguments: |
160 |
|
- script: the script (if any) associated with this event |
161 |
|
- inputEvent: the input event to pass to the function bound |
162 |
|
to this InputEventHandler instance. |
163 |
|
""" |
164 |
|
|
165 |
77 |
consumed = False |
166 |
|
|
167 |
77 |
if settings.learnModeEnabled and self._learnModeEnabled: |
168 |
0 |
if self._description: |
169 |
|
# These imports are here to eliminate circular imports. |
170 |
|
# |
171 |
0 |
import braille |
172 |
0 |
import speech |
173 |
0 |
braille.displayMessage(self._description) |
174 |
0 |
speech.speak(self._description) |
175 |
0 |
consumed = True |
176 |
|
else: |
177 |
77 |
try: |
178 |
77 |
consumed = self._function(script, inputEvent) |
179 |
0 |
except: |
180 |
0 |
debug.printException(debug.LEVEL_SEVERE) |
181 |
|
|
182 |
77 |
return consumed |