Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

If a getkeystate() hotkey runs too fast it gets stuck?


  • Please log in to reply
4 replies to this topic
WestsFastestHand
  • Guests
  • Last active:
  • Joined: --
Hello everyone,

I have been using AHK for spamming my "fire button" in gaming for some time now... *shamelessly admits*
And i have always had this little problem with my spamming getting stuck on the "ON" state, if the code runs too fast...

I've finally decided i've had enough trying to "tweak it enough for it to be reliable, by myself", and decided to open this thread and get some proffecional help...

Basically, the method i use is to have a hotkey with an inside
while{getKeyState(HOTKEY_KEY_HERE,"P"){SPAM_CODE_HERE}}
, with works fine most of the time, but every now and then, gets stuck on the "ON" state...

My search has found a few similar questions here, but the closest clue i got was from this post, and the situation described is not really the same. Aditionally, the problem im having might be completely unrelated to that described there (threads)...

Here is my actual code, any tips on improving it are welcome:
#SingleInstance,force
SendMode,Input
Process,Priority,,R

;=======================================================================================================

$XButton1::
{
While(GetKeyState("XButton1","P"))
  {
  Send,{XButton1 down}
  Sleep,25
  Send,{XButton1 up}
  Sleep,25
  }
}
Return

;=======================================================================================================

$F1::
{
While(GetKeyState("F1","P"))
  {
  Send,{F1 down}
  Sleep,25
  Send,{F1 up}
  Sleep,25
  }
}
Return

;=======================================================================================================

::`/tg::
{
  send,Frag!{space}Get{space}down!{enter}
}

BTW, yes, i have tried the same code with keyboard and mouse hooks...Problem persists.

Thanks in advance for any help.

dylan904
  • Members
  • 706 posts
  • Last active: Nov 14 2016 06:17 PM
  • Joined: 18 Jan 2012
The prefix $ does work to only look for the physical input for the hotkey to use, but to my knowledge, when you use {Key Down}, it will interfere with your hotkey and cause itself to loop.

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
Your problem is not so much related to the speed of the routine -- although that could be a problem given the length of the sleeps you're using in the sample code. It's the way you've implemented the hotkeys.. to see that try this
$XButton1::
    [color=#FF0000]Suspend, On[/color]
    While(GetKeyState("XButton1","P"))
    {
        Send, {XButton1 down}
        Sleep, [color=#FF0000]100[/color]
        Send, {XButton1 up}
        Sleep, [color=#FF0000]100[/color]
    }
    [color=#FF0000]Suspend, Off[/color]
    Return
Change only that one routine in your script and see how the XButton routine functions now compared to the other hotkeys you have. I'm betting that you get a serious improvement in both speed and reliability.

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


WestsFastestHand
  • Members
  • 1 posts
  • Last active: Aug 03 2012 11:12 PM
  • Joined: 03 Aug 2012

Your problem is not so much related to the speed of the routine -- although that could be a problem given the length of the sleeps you're using in the sample code. It's the way you've implemented the hotkeys.. to see that try this

$XButton1::
    [color=#FF0000]Suspend, On[/color]
    While(GetKeyState("XButton1","P"))
    {
        Send, {XButton1 down}
        Sleep, [color=#FF0000]100[/color]
        Send, {XButton1 up}
        Sleep, [color=#FF0000]100[/color]
    }
    [color=#FF0000]Suspend, Off[/color]
    Return
Change only that one routine in your script and see how the XButton routine functions now compared to the other hotkeys you have. I'm betting that you get a serious improvement in both speed and reliability.


I'm pretty sure it wasn't supposed to work that way (since the hotkey has already been triggered when the suspend is called), but that code actually made my hotkey's spam-code not fire...
To confirm it, i made the following test code:
*$XButton1::
{
suspend,on
;msgbox, hotkey triggered
While(GetKeyState("XButton1","P"))
  {
  Send,{lbutton down}
  Sleep,25
  ;msgbox, hotkey going
  Send,{lbutton up}
  Sleep,30
  }
;EDIT:vvv
;msgbox, hotkey going number 2
suspend,off
;msgbox, hotkey released
}
Return

*$XButton2::
{
While(GetKeyState("XButton2","P"))
  {
  Send,{lbutton down}
  Sleep,25
  Send,{lbutton up}
  Sleep,25
  }
}
Return

It CONFIRMED the issue, as XButton1's code only runs up to the msgbox, skips untill the suspend is off [EDIT], and shows the "hotkey released" msgbox, while XButton2's actually runs the internal code.

But i liked that idea of suspending the hotkey, if nothing else, it'd make me feel safer about the code. any alternatives to the "suspend" command?

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
Try this [untested]:
*$XButton1::
    While GetKeyState( "XButton1" , "P" )
    {
        Send,{LButton Down}
        Sleep, 25
        Send,{LButton Up}
        Sleep, 30
    }
    [color=#FF0000]KeyWait, XButton1[/color]
Return

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.