Simulate typing keys on keyboard. text: str, keys to type. interval: float, seconds between keys. waitTime: float. charMode: bool, if False, the text typied is depend on the input method if a input method is on. debug: bool, if True, print the keys. Examples: {Ctrl},
(text: str, interval: float = 0.01, waitTime: float = OPERATION_WAIT_TIME, charMode: bool = True, debug: bool = False)
| 2642 | |
| 2643 | |
| 2644 | def SendKeys(text: str, interval: float = 0.01, waitTime: float = OPERATION_WAIT_TIME, charMode: bool = True, debug: bool = False) -> None: |
| 2645 | """ |
| 2646 | Simulate typing keys on keyboard. |
| 2647 | text: str, keys to type. |
| 2648 | interval: float, seconds between keys. |
| 2649 | waitTime: float. |
| 2650 | charMode: bool, if False, the text typied is depend on the input method if a input method is on. |
| 2651 | debug: bool, if True, print the keys. |
| 2652 | Examples: |
| 2653 | {Ctrl}, {Delete} ... are special keys' name in SpecialKeyNames. |
| 2654 | SendKeys('{Ctrl}a{Delete}{Ctrl}v{Ctrl}s{Ctrl}{Shift}s{Win}e{PageDown}') #press Ctrl+a, Delete, Ctrl+v, Ctrl+s, Ctrl+Shift+s, Win+e, PageDown |
| 2655 | SendKeys('{Ctrl}(AB)({Shift}(123))') #press Ctrl+A+B, type (, press Shift+1+2+3, type ), if () follows a hold key, hold key won't release util ) |
| 2656 | SendKeys('{Ctrl}{a 3}') #press Ctrl+a at the same time, release Ctrl+a, then type a 2 times |
| 2657 | SendKeys('{a 3}{B 5}') #type a 3 times, type B 5 times |
| 2658 | SendKeys('{{}Hello{}}abc {a}{b}{c} test{} 3}{!}{a} (){(}{)}') #type: {Hello}abc abc test}}}!a ()() |
| 2659 | SendKeys('0123456789{Enter}') |
| 2660 | SendKeys('ABCDEFGHIJKLMNOPQRSTUVWXYZ{Enter}') |
| 2661 | SendKeys('abcdefghijklmnopqrstuvwxyz{Enter}') |
| 2662 | SendKeys('`~!@#$%^&*()-_=+{Enter}') |
| 2663 | SendKeys('[]{{}{}}\\|;:\'\",<.>/?{Enter}') |
| 2664 | """ |
| 2665 | holdKeys = ('WIN', 'LWIN', 'RWIN', 'SHIFT', 'LSHIFT', 'RSHIFT', 'CTRL', 'CONTROL', 'LCTRL', 'RCTRL', 'LCONTROL', 'LCONTROL', 'ALT', 'LALT', 'RALT') |
| 2666 | keys = [] |
| 2667 | printKeys = [] |
| 2668 | i = 0 |
| 2669 | insertIndex = 0 |
| 2670 | length = len(text) |
| 2671 | hold = False |
| 2672 | include = False |
| 2673 | lastKeyValue = None |
| 2674 | while True: |
| 2675 | if text[i] == '{': |
| 2676 | rindex = text.find('}', i) |
| 2677 | if rindex == i + 1:#{}} |
| 2678 | rindex = text.find('}', i + 2) |
| 2679 | if rindex == -1: |
| 2680 | raise ValueError('"{" or "{}" is not valid, use "{{}" for "{", use "{}}" for "}"') |
| 2681 | key = text[i + 1:rindex] |
| 2682 | key = [it for it in key.split(' ') if it] |
| 2683 | if not key: |
| 2684 | raise ValueError('"{}" is not valid, use "{{Space}}" or " " for " "'.format(text[i:rindex + 1])) |
| 2685 | if (len(key) == 2 and not key[1].isdigit()) or len(key) > 2: |
| 2686 | raise ValueError('"{}" is not valid'.format(text[i:rindex + 1])) |
| 2687 | upperKey = key[0].upper() |
| 2688 | count = 1 |
| 2689 | if len(key) > 1: |
| 2690 | count = int(key[1]) |
| 2691 | for j in range(count): |
| 2692 | if hold: |
| 2693 | if upperKey in SpecialKeyNames: |
| 2694 | keyValue = SpecialKeyNames[upperKey] |
| 2695 | if type(lastKeyValue) == type(keyValue) and lastKeyValue == keyValue: |
| 2696 | insertIndex += 1 |
| 2697 | printKeys.insert(insertIndex, (key[0], 'KeyDown | ExtendedKey')) |
| 2698 | printKeys.insert(insertIndex + 1, (key[0], 'KeyUp | ExtendedKey')) |
| 2699 | keys.insert(insertIndex, (keyValue, KeyboardEventFlag.KeyDown | KeyboardEventFlag.ExtendedKey)) |
| 2700 | keys.insert(insertIndex + 1, (keyValue, KeyboardEventFlag.KeyUp | KeyboardEventFlag.ExtendedKey)) |
| 2701 | lastKeyValue = keyValue |
no test coverage detected