欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python pynput监听键盘

程序员文章站 2022-03-10 09:45:18
"""小白随笔,大佬勿喷"""#键盘输入from pynput.keyboard import Key,Controller,Listenerkeyboard = Controller()keyboard.press("a") #按下akeyboard.release("a") #松开akeyboa ......
"""小白随笔,大佬勿喷"""
#键盘输入
from pynput.keyboard import key,controller,listener
keyboard = controller()
keyboard.press("a") #按下a
keyboard.release("a") #松开a
keyboard.press("a") #按下a
keyboard.release("a") #松开a
with keyboard.pressed(key.shift): #按下shift
keyboard.press("a") #shift + a
keyboard.type("hello world") #输出文字

--------------------------------------------------

# 键盘监听
from pynput.keyboard import key,listener
def on_press(key):
if key == key.enter:
print('you press enter')
else:
return false #按键不是enter,停止监视
def on_release(key):
if key == key.enter:
print('you release enter')
#监听键盘按键
with listener(on_press=on_press,on_release=on_release) as listener:
listener.join()
#停止监视
listener.stop()