1
2 """
3 Handles raw input using AT-SPI event generation.
4
5 Note: Think of keyvals as keysyms, and keynames as keystrings.
6
7 Authors: David Malcolm <dmalcolm@redhat.com>, Zack Cerza <zcerza@redhat.com>
8 """
9
10 __author__ = """
11 David Malcolm <dmalcolm@redhat.com>,
12 Zack Cerza <zcerza@redhat.com>
13 """
14
15 import gtk.keysyms
16 import gtk.gdk
17 from config import config
18 from utils import doDelay
19 from logging import debugLogger as logger
20 from pyatspi import Registry as registry
21 from pyatspi import (KEY_SYM, KEY_PRESS, KEY_PRESSRELEASE, KEY_RELEASE)
22
25
26 -def click (x, y, button = 1):
27 """
28 Synthesize a mouse button click at (x,y)
29 """
30 logger.log("Mouse button %s click at (%s,%s)"%(button,x,y))
31 registry.generateMouseEvent(x, y, 'b%sc' % button)
32 doDelay(config.actionDelay)
33
35 """
36 Synthesize a mouse button double-click at (x,y)
37 """
38 logger.log("Mouse button %s doubleclick at (%s,%s)"%(button,x,y))
39 registry.generateMouseEvent(x,y, 'b%sd' % button)
40 doDelay()
41
42 -def press (x, y, button = 1):
43 """
44 Synthesize a mouse button press at (x,y)
45 """
46 logger.log("Mouse button %s press at (%s,%s)"%(button,x,y))
47 registry.generateMouseEvent(x,y, 'b%sp' % button)
48 doDelay()
49
51 """
52 Synthesize a mouse button release at (x,y)
53 """
54 logger.log("Mouse button %s release at (%s,%s)"%(button,x,y))
55 registry.generateMouseEvent(x,y, 'b%sr' % button)
56 doDelay()
57
59 """
60 Synthesize mouse absolute motion to (x,y)
61 """
62 logger.log("Mouse absolute motion to (%s,%s)"%(x,y))
63 registry.generateMouseEvent(x,y, 'abs')
64 doDelay()
65
67 logger.log("Mouse relative motion of (%s,%s)"%(x,y))
68 registry.generateMouseEvent(x,y, 'rel')
69 doDelay()
70
71 -def drag(fromXY, toXY, button = 1):
72 """
73 Synthesize a mouse press, drag, and release on the screen.
74 """
75 logger.log("Mouse button %s drag from %s to %s"%(button, fromXY, toXY))
76
77 (x,y) = fromXY
78 press (x, y, button)
79
80
81 (x,y) = toXY
82 absoluteMotion(x,y)
83 doDelay()
84
85 release (x, y, button)
86 doDelay()
87
88 -def typeText(string):
89 """
90 Types the specified string, one character at a time.
91 """
92 if not isinstance(string, unicode):
93 string = string.decode('utf-8')
94 for char in string:
95 pressKey(char)
96
97 keyNameAliases = {
98 'enter' : 'Return',
99 'esc' : 'Escape',
100 'alt' : 'Alt_L',
101 'control' : 'Control_L',
102 'ctrl' : 'Control_L',
103 'shift' : 'Shift_L',
104 'del' : 'Delete',
105 'ins' : 'Insert',
106 'pageup' : 'Page_Up',
107 'pagedown' : 'Page_Down',
108 ' ' : 'space',
109 '\t' : 'Tab',
110 '\n' : 'Return'
111 }
112
114 i = gtk.gdk.keyval_to_unicode(keySym)
115 if i: UniChar = unichr(i)
116 else: UniChar = ''
117 return UniChar
118
120
121 if not isinstance(uniChar, unicode): uniChar = unicode(uniChar)
122 i = ord(uniChar)
123 keySym = gtk.gdk.unicode_to_keyval(i)
124 return keySym
125
127 return gtk.gdk.keyval_name(keySym)
128
130 try:
131 keyName = keyNameAliases.get(keyName.lower(), keyName)
132 keySym = gtk.gdk.keyval_from_name(keyName)
133 if not keySym: keySym = getattr(gtk.keysyms, keyName)
134 except AttributeError:
135 try: keySym = uniCharToKeySym(keyName)
136 except TypeError: raise KeyError, keyName
137 return keySym
138
140 """
141 Use GDK to get the keycode for a given keystring.
142
143 Note that the keycode returned by this function is often incorrect when
144 the requested keystring is obtained by holding down the Shift key.
145
146 Generally you should use uniCharToKeySym() and should only need this
147 function for nonprintable keys anyway.
148 """
149 keymap = gtk.gdk.keymap_get_default()
150 entries = keymap.get_entries_for_keyval( \
151 gtk.gdk.keyval_from_name(keyName))
152 try: return entries[0][0]
153 except TypeError: pass
154
156 """
157 Presses (and releases) the key specified by keyName.
158 keyName is the English name of the key as seen on the keyboard. Ex: 'enter'
159 Names are looked up in gtk.keysyms. If they are not found there, they are
160 looked up by uniCharToKeySym().
161 """
162 keySym = keyNameToKeySym(keyName)
163 registry.generateKeyboardEvent(keySym, None, KEY_SYM)
164 doTypingDelay()
165
167 """
168 Generates the appropriate keyboard events to simulate a user pressing the
169 specified key combination.
170
171 comboString is the representation of the key combo to be generated.
172 e.g. '<Control><Alt>p' or '<Control><Shift>PageUp' or '<Control>q'
173 """
174 strings = []
175 for s in comboString.split('<'):
176 if s:
177 for S in s.split('>'):
178 if S:
179 S = keyNameAliases.get(S.lower(), S)
180 strings.append(S)
181 for s in strings:
182 if not hasattr(gtk.keysyms, s):
183 raise ValueError, "Cannot find key %s" % s
184
185 modifiers = strings[:-1]
186 finalKey = strings[-1]
187
188 for modifier in modifiers:
189 code = keyNameToKeyCode(modifier)
190 registry.generateKeyboardEvent(code, None, KEY_PRESS)
191
192 code = keyNameToKeyCode(finalKey)
193 registry.generateKeyboardEvent(code, None, KEY_PRESSRELEASE)
194
195 for modifier in modifiers:
196 code = keyNameToKeyCode(modifier)
197 registry.generateKeyboardEvent(code, None, KEY_RELEASE)
198
199 doDelay()
200