From eef3e80d598bb3a705f52165ba6d010dae7f37de Mon Sep 17 00:00:00 2001 From: winstxnhdw Date: Thu, 4 Jul 2024 00:00:12 +0100 Subject: [PATCH] feat/docs: remove `keyboard.write` - This is removed because it is very reliant on the current selected keyboard layout. For example, writing `@` may work fine on the US keyboard layout but not on the UK keyboard layout --- README.md | 10 - keywin/keyboard/__init__.py | 18 -- keywin/keyboard/codes/__init__.py | 289 ------------------- keywin/keyboard/exceptions/__init__.py | 6 - keywin/keyboard/utils/__init__.py | 3 - keywin/keyboard/utils/convert_to_key_code.py | 38 --- tests/test_keywin.py | 19 +- 7 files changed, 1 insertion(+), 382 deletions(-) delete mode 100644 keywin/keyboard/codes/__init__.py delete mode 100644 keywin/keyboard/exceptions/__init__.py delete mode 100644 keywin/keyboard/utils/__init__.py delete mode 100644 keywin/keyboard/utils/convert_to_key_code.py diff --git a/README.md b/README.md index 0a43720..ccd474c 100644 --- a/README.md +++ b/README.md @@ -47,16 +47,6 @@ keyboard.press(0x0D) keyboard.press(0x5B, 0x44) ``` -#### String Inputs - -`KeyWin` can also convert strings to key codes, albeit at a slight performance cost. - -```python -from keywin import keyboard - -keyboard.write('Hello World!') -``` - ### Mouse Similar to the keyboard, `KeyWin` provides a low-level API for mouse inputs based on Microsoft's [MOUSEINPUT](https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput) structure. diff --git a/keywin/keyboard/__init__.py b/keywin/keyboard/__init__.py index 2b84d37..5d1236e 100644 --- a/keywin/keyboard/__init__.py +++ b/keywin/keyboard/__init__.py @@ -1,4 +1,3 @@ -from keywin.keyboard.utils import convert_to_key_code from keywin.send_input import press_keyboard @@ -17,20 +16,3 @@ def press(*keys: int) -> bool: success (bool) : the success of the event """ return press_keyboard(keys) - - -def write(string: str) -> list[bool]: - """ - Summary - ------- - high-level function to type a string - - Parameters - ---------- - string (str) : string to type - - Returns - ------- - successes (list[bool]) : the success of the event(s) - """ - return [press(*key_codes) for key_codes in convert_to_key_code(string)] diff --git a/keywin/keyboard/codes/__init__.py b/keywin/keyboard/codes/__init__.py deleted file mode 100644 index d3833ed..0000000 --- a/keywin/keyboard/codes/__init__.py +++ /dev/null @@ -1,289 +0,0 @@ -class KeyCodes: - """ - Summary - ------- - pre-mapped key codes based on Microsoft's [Virtual-Key Codes](https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes) - """ - - VK_LBUTTON = 0x01 - VK_RBUTTON = 0x02 - VK_CANCEL = 0x03 - VK_MBUTTON = 0x04 - VK_XBUTTON1 = 0x05 - VK_XBUTTON2 = 0x06 - VK_BACK = 0x08 - VK_TAB = 0x09 - VK_CLEAR = 0x0C - VK_RETURN = 0x0D - VK_SHIFT = 0x10 - VK_CONTROL = 0x11 - VK_MENU = 0x12 - VK_PAUSE = 0x13 - VK_CAPITAL = 0x14 - VK_KANA = 0x15 - VK_HANGUL = 0x15 - VK_JUNJA = 0x17 - VK_FINAL = 0x18 - VK_HANJA = 0x19 - VK_KANJI = 0x19 - VK_ESCAPE = 0x1B - VK_CONVERT = 0x1C - VK_NONCONVERT = 0x1D - VK_ACCEPT = 0x1E - VK_MODECHANGE = 0x1F - VK_SPACE = 0x20 - VK_PRIOR = 0x21 - VK_NEXT = 0x22 - VK_END = 0x23 - VK_HOME = 0x24 - VK_LEFT = 0x25 - VK_UP = 0x26 - VK_RIGHT = 0x27 - VK_DOWN = 0x28 - VK_SELECT = 0x29 - VK_PRINT = 0x2A - VK_EXECUTE = 0x2B - VK_SNAPSHOT = 0x2C - VK_INSERT = 0x2D - VK_DELETE = 0x2E - VK_HELP = 0x2F - VK_0 = 0x30 - VK_1 = 0x31 - VK_2 = 0x32 - VK_3 = 0x33 - VK_4 = 0x34 - VK_5 = 0x35 - VK_6 = 0x36 - VK_7 = 0x37 - VK_8 = 0x38 - VK_9 = 0x39 - VK_A = 0x41 - VK_B = 0x42 - VK_C = 0x43 - VK_D = 0x44 - VK_E = 0x45 - VK_F = 0x46 - VK_G = 0x47 - VK_H = 0x48 - VK_I = 0x49 - VK_J = 0x4A - VK_K = 0x4B - VK_L = 0x4C - VK_M = 0x4D - VK_N = 0x4E - VK_O = 0x4F - VK_P = 0x50 - VK_Q = 0x51 - VK_R = 0x52 - VK_S = 0x53 - VK_T = 0x54 - VK_U = 0x55 - VK_V = 0x56 - VK_W = 0x57 - VK_X = 0x58 - VK_Y = 0x59 - VK_Z = 0x5A - VK_LWIN = 0x5B - VK_RWIN = 0x5C - VK_APPS = 0x5D - VK_SLEEP = 0x5F - VK_NUMPAD0 = 0x60 - VK_NUMPAD1 = 0x61 - VK_NUMPAD2 = 0x62 - VK_NUMPAD3 = 0x63 - VK_NUMPAD4 = 0x64 - VK_NUMPAD5 = 0x65 - VK_NUMPAD6 = 0x66 - VK_NUMPAD7 = 0x67 - VK_NUMPAD8 = 0x68 - VK_NUMPAD9 = 0x69 - VK_MULTIPLY = 0x6A - VK_ADD = 0x6B - VK_SEPARATOR = 0x6C - VK_SUBTRACT = 0x6D - VK_DECIMAL = 0x6E - VK_DIVIDE = 0x6F - VK_F1 = 0x70 - VK_F2 = 0x71 - VK_F3 = 0x72 - VK_F4 = 0x73 - VK_F5 = 0x74 - VK_F6 = 0x75 - VK_F7 = 0x76 - VK_F8 = 0x77 - VK_F9 = 0x78 - VK_F10 = 0x79 - VK_F11 = 0x7A - VK_F12 = 0x7B - VK_F13 = 0x7C - VK_F14 = 0x7D - VK_F15 = 0x7 - VK_F16 = 0x7F - VK_F17 = 0x80 - VK_F18 = 0x81 - VK_F19 = 0x82 - VK_F20 = 0x83 - VK_F21 = 0x84 - VK_F22 = 0x85 - VK_F23 = 0x86 - VK_F24 = 0x87 - VK_NUMLOCK = 0x90 - VK_SCROLL = 0x91 - VK_LSHIFT = 0xA0 - VK_RSHIFT = 0xA1 - VK_LCONTROL = 0xA2 - VK_RCONTROL = 0xA3 - VK_LMENU = 0xA4 - VK_RMENU = 0xA5 - VK_BROWSER_BACK = 0xA6 - VK_BROWSER_FORWARD = 0xA7 - VK_BROWSER_REFRESH = 0xA8 - VK_BROWSER_STOP = 0xA9 - VK_BROWSER_SEARCH = 0xAA - VK_BROWSER_FAVORITES = 0xAB - VK_BROWSER_HOME = 0xAC - VK_VOLUME_MUTE = 0xAD - VK_VOLUME_DOWN = 0xAE - VK_VOLUME_UP = 0xAF - VK_MEDIA_NEXT_TRACK = 0xB0 - VK_MEDIA_PREV_TRACK = 0xB1 - VK_MEDIA_STOP = 0xB2 - VK_MEDIA_PLAY_PAUSE = 0xB3 - VK_LAUNCH_MAIL = 0xB4 - VK_LAUNCH_MEDIA_SELECT = 0xB5 - VK_LAUNCH_APP1 = 0xB6 - VK_LAUNCH_APP2 = 0xB7 - VK_OEM_1 = 0xBA - VK_OEM_PLUS = 0xBB - VK_OEM_COMMA = 0xBC - VK_OEM_MINUS = 0xBD - VK_OEM_PERIOD = 0xBE - VK_OEM_2 = 0xBF - VK_OEM_3 = 0xC0 - VK_OEM_4 = 0xDB - VK_OEM_5 = 0xDC - VK_OEM_6 = 0xDD - VK_OEM_7 = 0xDE - VK_OEM_8 = 0xDF - VK_OEM_102 = 0xE2 - VK_PROCESSKEY = 0xE5 - VK_PACKET = 0xE7 - VK_ATTN = 0xF6 - VK_CRSEL = 0xF7 - VK_EXSEL = 0xF8 - VK_EREOF = 0xF9 - VK_PLAY = 0xFA - VK_ZOOM = 0xFB - VK_NONAME = 0xFC - VK_PA1 = 0xFD - VK_OEM_CLEAR = 0xFE - - -class Typables: - """ - Summary - ------- - pre-mapped typable characters based on Microsoft's [Virtual-Key Codes](https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes) - """ - - table = { - "a": [KeyCodes.VK_A], - "b": [KeyCodes.VK_B], - "c": [KeyCodes.VK_C], - "d": [KeyCodes.VK_D], - "e": [KeyCodes.VK_E], - "f": [KeyCodes.VK_F], - "g": [KeyCodes.VK_G], - "h": [KeyCodes.VK_H], - "i": [KeyCodes.VK_I], - "j": [KeyCodes.VK_J], - "k": [KeyCodes.VK_K], - "l": [KeyCodes.VK_L], - "m": [KeyCodes.VK_M], - "n": [KeyCodes.VK_N], - "o": [KeyCodes.VK_O], - "p": [KeyCodes.VK_P], - "q": [KeyCodes.VK_Q], - "r": [KeyCodes.VK_R], - "s": [KeyCodes.VK_S], - "t": [KeyCodes.VK_T], - "u": [KeyCodes.VK_U], - "v": [KeyCodes.VK_V], - "w": [KeyCodes.VK_W], - "x": [KeyCodes.VK_X], - "y": [KeyCodes.VK_Y], - "z": [KeyCodes.VK_Z], - "A": [KeyCodes.VK_SHIFT, KeyCodes.VK_A], - "B": [KeyCodes.VK_SHIFT, KeyCodes.VK_B], - "C": [KeyCodes.VK_SHIFT, KeyCodes.VK_C], - "D": [KeyCodes.VK_SHIFT, KeyCodes.VK_D], - "E": [KeyCodes.VK_SHIFT, KeyCodes.VK_E], - "F": [KeyCodes.VK_SHIFT, KeyCodes.VK_F], - "G": [KeyCodes.VK_SHIFT, KeyCodes.VK_G], - "H": [KeyCodes.VK_SHIFT, KeyCodes.VK_H], - "I": [KeyCodes.VK_SHIFT, KeyCodes.VK_I], - "J": [KeyCodes.VK_SHIFT, KeyCodes.VK_J], - "K": [KeyCodes.VK_SHIFT, KeyCodes.VK_K], - "L": [KeyCodes.VK_SHIFT, KeyCodes.VK_L], - "M": [KeyCodes.VK_SHIFT, KeyCodes.VK_M], - "N": [KeyCodes.VK_SHIFT, KeyCodes.VK_N], - "O": [KeyCodes.VK_SHIFT, KeyCodes.VK_O], - "P": [KeyCodes.VK_SHIFT, KeyCodes.VK_P], - "Q": [KeyCodes.VK_SHIFT, KeyCodes.VK_Q], - "R": [KeyCodes.VK_SHIFT, KeyCodes.VK_R], - "S": [KeyCodes.VK_SHIFT, KeyCodes.VK_S], - "T": [KeyCodes.VK_SHIFT, KeyCodes.VK_T], - "U": [KeyCodes.VK_SHIFT, KeyCodes.VK_U], - "V": [KeyCodes.VK_SHIFT, KeyCodes.VK_V], - "W": [KeyCodes.VK_SHIFT, KeyCodes.VK_W], - "X": [KeyCodes.VK_SHIFT, KeyCodes.VK_X], - "Y": [KeyCodes.VK_SHIFT, KeyCodes.VK_Y], - "Z": [KeyCodes.VK_SHIFT, KeyCodes.VK_Z], - "0": [KeyCodes.VK_0], - "1": [KeyCodes.VK_1], - "2": [KeyCodes.VK_2], - "3": [KeyCodes.VK_3], - "4": [KeyCodes.VK_4], - "5": [KeyCodes.VK_5], - "6": [KeyCodes.VK_6], - "7": [KeyCodes.VK_7], - "8": [KeyCodes.VK_8], - "9": [KeyCodes.VK_9], - " ": [KeyCodes.VK_SPACE], - ")": [KeyCodes.VK_SHIFT, KeyCodes.VK_0], - "!": [KeyCodes.VK_SHIFT, KeyCodes.VK_1], - "@": [KeyCodes.VK_SHIFT, KeyCodes.VK_2], - "#": [KeyCodes.VK_SHIFT, KeyCodes.VK_3], - "$": [KeyCodes.VK_SHIFT, KeyCodes.VK_4], - "%": [KeyCodes.VK_SHIFT, KeyCodes.VK_5], - "^": [KeyCodes.VK_SHIFT, KeyCodes.VK_6], - "&": [KeyCodes.VK_SHIFT, KeyCodes.VK_7], - "*": [KeyCodes.VK_SHIFT, KeyCodes.VK_8], - "(": [KeyCodes.VK_SHIFT, KeyCodes.VK_9], - "-": [KeyCodes.VK_OEM_MINUS], - "=": [KeyCodes.VK_OEM_PLUS], - ",": [KeyCodes.VK_OEM_COMMA], - ".": [KeyCodes.VK_OEM_PERIOD], - ";": [KeyCodes.VK_OEM_1], - "/": [KeyCodes.VK_OEM_2], - "`": [KeyCodes.VK_OEM_3], - "[": [KeyCodes.VK_OEM_4], - "\\": [KeyCodes.VK_OEM_5], - "]": [KeyCodes.VK_OEM_6], - "'": [KeyCodes.VK_OEM_7], - "_": [KeyCodes.VK_SHIFT, KeyCodes.VK_OEM_MINUS], - "+": [KeyCodes.VK_SHIFT, KeyCodes.VK_OEM_PLUS], - "<": [KeyCodes.VK_SHIFT, KeyCodes.VK_OEM_COMMA], - ">": [KeyCodes.VK_SHIFT, KeyCodes.VK_OEM_PERIOD], - ":": [KeyCodes.VK_SHIFT, KeyCodes.VK_OEM_1], - "?": [KeyCodes.VK_SHIFT, KeyCodes.VK_OEM_2], - "~": [KeyCodes.VK_SHIFT, KeyCodes.VK_OEM_3], - "{": [KeyCodes.VK_SHIFT, KeyCodes.VK_OEM_4], - "|": [KeyCodes.VK_SHIFT, KeyCodes.VK_OEM_5], - "}": [KeyCodes.VK_SHIFT, KeyCodes.VK_OEM_6], - '"': [KeyCodes.VK_SHIFT, KeyCodes.VK_OEM_7], - "\n": [KeyCodes.VK_RETURN], - "\r": [KeyCodes.VK_RETURN], - "\t": [KeyCodes.VK_TAB], - "\b": [KeyCodes.VK_BACK], - } diff --git a/keywin/keyboard/exceptions/__init__.py b/keywin/keyboard/exceptions/__init__.py deleted file mode 100644 index 7835841..0000000 --- a/keywin/keyboard/exceptions/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -class UnknownTypableException(Exception): - """ - Summary - ------- - raised when a typable is not found in the typable list - """ diff --git a/keywin/keyboard/utils/__init__.py b/keywin/keyboard/utils/__init__.py deleted file mode 100644 index f962405..0000000 --- a/keywin/keyboard/utils/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from keywin.keyboard.utils.convert_to_key_code import ( - convert_to_key_code as convert_to_key_code, -) diff --git a/keywin/keyboard/utils/convert_to_key_code.py b/keywin/keyboard/utils/convert_to_key_code.py deleted file mode 100644 index 8d1e200..0000000 --- a/keywin/keyboard/utils/convert_to_key_code.py +++ /dev/null @@ -1,38 +0,0 @@ -from functools import lru_cache -from itertools import chain, groupby - -from keywin.keyboard.codes import Typables -from keywin.keyboard.exceptions import UnknownTypableException - - -@lru_cache(maxsize=None) -def convert_to_key_code(string: str) -> list[list[int]]: - """ - Summary - ------- - convert a string to key code(s) - - Parameters - ---------- - string (str) : string to convert - - Returns - ------- - key_codes (list[list[int]]) : converted key code(s) - """ - try: - key_codes: list[list[int]] = [] - - for multi_key, group in groupby( - (Typables.table[character] for character in string), lambda codes: len(codes) > 1 - ): - if multi_key: - key_codes.extend(group) - - else: - key_codes.append(list(chain.from_iterable(group))) - - return key_codes - - except KeyError as error: - raise UnknownTypableException from error diff --git a/tests/test_keywin.py b/tests/test_keywin.py index 410993c..f1a45e3 100644 --- a/tests/test_keywin.py +++ b/tests/test_keywin.py @@ -1,7 +1,5 @@ from keywin import KeyCodes from keywin.keyboard import press -from keywin.keyboard.codes import Typables -from keywin.keyboard.utils import convert_to_key_code from keywin.mouse import ( left_click, left_press, @@ -25,28 +23,13 @@ ) -def test_convert_to_key_code(): - """ - Summary - ------- - test the `convert_to_key_code` function - """ - assert convert_to_key_code("a") == [[KeyCodes.VK_A]] - assert convert_to_key_code("A") == [[KeyCodes.VK_SHIFT, KeyCodes.VK_A]] - assert convert_to_key_code("Hello!") == [ - [KeyCodes.VK_SHIFT, KeyCodes.VK_H], - [KeyCodes.VK_E, KeyCodes.VK_L, KeyCodes.VK_L, KeyCodes.VK_O], - [KeyCodes.VK_SHIFT, KeyCodes.VK_1], - ] - - def test_press(): """ Summary ------- test the `press` function """ - assert all(press(*key_codes) for key_codes in Typables.table.values()) + assert all(press(key) for key in KeyCodes.__dict__.values()) def test_mouse_helpers():