Package virtual_keyboard ::
Module virtual_keyboard
|
|
1
2
3 import subprocess
4 from subprocess import PIPE
5 import re
6
7
8
10
12 super(VirtualKeyboard,self).__init__();
13
14
15
16
17
18 self.screenPosPattern = re.compile(r'x:(?P<x>[\d]+).*y:(?P<y>[\d]+)');
19
20 self.windowIDPattern = re.compile(r'.*window:(?P<winID>[\d]+)');
21
22
23
24
25
26
27 self.winGeometryPattern = re.compile('.*Position: (?P<x>[\d]+)[,]+(?P<y>[\d]+).*Geometry: (?P<width>[\d]+)x(?P<height>[\d]+)', re.DOTALL);
28
29
30 self.windowIDs = {};
31
32 self.saveActiveWindowID('__startup_win_id__');
33
35 '''
36 Type a given keystroke. Examples being "alt+r", "Control_L+J",
37 "ctrl+alt+n", "BackSpace", "Linefeed".
38 Generally, any valid X Keysym string will work. Composited control keys are
39 separated by '+'. Aliases exist for "alt", "ctrl", "shift", "super",
40 and "meta" which all map to Foo_L, such as Alt_L and Control_L, etc.
41 @param theStr: control-char string to send to active window
42 @type theStr: string
43 '''
44 resCode = subprocess.call(['xdotool', 'key', str(theStr)]);
45
46 - def typeTextToActiveWindow(self, theStr):
47 '''
48 Type a given string, which must not contain control chars.
49 For sending individual control chars, use typeControlCharToActiveWindow().
50 @param theStr: control-char string to send to active window
51 @type theStr: string
52 '''
53 resCode = subprocess.call(['xdotool', 'type', str(theStr)]);
54
56 '''
57 Buttons map this way: Left mouse is 1, middle is 2, right is 3,
58 wheel up is 4, wheel down is 5.
59 @param buttonNum: which mouse button to click
60 @type buttonNum: int
61 '''
62 if buttonNum < 1 or buttonNum > 5:
63 raise ValueError("Mouse buttons are 1-3; mouse wheel up is 4; mouse wheel down is 5. Called with %d" % buttonNum)
64 resCode = subprocess.call(['xdotool', 'getactivewindow', 'click', str(buttonNum)]);
65
67 '''
68 Buttons map this way: Left mouse is 1, middle is 2, right is 3,
69 wheel up is 4, wheel down is 5.
70 @param buttonNum: which mouse button to hold down
71 @type buttonNum: int
72 '''
73 if buttonNum < 1 or buttonNum > 5:
74 raise ValueError("Mouse buttons are 1-3; mouse wheel up is 4; mouse wheel down is 5. Called with %d" % buttonNum)
75 resCode = subprocess.call(['xdotool', 'getactivewindow', 'mousedown', str(buttonNum)]);
76
78 '''
79 Buttons map this way: Left mouse is 1, middle is 2, right is 3,
80 wheel up is 4, wheel down is 5.
81 @param buttonNum: which mouse button to hold down
82 @type buttonNum: int
83 '''
84 if buttonNum < 1 or buttonNum > 5:
85 raise ValueError("Mouse buttons are 1-3; mouse wheel up is 4; mouse wheel down is 5. Called with %d" % buttonNum)
86 proc = subprocess.Popen(['xdotool', 'getactivewindow', 'mouseup', str(buttonNum)]);
87
89 '''
90 Return a dictionary that provides 'x' and 'y' keys,
91 whose values are integer x and y coordinates of the
92 mouse cursor. The position is relative to the upper left
93 corner of the display.
94 '''
95 proc = subprocess.Popen(['xdotool', 'getmouselocation'], stdout=PIPE, stderr=file("/dev/null", 'w'));
96
97 (stdoutData, stdinData) = proc.communicate();
98
99 xdotoolOutputMatch = re.match(self.screenPosPattern, stdoutData);
100 if xdotoolOutputMatch is None:
101 raise ValueError("Call to xdotool for obtaining mouse cursor position did not return output of expected format. It returned '%s'" % stdoutData)
102
103 resDict = xdotoolOutputMatch.groupdict();
104
105 resDict['x'] = int(resDict['x']);
106 resDict['y'] = int(resDict['y']);
107 return resDict
108
110 '''
111 Move mouse cursor to absolute position relative to upper left
112 corner of display.
113 @param x: horizontal coordinate
114 @type x: int
115 @param y: vertical coordinate
116 @type y: int
117 '''
118 resCode = subprocess.call(['xdotool', 'mousemove', '--sync', str(x), str(y)]);
119
121 '''
122 Move mouse cursor new position relative to where
123 it is currently located. It is legal to use negative
124 offsets for x and/or y.
125 @param x: horizontal coordinate
126 @type x: int
127 @param y: vertical coordinate
128 @type y: int
129 '''
130 resCode = subprocess.call(['xdotool', 'mousemove_relative', '--sync', '--', str(x), str(y)]);
131
133 '''
134 Internally saves the currently active X11 window's ID.
135 Use getRecentWindow() to retrieve the ID for use with
136 later xdotool commands.
137 @param retrievalKey: key under which caller will ask for the ID later on.
138 @type retrievalKey: string
139 '''
140
141 winid = subprocess.check_output(['xdotool', 'getactivewindow']).strip();
142
143 self.windowIDs[retrievalKey] = subprocess.check_output(['xdotool', 'getactivewindow']).strip();
144
147
149 '''
150 Activates the X11 window with the given window ID.
151 If windowID is omitted, the most recently active
152 window is activated (see getRecentWindow()).
153 @param retrievalKey: key under which caller asked to associate with window in earlier call to saveActiveWindowID
154 @type retrievalKey: string
155 '''
156 if ((retrievalKey is None) and (windowTitle is None)) or\
157 ((retrievalKey is not None) and (windowTitle is not None)):
158 raise ValueError('Either retrievalKey or windowTitle must be provided (but not both)');
159
160 if retrievalKey is not None:
161
162 winKey = str(self._getWinIDSafely_(retrievalKey));
163
164 resCode = subprocess.call(['xdotool', 'windowactivate', '--sync', str(self._getWinIDSafely_(retrievalKey))]);
165 else:
166 resCode = subprocess.call(['xdotool', 'search', '--name', '--sync', windowTitle, 'windowactivate']);
167
169 if retrievalKey is None:
170 geo = subprocess.check_output(['xdotool', 'getactivewindow', 'getwindowgeometry']);
171 else:
172 geo = subprocess.check_output(['xdotool', 'getwindowgeometry', str(self._getWinIDSafely_(retrievalKey))]);
173 theMatch = self.winGeometryPattern.match(geo);
174 if theMatch is None:
175 return None;
176 resDict = theMatch.groupdict();
177 return resDict;
178
180 '''
181 Flakey; don't rely on it.
182 @param retrievalKey:
183 @type retrievalKey:
184 '''
185 if retrievalKey is None:
186 rescode = subprocess.call(['xdotool', 'getactivewindow', 'windowraise']);
187 else:
188 rescode = subprocess.call(['xdotool', 'windowraise', str(self._getWinIDSafely_(retrievalKey))]);
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
210 try:
211 return self.windowIDs[retrievalKey];
212 except KeyError as e:
213 raise ValueError("No retrieval key %s is known to virtual keyboard." % `e`);
214
215
216 if __name__ == '__main__':
217
218 import time
219 vBoard = VirtualKeyboard()
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251 vBoard.windowIDs['test'] = str(46137569);
252 vBoard.raiseWindow(retrievalKey='test')
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267