pyautogui 文檔(三):鍵盤控制
阿新 • • 發佈:2019-04-29
ret inter 列表 latest color tab launch 模擬 auto
press(),keyDown()和keyUp()函數
typewrite()函數
主要的鍵盤功能是typewrite()
。此函數將鍵入字符串中傳遞的字符。要在按下每個字符鍵之間添加延遲間隔,請為interval
關鍵字參數傳遞int float 。
>>> pyautogui.typewrite(‘Hello world!‘) >>> pyautogui.typewrite(‘Hello world!‘, interval=0.25)
您只能按下單字符鍵typewrite()
,因此您無法按Shift或F1鍵。
press(),keyDown()和keyUp()函數
要按下這些鍵,調用press()
函數,從它傳遞一個字符串pyautogui.KEYBOARD_KEYS
,例如enter
,esc
,f1
。
>>> pyautogui.press(‘enter‘)
該press()
功能實際上只是一個包裝keyDown()
和keyUp()
功能,模擬按下一個鍵按下,然後釋放它。這些功能可以自己調用。例如,要在按住Shift鍵的同時按下左箭頭鍵三次,請調用以下內容:
>>> pyautogui.keyDown(‘shift‘) # hold down the shift key >>> pyautogui.press(‘left‘) # press the left arrow key >>> pyautogui.press(‘left‘) # press the left arrow key >>> pyautogui.press(‘left‘) # press the left arrow key >>> pyautogui.keyUp(‘shift‘) # release the shift key
要按下類似於多個鍵的鍵typewrite()
,請將字符串列表傳遞給press()
。例如:
>>> pyautogui.press([‘left‘, ‘left‘, ‘left‘])
hotkey()函數
為了使按鍵熱鍵或鍵盤快捷鍵方便,hotkey()
可以傳遞幾個按鍵順序按下的鍵,然後以相反的順序釋放。這段代碼:
>>> pyautogui.hotkey(‘ctrl‘, ‘shift‘, ‘esc‘)
......相當於這段代碼:
>>> pyautogui.keyDown(‘ctrl‘) >>> pyautogui.keyDown(‘shift‘) >>> pyautogui.keyDown(‘esc‘) >>> pyautogui.keyUp(‘esc‘) >>> pyautogui.keyUp(‘shift‘) >>> pyautogui.keyUp(‘ctrl‘)
KEYBOARD_KEYS
通過對以下是有效字符串press()
,keyDown()
,keyUp()
,和hotkey()
功能:
[‘\t‘, ‘\n‘, ‘\r‘, ‘ ‘, ‘!‘, ‘"‘, ‘#‘, ‘$‘, ‘%‘, ‘&‘, "‘", ‘(‘, ‘)‘, ‘*‘, ‘+‘, ‘,‘, ‘-‘, ‘.‘, ‘/‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘:‘, ‘;‘, ‘<‘, ‘=‘, ‘>‘, ‘?‘, ‘@‘, ‘[‘, ‘\\‘, ‘]‘, ‘^‘, ‘_‘, ‘`‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘,‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘{‘, ‘|‘, ‘}‘, ‘~‘, ‘accept‘, ‘add‘, ‘alt‘, ‘altleft‘, ‘altright‘, ‘apps‘, ‘backspace‘, ‘browserback‘, ‘browserfavorites‘, ‘browserforward‘, ‘browserhome‘, ‘browserrefresh‘, ‘browsersearch‘, ‘browserstop‘, ‘capslock‘, ‘clear‘, ‘convert‘, ‘ctrl‘, ‘ctrlleft‘, ‘ctrlright‘, ‘decimal‘, ‘del‘, ‘delete‘, ‘divide‘, ‘down‘, ‘end‘, ‘enter‘, ‘esc‘, ‘escape‘, ‘execute‘, ‘f1‘, ‘f10‘, ‘f11‘, ‘f12‘, ‘f13‘, ‘f14‘, ‘f15‘, ‘f16‘, ‘f17‘, ‘f18‘, ‘f19‘, ‘f2‘, ‘f20‘, ‘f21‘, ‘f22‘, ‘f23‘, ‘f24‘, ‘f3‘, ‘f4‘, ‘f5‘, ‘f6‘, ‘f7‘, ‘f8‘, ‘f9‘, ‘final‘, ‘fn‘, ‘hanguel‘, ‘hangul‘, ‘hanja‘, ‘help‘, ‘home‘, ‘insert‘, ‘junja‘, ‘kana‘, ‘kanji‘, ‘launchapp1‘, ‘launchapp2‘, ‘launchmail‘, ‘launchmediaselect‘, ‘left‘, ‘modechange‘, ‘multiply‘, ‘nexttrack‘, ‘nonconvert‘, ‘num0‘, ‘num1‘, ‘num2‘, ‘num3‘, ‘num4‘, ‘num5‘, ‘num6‘, ‘num7‘, ‘num8‘, ‘num9‘, ‘numlock‘, ‘pagedown‘, ‘pageup‘, ‘pause‘, ‘pgdn‘, ‘pgup‘, ‘playpause‘, ‘prevtrack‘, ‘print‘, ‘printscreen‘, ‘prntscrn‘, ‘prtsc‘, ‘prtscr‘, ‘return‘, ‘right‘, ‘scrolllock‘, ‘select‘, ‘separator‘, ‘shift‘, ‘shiftleft‘, ‘shiftright‘, ‘sleep‘, ‘space‘, ‘stop‘, ‘subtract‘, ‘tab‘, ‘up‘, ‘volumedown‘, ‘volumemute‘, ‘volumeup‘, ‘win‘, ‘winleft‘, ‘winright‘, ‘yen‘, ‘command‘, ‘option‘, ‘optionleft‘, ‘optionright‘]
資料:https://pyautogui.readthedocs.io/en/latest/keyboard.html
pyautogui 文檔(三):鍵盤控制