[Python进阶] 操纵键盘:Pynput

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

按下并弹起某个按键,按键只能为单个字符。否则报错。

from pynput.keyboard import Controller, Key

keyboard = Controller()
# 按下并释放f
keyboard.tap('f')
# 按下python
for s in list('python'):
    keyboard.tap(s)

输入字符串及组合键

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进阶,#,六,系统及相关操作,python,计算机外设,Python进阶)