如何使用Python同时记录鼠标和键盘的运动? [英] How to record mouse and keyboard movement simultaneously with Python?

查看:167
本文介绍了如何使用Python同时记录鼠标和键盘的运动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个记录鼠标和键盘事件的函数,直到按下特定的键,然后将它们一起重放.

I want to create a function that records both mouse and keyboard events until a specific key is pressed and then replays them together.

我认为这可以通过keyboardmouse模块来实现.在先前的问题中,我问了

I think that this can be achieved with the keyboard and mouse modules. In an earlier question, I asked how to record the mouse movement until a key is pressed, and I got the following code:

import mouse
import keyboard

events = []                 #This is the list where all the events will be stored
mouse.hook(events.append)   #starting the mouse recording
keyboard.wait("a")          #Waiting for 'a' to be pressed
mouse.unhook(events.append) #Stopping the mouse recording
mouse.play(events)          #Playing the recorded events

那很好.由于这两个模块是由同一个人制作的,因此我认为键盘模块也可以使用相同的模块.但事实并非如此.

That works fine. As both modules were made by the same people, I assumed that the same would work with the keyboard module. But it doesn't.

mouse_events = []
keyboard_events = []

mouse.hook(mouse_events.append)
keyboard.hook(keyboard_events.append)

keyboard.wait("a")

mouse.unhook(events.append)
keyboard.unhook(events.append)
keyboard.play(events)

上面的代码中的keyboard.hook(events.append)行引发错误: TypeError: unhashable type: 'list'.

The keyboard.hook(events.append) line in the code above throws an error: TypeError: unhashable type: 'list'.

我试图检查模块文件,但是我大部分都不理解.

I tried to check the module files but I fail to understand most of it.

因此,总结一下:如何同时开始录制鼠标和键盘,同时停止它们和同时运行它们? mousekeyboard模块是实现这一目标的最佳选择吗?

So, to summarize: How can I start the mouse and keyboard recording at the same moment, stop them both at the same time and run both simultaneously? Are the mouse and keyboard modules the best option to achieve this?

推荐答案

您的代码有问题.
您的清单是:

There is a problem with your code.
Your lists are:

mouse_events = []
keyboard_events = []

但是您使用的是events.append而不是列表名称.好像您忘了修改代码.

But you are using events.append rather than the list name. Looks like you forgot to modify the code.

抛出错误是因为模块keyboarddict用于hook与模块mouse不同,并且您不能使用list作为键.

The error is thrown because the module keyboard uses dict for hook unlike module mouse and you can't use list as keys.

您可以使用lambda解决此问题:

You can solve this by using lambda:

keyboard.hook(lambda _: keyboard_events.append(_))


还有一种更简单的方法,而无需使用hook,但这仅适用于模块keyboard


There is more simpler way to do this without using hook but its only for module keyboard

使用start_recording()stop_recording()

1) start_recording()启用键盘事件的记录.无需回调,您一次只能录制一次.
2) stop_recording()停止开始的录制.它返回已记录事件的列表.

1) start_recording() to enable the recording of keyboard events. It doesn't take a callback and you can record once at a time.
2) stop_recording() to stop the started recording. It returns the list of recorded events.

mouse模块没有stop/start_recording
因此,您的最终代码将如下所示:

mouse module doesn't have stop/start_recording
So your final code will look like this:

import mouse
import keyboard

mouse_events = []


mouse.hook(mouse_events.append)
keyboard.start_recording()       #Starting the recording

keyboard.wait("a")

mouse.unhook(mouse_events.append)
keyboard_events = keyboard.stop_recording()  #Stopping the recording. Returns list of events


一起播放事件:

同时播放两个事件的唯一方法是使用 threading

The only way to play both events at the same time is to use threading

以下是您的代码示例:

import threading
import mouse
import keyboard

mouse_events = []


mouse.hook(mouse_events.append)
keyboard.start_recording()

keyboard.wait("a")

mouse.unhook(mouse_events.append)
keyboard_events = keyboard.stop_recording()

#Keyboard threadings:

k_thread = threading.Thread(target = lambda :keyboard.play(keyboard_events))
k_thread.start()

#Mouse threadings:

m_thread = threading.Thread(target = lambda :mouse.play(mouse_events))
m_thread.start()

#waiting for both threadings to be completed

k_thread.join() 
m_thread.join()

这篇关于如何使用Python同时记录鼠标和键盘的运动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆