1

I'm new to python and I'm trying to learn how to use the mouse and keyboard modules. I am trying to write some code that plays a recording while I'm holding left click, I think the issue is that this may be only part of the recording depending on how long I hold it. This is what I have so far but it isn't playing the recording.

import mouse
import keyboard


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


def onleftclick():
    while mouse.is_pressed(button='left') == True:
        mouse.play(events)


mouse.on_click(onleftclick)
keyboard.wait('esc')
apex
  • 19
  • 2

2 Answers2

1

you could do:

import keyboard
while True:
    #lets just say that you want to detect q and left key pressed
    if keyboard.is_pressed('left') and keyboard.is_pressed('q'):
        print('left key and q pressed')






0

Use a keystroke to play back the recording. The recording won't play if you're using the mouse.

Try this code. Press a to play back the recording:

import mouse
import keyboard

events = []                 #This is the list where all the events will be stored

mouse.hook(events.append)   #starting the recording
keyboard.wait("a")          #Waiting for 'a' to be pressed
print('playback...')
mouse.unhook(events.append) #Stopping the recording
mouse.play(events)

keyboard.wait('esc')
print('bye')
Mike67
  • 11,175
  • 2
  • 7
  • 15
  • So the recording won't play if I'm also using the mouse at the same time, and this is how it always works? and if so, do you know any module that would allow me to use the mouse while also playing the recording? Thanks. – apex Sep 27 '20 at 02:30
  • I don't see how this is possible. If you're actively using the mouse, the 'live` events will always override the recording. – Mike67 Sep 27 '20 at 02:36
  • Well what I'm trying to do is kinda hard to explain but theoretically would it be possible to maybe change each value in the events to like instead of the (x,y) coordinates like mouse current position minus a certain x and y value? – apex Sep 27 '20 at 02:46