Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename keyboardMapping to KEYBOARD_MAPPING #800

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyautogui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ def isValidKey(key):
Returns:
bool: True if key is a valid value, False if not.
"""
return platformModule.keyboardMapping.get(key, None) is not None
return platformModule.KEYBOARD_MAPPING.get(key, None) is not None


@_genericPyAutoGUIChecks
Expand Down
16 changes: 8 additions & 8 deletions pyautogui/_pyautogui_osx.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
keyUp(), or press() into the code used for the OS-specific keyboard function.

They should always be lowercase, and the same keys should be used across all OSes."""
keyboardMapping = dict([(key, None) for key in pyautogui.KEY_NAMES])
keyboardMapping.update({
KEYBOARD_MAPPING = dict([(key, None) for key in pyautogui.KEY_NAMES])
KEYBOARD_MAPPING.update({
'a': 0x00, # kVK_ANSI_A
's': 0x01, # kVK_ANSI_S
'd': 0x02, # kVK_ANSI_D
Expand Down Expand Up @@ -185,7 +185,7 @@

# add mappings for uppercase letters
for c in 'abcdefghijklmnopqrstuvwxyz':
keyboardMapping[c.upper()] = keyboardMapping[c]
KEYBOARD_MAPPING[c.upper()] = KEYBOARD_MAPPING[c]

# Taken from ev_keymap.h
# http://www.opensource.apple.com/source/IOHIDFamily/IOHIDFamily-86.1/IOHIDSystem/IOKit/hidsystem/ev_keymap.h
Expand Down Expand Up @@ -217,7 +217,7 @@
}

def _keyDown(key):
if key not in keyboardMapping or keyboardMapping[key] is None:
if key not in KEYBOARD_MAPPING or KEYBOARD_MAPPING[key] is None:
return

if key in special_key_translate_table:
Expand All @@ -226,7 +226,7 @@ def _keyDown(key):
_normalKeyEvent(key, 'down')

def _keyUp(key):
if key not in keyboardMapping or keyboardMapping[key] is None:
if key not in KEYBOARD_MAPPING or KEYBOARD_MAPPING[key] is None:
return

if key in special_key_translate_table:
Expand All @@ -240,16 +240,16 @@ def _normalKeyEvent(key, upDown):

try:
if pyautogui.isShiftCharacter(key):
key_code = keyboardMapping[key.lower()]
key_code = KEYBOARD_MAPPING[key.lower()]

event = Quartz.CGEventCreateKeyboardEvent(None,
keyboardMapping['shift'], upDown == 'down')
KEYBOARD_MAPPING['shift'], upDown == 'down')
Quartz.CGEventPost(Quartz.kCGHIDEventTap, event)
# Tiny sleep to let OS X catch up on us pressing shift
time.sleep(pyautogui.DARWIN_CATCH_UP_TIME)

else:
key_code = keyboardMapping[key]
key_code = KEYBOARD_MAPPING[key]

event = Quartz.CGEventCreateKeyboardEvent(None, key_code, upDown == 'down')
Quartz.CGEventPost(Quartz.kCGHIDEventTap, event)
Expand Down
30 changes: 15 additions & 15 deletions pyautogui/_pyautogui_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ class _I(ctypes.Union):
keyUp(), or press() into the code used for the OS-specific keyboard function.

They should always be lowercase, and the same keys should be used across all OSes."""
keyboardMapping = dict([(key, None) for key in pyautogui.KEY_NAMES])
keyboardMapping.update({
KEYBOARD_MAPPING = dict([(key, None) for key in pyautogui.KEY_NAMES])
KEYBOARD_MAPPING.update({
'backspace': 0x08, # VK_BACK
'\b': 0x08, # VK_BACK
'super': 0x5B, #VK_LWIN
Expand Down Expand Up @@ -244,7 +244,7 @@ class _I(ctypes.Union):
# Populate the basic printable ascii characters.
# https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-vkkeyscana
for c in range(32, 128):
keyboardMapping[chr(c)] = ctypes.windll.user32.VkKeyScanA(ctypes.wintypes.WCHAR(chr(c)))
KEYBOARD_MAPPING[chr(c)] = ctypes.windll.user32.VkKeyScanA(ctypes.wintypes.WCHAR(chr(c)))


def _keyDown(key):
Expand All @@ -261,25 +261,25 @@ def _keyDown(key):
Returns:
None
"""
if key not in keyboardMapping or keyboardMapping[key] is None:
if key not in KEYBOARD_MAPPING or KEYBOARD_MAPPING[key] is None:
return

needsShift = pyautogui.isShiftCharacter(key)

"""
# OLD CODE: The new code relies on having all keys be loaded in keyboardMapping from the start.
if key in keyboardMapping.keys():
vkCode = keyboardMapping[key]
# OLD CODE: The new code relies on having all keys be loaded in KEYBOARD_MAPPING from the start.
if key in KEYBOARD_MAPPING.keys():
vkCode = KEYBOARD_MAPPING[key]
elif len(key) == 1:
# note: I could use this case to update keyboardMapping to cache the VkKeyScan results, but I've decided not to just to make any possible bugs easier to reproduce.
# note: I could use this case to update KEYBOARD_MAPPING to cache the VkKeyScan results, but I've decided not to just to make any possible bugs easier to reproduce.
vkCode = ctypes.windll.user32.VkKeyScanW(ctypes.wintypes.WCHAR(key))
if vkCode == -1:
raise ValueError('There is no VK code for key "%s"' % (key))
if vkCode > 0x100: # the vk code will be > 0x100 if it needs shift
vkCode -= 0x100
needsShift = True
"""
mods, vkCode = divmod(keyboardMapping[key], 0x100)
mods, vkCode = divmod(KEYBOARD_MAPPING[key], 0x100)

for apply_mod, vk_mod in [(mods & 4, 0x12), (mods & 2, 0x11),
(mods & 1 or needsShift, 0x10)]: #HANKAKU not supported! mods & 8
Expand All @@ -302,24 +302,24 @@ def _keyUp(key):
Returns:
None
"""
if key not in keyboardMapping or keyboardMapping[key] is None:
if key not in KEYBOARD_MAPPING or KEYBOARD_MAPPING[key] is None:
return

needsShift = pyautogui.isShiftCharacter(key)
"""
# OLD CODE: The new code relies on having all keys be loaded in keyboardMapping from the start.
if key in keyboardMapping.keys():
vkCode = keyboardMapping[key]
# OLD CODE: The new code relies on having all keys be loaded in KEYBOARD_MAPPING from the start.
if key in KEYBOARD_MAPPING.keys():
vkCode = KEYBOARD_MAPPING[key]
elif len(key) == 1:
# note: I could use this case to update keyboardMapping to cache the VkKeyScan results, but I've decided not to just to make any possible bugs easier to reproduce.
# note: I could use this case to update KEYBOARD_MAPPING to cache the VkKeyScan results, but I've decided not to just to make any possible bugs easier to reproduce.
vkCode = ctypes.windll.user32.VkKeyScanW(ctypes.wintypes.WCHAR(key))
if vkCode == -1:
raise ValueError('There is no VK code for key "%s"' % (key))
if vkCode > 0x100: # the vk code will be > 0x100 if it needs shift
vkCode -= 0x100
needsShift = True
"""
mods, vkCode = divmod(keyboardMapping[key], 0x100)
mods, vkCode = divmod(KEYBOARD_MAPPING[key], 0x100)

for apply_mod, vk_mod in [(mods & 4, 0x12), (mods & 2, 0x11),
(mods & 1 or needsShift, 0x10)]: #HANKAKU not supported! mods & 8
Expand Down
20 changes: 10 additions & 10 deletions pyautogui/_pyautogui_x11.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _keyDown(key):
Returns:
None
"""
if key not in keyboardMapping or keyboardMapping[key] is None:
if key not in KEYBOARD_MAPPING or KEYBOARD_MAPPING[key] is None:
return

if type(key) == int:
Expand All @@ -142,12 +142,12 @@ def _keyDown(key):

needsShift = pyautogui.isShiftCharacter(key)
if needsShift:
fake_input(_display, X.KeyPress, keyboardMapping['shift'])
fake_input(_display, X.KeyPress, KEYBOARD_MAPPING['shift'])

fake_input(_display, X.KeyPress, keyboardMapping[key])
fake_input(_display, X.KeyPress, KEYBOARD_MAPPING[key])

if needsShift:
fake_input(_display, X.KeyRelease, keyboardMapping['shift'])
fake_input(_display, X.KeyRelease, KEYBOARD_MAPPING['shift'])
_display.sync()


Expand All @@ -166,13 +166,13 @@ def _keyUp(key):
Release a given character key. Also works with character keycodes as
integers, but not keysyms.
"""
if key not in keyboardMapping or keyboardMapping[key] is None:
if key not in KEYBOARD_MAPPING or KEYBOARD_MAPPING[key] is None:
return

if type(key) == int:
keycode = key
else:
keycode = keyboardMapping[key]
keycode = KEYBOARD_MAPPING[key]

fake_input(_display, X.KeyRelease, keycode)
_display.sync()
Expand All @@ -182,14 +182,14 @@ def _keyUp(key):
_display = Display(os.environ['DISPLAY'])


""" Information for keyboardMapping derived from PyKeyboard's special_key_assignment() function.
""" Information for KEYBOARD_MAPPING derived from PyKeyboard's special_key_assignment() function.

The *KB dictionaries in pyautogui map a string that can be passed to keyDown(),
keyUp(), or press() into the code used for the OS-specific keyboard function.

They should always be lowercase, and the same keys should be used across all OSes."""
keyboardMapping = dict([(key, None) for key in pyautogui.KEY_NAMES])
keyboardMapping.update({
KEYBOARD_MAPPING = dict([(key, None) for key in pyautogui.KEY_NAMES])
KEYBOARD_MAPPING.update({
'backspace': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('BackSpace')),
'\b': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('BackSpace')),
'tab': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Tab')),
Expand Down Expand Up @@ -318,4 +318,4 @@ def _keyUp(key):

# Trading memory for time" populate winKB so we don't have to call VkKeyScanA each time.
for c in """abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890""":
keyboardMapping[c] = _display.keysym_to_keycode(Xlib.XK.string_to_keysym(c))
KEYBOARD_MAPPING[c] = _display.keysym_to_keycode(Xlib.XK.string_to_keysym(c))