/usr/lib/python2.7/dist-packages/dogtail/rawinput.py is in python-dogtail 0.9.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | # -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
from dogtail.config import config
from dogtail.utils import doDelay
from dogtail.logging import debugLogger as logger
from pyatspi import Registry as registry
from pyatspi import (KEY_SYM, KEY_PRESS, KEY_PRESSRELEASE, KEY_RELEASE)
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk
"""
Handles raw input using AT-SPI event generation.
Note: Think of keyvals as keysyms, and keynames as keystrings.
"""
__author__ = """
David Malcolm <dmalcolm@redhat.com>,
Zack Cerza <zcerza@redhat.com>
"""
def doTypingDelay():
doDelay(config.typingDelay)
def checkCoordinates(x, y):
if x < 0 or y < 0:
raise ValueError("Attempting to generate a mouse event at negative coordinates: (%s,%s)" % (x, y))
def click(x, y, button=1, check=True):
"""
Synthesize a mouse button click at (x,y)
"""
if check:
checkCoordinates(x, y)
logger.log("Mouse button %s click at (%s,%s)" % (button, x, y))
registry.generateMouseEvent(x, y, name='b%sc' % button)
doDelay(config.actionDelay)
def doubleClick(x, y, button=1, check=True):
"""
Synthesize a mouse button double-click at (x,y)
"""
if check:
checkCoordinates(x, y)
logger.log("Mouse button %s doubleclick at (%s,%s)" % (button, x, y))
registry.generateMouseEvent(x, y, name='b%sd' % button)
doDelay()
def press(x, y, button=1, check=True):
"""
Synthesize a mouse button press at (x,y)
"""
if check:
checkCoordinates(x, y)
logger.log("Mouse button %s press at (%s,%s)" % (button, x, y))
registry.generateMouseEvent(x, y, name='b%sp' % button)
doDelay()
def release(x, y, button=1, check=True):
"""
Synthesize a mouse button release at (x,y)
"""
if check:
checkCoordinates(x, y)
logger.log("Mouse button %s release at (%s,%s)" % (button, x, y))
registry.generateMouseEvent(x, y, name='b%sr' % button)
doDelay()
def absoluteMotion(x, y, mouseDelay=None, check=True):
"""
Synthesize mouse absolute motion to (x,y)
"""
if check:
checkCoordinates(x, y)
logger.log("Mouse absolute motion to (%s,%s)" % (x, y))
registry.generateMouseEvent(x, y, name='abs')
if mouseDelay:
doDelay(mouseDelay)
else:
doDelay()
def absoluteMotionWithTrajectory(source_x, source_y, dest_x, dest_y, mouseDelay=None, check=True):
"""
Synthetize mouse absolute motion with trajectory. The 'trajectory' means that the whole motion
is divided into several atomic movements which are synthetized separately.
"""
if check:
checkCoordinates(source_x, source_y)
checkCoordinates(dest_x, dest_y)
logger.log("Mouse absolute motion with trajectory to (%s,%s)" % (dest_x, dest_y))
dx = float(dest_x - source_x)
dy = float(dest_y - source_y)
max_len = max(abs(dx), abs(dy))
if max_len == 0:
# actually, no motion requested
return
dx /= max_len
dy /= max_len
act_x = float(source_x)
act_y = float(source_y)
for _ in range(0, int(max_len)):
act_x += dx
act_y += dy
if mouseDelay:
doDelay(mouseDelay)
registry.generateMouseEvent(int(act_x), int(act_y), name='abs')
if mouseDelay:
doDelay(mouseDelay)
else:
doDelay()
def relativeMotion(x, y, mouseDelay=None):
"""
Synthetize a relative motion from actual position.
Note: Does not check if the end coordinates are positive.
"""
logger.log("Mouse relative motion of (%s,%s)" % (x, y))
registry.generateMouseEvent(x, y, name='rel')
if mouseDelay:
doDelay(mouseDelay)
else:
doDelay()
def drag(fromXY, toXY, button=1, check=True):
"""
Synthesize a mouse press, drag, and release on the screen.
"""
logger.log("Mouse button %s drag from %s to %s" % (button, fromXY, toXY))
(x, y) = fromXY
press(x, y, button, check)
(x, y) = toXY
absoluteMotion(x, y, check=check)
doDelay()
release(x, y, button, check)
doDelay()
def dragWithTrajectory(fromXY, toXY, button=1, check=True):
"""
Synthetize a mouse press, drag (including move events), and release on the screen
"""
logger.log("Mouse button %s drag with trajectory from %s to %s" % (button, fromXY, toXY))
(x, y) = fromXY
press(x, y, button, check)
(x, y) = toXY
absoluteMotionWithTrajectory(fromXY[0], fromXY[1], x, y, check=check)
doDelay()
release(x, y, button, check)
doDelay()
def typeText(string):
"""
Types the specified string, one character at a time.
Please note, you may have to set a higher typing delay,
if your machine misses/switches the characters typed.
Needed sometimes on slow setups/VMs typing non-ASCII utf8 chars.
"""
for char in string:
pressKey(char)
keyNameAliases = {
'enter': 'Return',
'esc': 'Escape',
'alt': 'Alt_L',
'control': 'Control_L',
'ctrl': 'Control_L',
'shift': 'Shift_L',
'del': 'Delete',
'ins': 'Insert',
'pageup': 'Page_Up',
'pagedown': 'Page_Down',
' ': 'space',
'\t': 'Tab',
'\n': 'Return'
}
def uniCharToKeySym(uniChar):
i = ord(uniChar)
keySym = Gdk.unicode_to_keyval(i)
return keySym
def keyNameToKeySym(keyName):
keyName = keyNameAliases.get(keyName.lower(), keyName)
keySym = Gdk.keyval_from_name(keyName)
# various error 'codes' returned for non-recognized chars in versions of GTK3.X
if keySym == 0xffffff or keySym == 0x0 or keySym is None:
try:
keySym = uniCharToKeySym(keyName)
except: # not even valid utf-8 char
try: # Last attempt run at a keyName ('Meta_L', 'Dash' ...)
keySym = getattr(Gdk, 'KEY_' + keyName)
except AttributeError:
raise KeyError(keyName)
return keySym
def keyNameToKeyCode(keyName):
"""
Use GDK to get the keycode for a given keystring.
Note that the keycode returned by this function is often incorrect when
the requested keystring is obtained by holding down the Shift key.
Generally you should use uniCharToKeySym() and should only need this
function for nonprintable keys anyway.
"""
keymap = Gdk.Keymap.get_for_display(Gdk.Display.get_default())
entries = keymap.get_entries_for_keyval(
Gdk.keyval_from_name(keyName))
try:
return entries[1][0].keycode
except TypeError:
pass
def pressKey(keyName):
"""
Presses (and releases) the key specified by keyName.
keyName is the English name of the key as seen on the keyboard. Ex: 'enter'
Names are looked up in Gdk.KEY_ If they are not found there, they are
looked up by uniCharToKeySym().
"""
keySym = keyNameToKeySym(keyName)
registry.generateKeyboardEvent(keySym, None, KEY_SYM)
doTypingDelay()
def keyCombo(comboString):
"""
Generates the appropriate keyboard events to simulate a user pressing the
specified key combination.
comboString is the representation of the key combo to be generated.
e.g. '<Control><Alt>p' or '<Control><Shift>PageUp' or '<Control>q'
"""
strings = []
for s in comboString.split('<'):
if s:
for S in s.split('>'):
if S:
S = keyNameAliases.get(S.lower(), S)
strings.append(S)
for s in strings:
if not hasattr(Gdk, s):
if not hasattr(Gdk, 'KEY_' + s):
raise ValueError("Cannot find key %s" % s)
modifiers = strings[:-1]
finalKey = strings[-1]
for modifier in modifiers:
code = keyNameToKeyCode(modifier)
registry.generateKeyboardEvent(code, None, KEY_PRESS)
code = keyNameToKeyCode(finalKey)
registry.generateKeyboardEvent(code, None, KEY_PRESSRELEASE)
for modifier in modifiers:
code = keyNameToKeyCode(modifier)
registry.generateKeyboardEvent(code, None, KEY_RELEASE)
doDelay()
|