Jump to content


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


  • Please log in to reply
4 replies to this topic

#1 WestsFastestHand

WestsFastestHand
  • Guests

Posted 03 August 2012 - 11:00 PM

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.

#2 dylan904

dylan904
  • Members
  • 706 posts

Posted 04 August 2012 - 02:41 AM

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.

#3 girlgamer

girlgamer
  • Moderators
  • 2039 posts

Posted 04 August 2012 - 04:14 AM

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.

#4 WestsFastestHand

WestsFastestHand
  • Members
  • 1 posts

Posted 05 August 2012 - 01:18 AM

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?

#5 MasterFocus

MasterFocus
  • Moderators
  • 4127 posts

Posted 05 August 2012 - 06:12 AM

Try this [untested]:
*$XButton1::
    While GetKeyState( "XButton1" , "P" )
    {
        Send,{LButton Down}
        Sleep, 25
        Send,{LButton Up}
        Sleep, 30
    }
    [color=#FF0000]KeyWait, XButton1[/color]
Return