ahk icon indicating copy to clipboard operation
ahk copied to clipboard

Mimicking a toggled auto clicker for LButton mode='p' not working

Open MiniFredx opened this issue 4 years ago • 2 comments

Hello, amazing library btw.

I am trying to emulate this ahk function within python

$LButton::
KeyIsDown := GetKeyState("LButton","p")
While GetKeyState("LButton","p"){
  Send {LButton}
  Sleep 150
}

My code currently is

while(self.ahk.key_state('Lbutton')):
                        self.ahk.key_press('{LButton}')
                        sleep(.15)

And what that does is repeatedly type the words "up down"

I tried using this code:

while(self.ahk.key_state('Lbutton')):
                        self.ahk.click()
                        sleep(.15)

But it doesn't work. I think the ahk.click() just makes the mouse button go up while I am holding it down.

I tried using mode='p' as well but it doesn't seem to work either. If I am missing something just let me know.

The expected behavior I am looking for is while I hold the left mouse button down it clicks it every 150 milliseconds. Is this possible to write in python? Thanks for the help ahead of time!

MiniFredx avatar Jul 04 '21 03:07 MiniFredx

bump xD

MiniFredx avatar Jul 12 '21 03:07 MiniFredx

Sorry for the long wait on a response here.

I think one of the difficulties you may have here is that the sending will usually cause the key to be released, meaning on the next iteration, the state is no longer True. This is also what I noticed in my own testing (the condition fires only once, despite holding the mouse button down).

Normally, using mode='p' would address this, but this only works because AHK tracks state changes while it's running:

it will accurately reflect whether or not the user is physically holding down the key or button (as long as it was pressed down while the script was running)

However, the default implementation of this library starts a new script for every function invocation, so that might be the problem you're encountering. Once #111 is available, it should solve that problem because there would just be one long-running daemon script that would be able to keep track of the physical state properly. You could also try it by installing from the daemon branch.

Testing using daemon mode, I was able to get this working by sending a lbutton DOWN only.

from ahk.daemon import AHKDaemon
from ahk.directives import InstallMouseHook, InstallKeybdHook
ahk = AHKDaemon(directives=[InstallKeybdHook, InstallMouseHook])
ahk.start()
from ahk.keys import Key
lbutton = Key('LButton')
import time
while True:
    print(ahk.key_state('Lbutton', mode='P'))
    while ahk.key_state('LButton', mode='P'):
        print('key is down! Sending Lclick!')
        ahk.send(lbutton.DOWN)
        time.sleep(0.15)
    print('key was not down. Doing nothing')
    time.sleep(0.15)

This seems to work™️ at least with Daemon Mode 😈, but may or may not work in your particular application.

Another option you may have in the meantime until #111 is merged, is using a separate raw script or hotkey script:

from ahk import AHK
ahk = AHK()
script = """
$LButton::
KeyIsDown := GetKeyState("LButton","p")
While GetKeyState("LButton","p"){
  Send {LButton}
  Sleep 150
}
"""

ahk.run_script(script, blocking=False)

Or maybe using a hotkey script.

from ahk import AHK
ahk = AHK()
import time
script = """
KeyIsDown := GetKeyState("LButton","p")
While GetKeyState("LButton","p"){
  Send {LButton}
  Sleep 150
}
"""
hotkey = ahk.hotkey("$LButton", script=script)
hotkey.start()
while True:
    time.sleep(1)  # prevent script from exiting to keep the hotkey going.

spyoungtech avatar Jul 13 '21 09:07 spyoungtech

I believe this should be resolved in v1. If not, please open a new issue.

spyoungtech avatar May 02 '23 05:05 spyoungtech