summaryrefslogtreecommitdiffstats
path: root/tools/site_compare/drivers/win32/keyboard.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/site_compare/drivers/win32/keyboard.py')
-rw-r--r--tools/site_compare/drivers/win32/keyboard.py66
1 files changed, 33 insertions, 33 deletions
diff --git a/tools/site_compare/drivers/win32/keyboard.py b/tools/site_compare/drivers/win32/keyboard.py
index a25df5e..246e14c 100644
--- a/tools/site_compare/drivers/win32/keyboard.py
+++ b/tools/site_compare/drivers/win32/keyboard.py
@@ -25,45 +25,45 @@ import win32con # Windows constants
def PressKey(down, key):
"""Presses or unpresses a key.
-
+
Uses keybd_event to simulate either depressing or releasing
a key
-
+
Args:
down: Whether the key is to be pressed or released
key: Virtual key code of key to press or release
"""
-
- # keybd_event injects key events at a very low level (it's the
+
+ # keybd_event injects key events at a very low level (it's the
# Windows API keyboard device drivers call) so this is a very
# reliable way of simulating user input
win32api.keybd_event(key, 0, (not down) * win32con.KEYEVENTF_KEYUP)
-
-
+
+
def TypeKey(key, keystroke_time=0):
"""Simulate a keypress of a virtual key.
-
+
Args:
key: which key to press
keystroke_time: length of time (in seconds) to "hold down" the key
Note that zero works just fine
-
+
Returns:
None
"""
-
+
# This just wraps a pair of PressKey calls with an intervening delay
PressKey(True, key)
time.sleep(keystroke_time)
PressKey(False, key)
-
+
def TypeString(string_to_type,
use_modifiers=False,
keystroke_time=0,
time_between_keystrokes=0):
"""Simulate typing a string on the keyboard.
-
+
Args:
string_to_type: the string to print
use_modifiers: specifies whether the following modifier characters
@@ -79,27 +79,27 @@ def TypeString(string_to_type,
nonprintable keys (F-keys, ESC, arrow keys, etc),
support for explicit control of left vs. right ALT or SHIFT,
support for Windows key
-
+
keystroke_time: length of time (in secondes) to "hold down" the key
time_between_keystrokes: length of time (seconds) to pause between keys
-
+
Returns:
None
"""
-
+
shift_held = win32api.GetAsyncKeyState(win32con.VK_SHIFT ) < 0
ctrl_held = win32api.GetAsyncKeyState(win32con.VK_CONTROL) < 0
alt_held = win32api.GetAsyncKeyState(win32con.VK_MENU ) < 0
-
+
next_escaped = False
escape_chars = {
'a': '\a', 'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t', 'v': '\v'}
-
+
for char in string_to_type:
vk = None
handled = False
-
- # Check to see if this is the start or end of a modified block (that is,
+
+ # Check to see if this is the start or end of a modified block (that is,
# {abc} for ALT-modified keys or [abc] for CTRL-modified keys
if use_modifiers and not next_escaped:
handled = True
@@ -117,17 +117,17 @@ def TypeString(string_to_type,
PressKey(False, win32con.VK_CONTROL)
else:
handled = False
-
+
# If this is an explicitly-escaped character, replace it with the
# appropriate code
if next_escaped and char in escape_chars: char = escape_chars[char]
-
+
# If this is \p, pause for one second.
if next_escaped and char == 'p':
time.sleep(1)
next_escaped = False
handled = True
-
+
# If this is \(d), press F key
if next_escaped and char.isdigit():
fkey = int(char)
@@ -139,28 +139,28 @@ def TypeString(string_to_type,
if not next_escaped and char == "\\":
next_escaped = True
handled = True
-
+
# If we make it here, it's not a special character, or it's an
# escaped special character which should be treated as a literal
if not handled:
next_escaped = False
if not vk: vk = win32api.VkKeyScan(char)
-
+
# VkKeyScan() returns the scan code in the low byte. The upper
# byte specifies modifiers necessary to produce the given character
# from the given scan code. The only one we're concerned with at the
- # moment is Shift. Determine the shift state and compare it to the
+ # moment is Shift. Determine the shift state and compare it to the
# current state... if it differs, press or release the shift key.
new_shift_held = bool(vk & (1<<8))
-
+
if new_shift_held != shift_held:
PressKey(new_shift_held, win32con.VK_SHIFT)
shift_held = new_shift_held
-
+
# Type the key with the specified length, then wait the specified delay
TypeKey(vk & 0xFF, keystroke_time)
time.sleep(time_between_keystrokes)
-
+
# Release the modifier keys, if held
if shift_held: PressKey(False, win32con.VK_SHIFT)
if ctrl_held: PressKey(False, win32con.VK_CONTROL)
@@ -168,18 +168,18 @@ def TypeString(string_to_type,
if __name__ == "__main__":
# We're being invoked rather than imported. Let's do some tests
-
+
# Press command-R to bring up the Run dialog
PressKey(True, win32con.VK_LWIN)
TypeKey(ord('R'))
PressKey(False, win32con.VK_LWIN)
-
+
# Wait a sec to make sure it comes up
time.sleep(1)
-
+
# Invoke Notepad through the Run dialog
TypeString("wordpad\n")
-
+
# Wait another sec, then start typing
time.sleep(1)
TypeString("This is a test of SiteCompare's Keyboard.py module.\n\n")
@@ -194,5 +194,5 @@ if __name__ == "__main__":
use_modifiers=True,
keystroke_time=0.05,
time_between_keystrokes=0.05)
-
-
+
+