6.7 操纵键盘:Pynput
6.7.1 press、release
按下或释放某个按键。
from pynput.keyboard import Controller, Key
keyboard = Controller()
# 按下并释放f
keyboard.press('f')
keyboard.release('f')
# 按下组合按键:alt + tab
keyboard.press(Key.alt)
keyboard.press(Key.tab)
keyboard.release(Key.alt)
keyboard.release(Key.tab)
6.7.2 tap
按下并弹起某个按键,按键只能为单个字符。否则报错。文章来源:https://www.toymoban.com/news/detail-752212.html
from pynput.keyboard import Controller, Key
keyboard = Controller()
# 按下并释放f
keyboard.tap('f')
# 按下python
for s in list('python'):
keyboard.tap(s)
输入字符串及组合键
文章来源地址https://www.toymoban.com/news/detail-752212.html
from pynput.keyboard import Controller, Key
keyboard = Controller()
# 按下并释放'a'键
keyboard.press(Key.a)
keyboard.release(Key.a)
# 输入一个字符串
keyboard.type('Hello World')
# 按下并释放组合键 'Ctrl+Shift+T'
with keyboard.pressed(Key.ctrl, Key.shift):
keyboard.press('t')
keyboard.release('t')
到了这里,关于[Python进阶] 操纵键盘:Pynput的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!