Jump to content

Recommended Posts

Posted

Hi

I'm just learning autoit and was trying a simple idea to throttle how fast someone can press a key.

HotKeySet("{SPACE}", "captureSpace")

Func captureSpace()
         HotKeySet("{SPACE}")
         Send("{SPACE}")
         HotKeySet("{SPACE}", "captureSpace")
EndFunc

 

How would I stop someone pressing space bar more than once a second even if they were thrashing the key?  I thought this would be simple but my attemps at introducing TimerDiff() have not worked.  Thank you for any pointers!

Posted
HotKeySet("{SPACE}", "captureSpace")

Local $iTimer = TimerInit()

While (True)
    Sleep(100)
WEnd

Func captureSpace()
    If (TimerDiff($iTimer) >= 1000) Then
        ConsoleWrite("More than a second passed, allowed" & @CRLF)
        HotKeySet("{SPACE}")
        Send("{SPACE}")
        HotKeySet("{SPACE}", "captureSpace")
        $iTimer = TimerInit()
    Else
        ConsoleWrite("Stop spamming space bar!" & @CRLF)
    EndIf
EndFunc   ;==>captureSpace

 

Posted (edited)

that logic is off, imho I think you would capture space as you have, only capturespace would...

1) start the timer

2) start a loop

3) set the hotkey for space to a function that does nothing

4) check the timer, if past a second then reset the hotkey back to the capturespace function and exitloop

 

*Inuno's pretty much does that, it's what I get for disabling notifications.

Edited by iamtheky

  Reveal hidden contents

Posted

Thank you for your fast responses.

@InunoTaishou

Thank you, that helped immensely!  I was making a silly mistake now I see what you've done.  That works perfectly.  Thank you also for including the console messaging, it's very useful!

Posted

dougal88,

Do you realize that this will affect all apps running?  Start your script, then notepad and press the space bar.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...