【PyAutoGUI操作指南】03 键盘控制功能+实现热键组合+ KEYBOARD_KEYS

简介: 【PyAutoGUI操作指南】03 键盘控制功能+实现热键组合+ KEYBOARD_KEYS

4.1 write()函数


import pyautogui
# 使用write()只能按单字符键,因此不能按Shift 或 F1键
# 立即输入字符
pyautogui.write('Hello world!')
# 依次输入字符,每个字符之间间隔0.25秒
pyautogui.write('Hello world!', interval=0.25)


4.2 press()、keyDown()和keyUp()函数


import pyautogui
# press()函数实际上只是keyDown()和keyUp()函数的包装器,它们模拟按下一个键,然后释放它。
# 在每次按下之间添加延迟间隔,设置interval即可,传入int或float
# 按下回车键
pyautogui.press('enter')
# 按下F1键
pyautogui.press('f1')
# 按下←键
pyautogui.press('left')
# 要在按住Shift键的同时按左箭头键三次
pyautogui.keyUp('shift')
pyautogui.press(['left', 'left', 'left']) # 多个按键用字符串列表
pyautogui.keyDown('shift')
# 设置presses=3,实现按←键3次
pyautogui.press('left', presses=3)


4.3 hotkey() :实现热键组合


import pyautogui
# 为了方便按下热键或键盘快捷键,可将热键()传递给几个键串,这些键串将按顺序按下,然后按相反顺序释放。
# 要在每次按下之间添加延迟间隔,可为interval参数传递int或float
pyautogui.hotkey('ctrl', 'shift', 'esc')
# 等价于
pyautogui.keyDown('ctrl')
pyautogui.keyDown('shift')
pyautogui.keyDown('esc')
pyautogui.keyUp('esc')
pyautogui.keyUp('shift')
pyautogui.keyUp('ctrl')


4.4 KEYBOARD_KEYS


KEY_NAMES = [
    "\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",
]


目录
相关文章
|
6月前
|
iOS开发 MacOS
mac bookpro m1 笔记本关闭键盘屏幕 禁用键盘屏幕使用 键盘屏幕误触 Touch Bar禁用 禁用MacBook-Pro的触摸栏
mac bookpro m1 笔记本关闭键盘屏幕 禁用键盘屏幕使用 键盘屏幕误触 Touch Bar禁用 禁用MacBook-Pro的触摸栏
232 3
|
7月前
LabVIEW在不按Control键的情况下选择列表框中的多个项目
LabVIEW在不按Control键的情况下选择列表框中的多个项目
50 1
window caps 转换成 ctrl 按键映射
window caps 转换成 ctrl 按键映射
键盘排序、FN-Shift功能
每件事情或者每个事物都是有背景和由来的,就像是键盘上的每个键位字母,都是有存在的价值和意义的,缺一不可,发挥着它的巨大作用。
240 0
|
Windows
电脑睡眠模式快捷键是什么(Fn按键作用和组合键详解)
电脑睡眠模式快捷键是什么(Fn按键作用和组合键详解)
1750 0
Qt实用技巧:将QWidget作为输入窗口,接收键盘消息、输入法并且控制输入法悬浮工具栏位置控制
Qt实用技巧:将QWidget作为输入窗口,接收键盘消息、输入法并且控制输入法悬浮工具栏位置控制
HMI-5-[QtKeyEvent]:解决Qt键盘事件无法获取到上下左右等按键并实现键盘解析
HMI-5-[QtKeyEvent]:解决Qt键盘事件无法获取到上下左右等按键并实现键盘解析
444 0
HMI-5-[QtKeyEvent]:解决Qt键盘事件无法获取到上下左右等按键并实现键盘解析
|
Python
Python 技术篇-pyHook键盘鼠标监听事件,监测鼠标键盘按键实例演示
Python 技术篇-pyHook键盘鼠标监听事件,监测鼠标键盘按键实例演示
416 0
Python 技术篇-pyHook键盘鼠标监听事件,监测鼠标键盘按键实例演示
|
图形学
Unity3D Input按键系统
默认输入轴:   Horizontal 和 Vertical被映射到w, a, s, d键和方向键   Fire1, Fire2, Fire3被分别映射到Ctrl,Option(Alt)和Command键   Mouse X 和 Mouse Y被映射到鼠标移动增量   Window Shake X...
1596 0