from pynput.keyboard import Listener
import time
def on_press(key):
pass
def on_release(key):
all_key.append(str(key))
print(all_key)
if "'r'" in all_key:
print("开大招了")
all_key.clear()
if "'q'" in all_key:
print("开q技能了")
all_key.clear()
if "'w'" in all_key:
print("开w技能了")
all_key.clear()
if "'e'" in all_key:
print("开e技能了")
all_key.clear()
if 'Key.ctrl_l' in all_key and "'c'" in all_key: # ctrl+c
print('这是复制键')
all_key.clear()
if 'Key.ctrl_l' in all_key and "'v'" in all_key: # ctrl+v
print('这是粘贴键')
all_key.clear()
try:
if all_key[-1] == 'Key.ctrl_l':
time1 = time.time()
while True:
if time.time() - time1 >= 1:
all_key.clear()
break
except:
pass
# if key == Key.esc: # 停止监听
# return Falseurn False
def start_listen():
with Listener(on_press=None, on_release=on_release) as listener:
listener.join()
if __name__ == '__main__':
all_key = []
start_listen()
python监控键盘操作
最新推荐文章于 2024-07-12 07:17:13 发布