使用pyhook3 pynput實現鍵盤連發

2021-10-05 11:14:44 字數 2592 閱讀 4070

pyhook在視窗標題為中文時會報錯,必須安裝pyhook3。

pip install pyhook3

可能存在以下問題:

swig.exe不存在:

提示缺少vc++ 14.0:

visual c++ build tools 2015

cl.exe不存在

開啟 c:\program files (x86)\microsoft visual c++ build tools\visual c++ 2015 x86 x64 cross build tools command prompt.lnk 然後在其中執行pip安裝命令

使用pynput監聽:

from pynput.keyboard import key, listener, controller

import time

from threading import lock, thread

import random

lock = lock(

)abilities =

set(

)hot_keys =

keyboard = controller(

)def

cast_ability()

:while

true

: time.sleep(

0.001

) bak =

list

(abilities)

random.shuffle(bak)

for ability in bak:

time.sleep(random.uniform(

0.05

,0.1))

keyboard.press(ability)

keyboard.release(ability)

deftoggle_ability_status

(key)

:if key in hot_keys:

ability = hot_keys[key]

with lock:

if ability in abilities:

abilities.remove(ability)

else

: abilities.add(ability)

threads =

[thread(target=cast_ability)

, listener(on_press=toggle_ability_status)

]for listener in threads:

listener.start(

)for listener in threads:

listener.join(

)

使用pyhook3監聽

import pythoncom

import pyhook3

from pynput.keyboard import controller

import time

from threading import lock, thread

import random

lock = lock(

)abilities =

set(

)hot_keys =

keyboard = controller(

)def

cast_ability()

:while

true

: time.sleep(

0.001

) bak =

list

(abilities)

random.shuffle(bak)

for ability in bak:

time.sleep(random.uniform(

0.05

,0.1))

keyboard.press(ability)

keyboard.release(ability)

deftoggle_ability_status

(event)

: key = event.key

if key in hot_keys:

print

(key)

ability = hot_keys[key]

with lock:

if ability in abilities:

abilities.remove(ability)

else

: abilities.add(ability)

return

true

hm = pyhook3.hookmanager(

)hm.keydown = toggle_ability_status

hm.hookkeyboard(

)thread(target=cast_ability)

.start(

)pythoncom.pumpmessages(

)

Python使用pynput統計鍵盤使用情況

今天天氣突然變冷,下午快上課的時候下起了雨,所以果斷逃了課,哈哈哈 在宿舍看完部電影後,覺得有點無聊 突發奇想想用python寫個統計鍵盤使用情況的小指令碼 好了,廢話不多說,上 import pynput import pymysql from copy import deepcopy creat...

Python3 8環境安裝PyHook3

1.安裝python對應版本的pyhook3 網 址 利用cmd視窗定位到解壓縮的資料夾執行setup.py。執行命令為python setup.py build。將生成的檔案放到c python3.7 lib site packages 中。2 安裝pythoncom 執行命令為pip insta...

Pynput使用組合鍵,以及同時監控鍵盤和控制鍵盤

我想寫乙個使用快捷鍵來執行網頁搜尋的小軟體,但發現網上還沒有很多關於pynput使用組合鍵的帖子,只好自己寫乙個了.我剛開始的時候找了半天,發現了乙個人使用多執行緒的方式來實現這個功能,但我覺得這個東西還是越簡單越好 from pynput import keyboard from pynput.k...