Code Monkey home page Code Monkey logo

Comments (7)

boppreh avatar boppreh commented on July 22, 2024 1

You can sort them together using lambda i: i.timeas key, or just append both to the same list. But that's not very useful because you still need to split them later to replay.

Thats something still missing from the libraries, but I intent to fix in the future. You can still replay them in separate threads.

from mouse.

reckoner avatar reckoner commented on July 22, 2024 1
class Recorder(object):
    def __init__(self):
        self.mouse_recorded=[]
        self.keyboard_recorded=[]

    def record(self,key_abort_seq='esc, esc'):
        self.mouse_recorded[:] = [] # reset upon new recording
        self.keyboard_recorded[:] = []
        mouse.hook(self.mouse_recorded.append)
        keyboard.hook(self.keyboard_recorded.append)
        keyboard.wait(key_abort_seq)
        mouse.unhook(self.mouse_recorded.append)
        keyboard.unhook(self.keyboard_recorded.append)

    def playmouse(self,**kwds):
        '''
        play only mouse events
        '''
        mouse.play(self.mouse_recorded,**kwds)

    def playkeyboard(self,**kwds):
        '''
        play only keyboard events
        '''
        keyboard.play(self.mouse_recorded,**kwds)

    def play(self,key_speed_factor=1.0, mouse_speed_factor=1.0, **kwds):
        '''
        play both mouse and keyboard events in order
        '''
        k=sorted(self.mouse_recorded+ self.keyboard_recorded,key=lambda i:i.time)
        for c,m in it.groupby(k,key=lambda i:isinstance(i,keyboard.KeyboardEvent)):
            if c:
                speed_factor = kwds.pop('speed_factor',key_speed_factor)
                keyboard.play(m,speed_factor=speed_factor,**kwds)
            else:
                speed_factor = kwds.pop('speed_factor',mouse_speed_factor)
                mouse.play(m,speed_factor=speed_factor,**kwds)

Then, you can do

r = Recorder()
r.record()

Thanks for the headstart!

from mouse.

boppreh avatar boppreh commented on July 22, 2024

Not with the record function, but record is so simple that you can just reimplement it:

def record(button=RIGHT, target_types=(DOWN,)):

My suggestion:

import mouse, keyboard
recorded = []
mouse.hook(recorded.append)
keyboard.wait('esc, esc')
mouse.unhook(recorded.append)
print(recorded)

from mouse.

boppreh avatar boppreh commented on July 22, 2024

If this solves your problem, I'll add to the examples directory.

from mouse.

reckoner avatar reckoner commented on July 22, 2024

It does! Thanks!

Along these lines, is it possible to simultaneously record both mouse and keyboard interactions? I have tried the following:

mouse_recorded = []
keyboard_recorded = []
mouse.hook(mouse_recorded.append)
keyboard.hook(keyboard_recorded.append)
keyboard.wait('esc, esc')
mouse.unhook(mouse_recorded.append)
keyboard.unhook(keyboard_recorded.append)
print(mouse_recorded)
print keyboard_recorded

However there is no time-alignment between the two lists, so I don't know the sequential order of the mouse and keyboard interactions.

Any ideas here?

from mouse.

boppreh avatar boppreh commented on July 22, 2024

Thanks @reckoner for the script. To simplify this process in the future, I'll add an event.replay(speed_factor) method to both keyboard and mouse events. This would allow a simple for event in events: event.replay(). Unfortunately it requires merging the events into the main file for each module to avoid circular dependencies, which breaks compatibility or requires hackish aliases, so I'll wait until the next breaking changeset.

from mouse.

QiangF avatar QiangF commented on July 22, 2024

When I call

r = Recorder()
r.record()

I get this error:

File "keyboard/__init__.py", line 465, in hook
    _hooks[callback] = _hooks[remove_] = remove_
TypeError: unhashable type: 'list'

from mouse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.