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

keyDown strict mode #876

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
9 changes: 5 additions & 4 deletions pyautogui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,7 @@ def isValidKey(key):


@_genericPyAutoGUIChecks
def keyDown(key, logScreenshot=None, _pause=True):
def keyDown(key, logScreenshot=None, _pause=True, strict=False):
"""Performs a keyboard key press without the release. This will put that
key in a held down state.

Expand All @@ -1548,16 +1548,17 @@ def keyDown(key, logScreenshot=None, _pause=True):

Args:
key (str): The key to be pressed down. The valid names are listed in
KEYBOARD_KEYS.

KEYBOARD_KEYS.
strict (bool, optional): If True and the key is not a valid value,
raise a ValueError.
Returns:
None
"""
if len(key) > 1:
key = key.lower()

_logScreenshot(logScreenshot, "keyDown", key, folder=".")
platformModule._keyDown(key)
platformModule._keyDown(key, strict)


@_genericPyAutoGUIChecks
Expand Down
4 changes: 3 additions & 1 deletion pyautogui/_pyautogui_osx.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@
'KEYTYPE_ILLUMINATION_TOGGLE': 23
}

def _keyDown(key):
def _keyDown(key, strict=False):
if key not in keyboardMapping or keyboardMapping[key] is None:
if strict:
raise ValueError("Key '%s' is not a valid key." % (key))
return

if key in special_key_translate_table:
Expand Down
8 changes: 6 additions & 2 deletions pyautogui/_pyautogui_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class _I(ctypes.Union):
keyboardMapping[chr(c)] = ctypes.windll.user32.VkKeyScanA(ctypes.wintypes.WCHAR(chr(c)))


def _keyDown(key):
def _keyDown(key, strict=False):
"""Performs a keyboard key press without the release. This will put that
key in a held down state.

Expand All @@ -256,12 +256,16 @@ def _keyDown(key):

Args:
key (str): The key to be pressed down. The valid names are listed in
pyautogui.KEY_NAMES.
pyautogui.KEY_NAMES.
strict (bool): If strict is True and the key is not a valid key,
raise a ValueError.

Returns:
None
"""
if key not in keyboardMapping or keyboardMapping[key] is None:
if strict:
raise ValueError('The key "%s" is not a valid key.' % (key))
return

needsShift = pyautogui.isShiftCharacter(key)
Expand Down
5 changes: 4 additions & 1 deletion pyautogui/_pyautogui_x11.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _mouseUp(x, y, button):
_display.sync()


def _keyDown(key):
def _keyDown(key, strict=False):
"""Performs a keyboard key press without the release. This will put that
key in a held down state.

Expand All @@ -128,11 +128,14 @@ def _keyDown(key):
Args:
key (str): The key to be pressed down. The valid names are listed in
pyautogui.KEY_NAMES.
strict (bool, optional): If strict is True and the key is not recognized, will raise a ValueError.

Returns:
None
"""
if key not in keyboardMapping or keyboardMapping[key] is None:
if strict:
raise ValueError("Key "%s" is not a valid key." % key)
return

if type(key) == int:
Expand Down