Coverage Report - orca.input_event

ModuleCoverage %
orca.input_event
71%
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 2437 2007-05-31 12:41:26Z wwalker $"
27 1
__version__   = "$Revision: 2437 $"
28 1
__date__      = "$Date: 2007-05-31 08:41:26 -0400 (Thu, 31 May 2007) $"
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 3579
        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 3579
        event_string = event.event_string
73 3579
        if (event.modifiers & (1 << atspi.Accessibility.MODIFIER_CONTROL)) \
74 213
            and (not event.is_text) and (len(event_string) == 1):
75 102
            value = ord(event.event_string[0])
76 102
            if value < 32:
77 102
                event_string = chr(value + 0x40)
78
79 3579
        InputEvent.__init__(self, KEYBOARD_EVENT)
80 3579
        self.type = event.type
81 3579
        self.hw_code = event.hw_code
82 3579
        self.modifiers = event.modifiers
83 3579
        self.event_string = event_string
84 3579
        self.is_text = event.is_text
85 3579
        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 0
        InputEvent.__init__(self, MOUSE_BUTTON_EVENT)
104 0
        self.x = event.detail1
105 0
        self.y = event.detail2
106 0
        self.pressed = event.type.endswith('p')
107 0
        self.button = event.type[len("mouse:button:"):-1]
108 0
        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 4266
        self._function = function
146 4266
        self._description = description
147 4266
        self._learnModeEnabled = learnModeEnabled
148
149 1
    def __eq__(self, other):
150
        """Compares one input handler to another."""
151 0
        return (self._function == other._function)
152
153 1
    def processInputEvent(self, script, inputEvent):
154
        """Processes an input event.  If settings.learnModeEnabled is True,
155
        this will merely report the description of the input event to braille
156
        and speech.  If settings.learnModeEnabled is False, this will call the
157
        function bound to this InputEventHandler instance, passing the
158
        inputEvent as the sole argument to the function.
159
160
        This function is expected to return True if it consumes the
161
        event; otherwise it is expected to return False.
162
163
        Arguments:
164
        - script:     the script (if any) associated with this event
165
        - inputEvent: the input event to pass to the function bound
166
                      to this InputEventHandler instance.
167
        """
168
169 97
        consumed = False
170
171 97
        if settings.learnModeEnabled and self._learnModeEnabled:
172 0
            if self._description:
173
                # These imports are here to eliminate circular imports.
174
                #
175 0
                import braille
176 0
                import speech
177 0
                braille.displayMessage(self._description)
178 0
                speech.speak(self._description)
179 0
                consumed = True
180
        else:
181 97
            try:
182 97
                consumed = self._function(script, inputEvent)
183 0
            except:
184 0
                debug.printException(debug.LEVEL_SEVERE)
185
186 97
        return consumed