Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Loop a Button?


  • Please log in to reply
16 replies to this topic
r4nd0m0s1ty
  • Members
  • 14 posts
  • Last active: Oct 21 2009 03:20 AM
  • Joined: 26 Jun 2009
Hey, I already found a script somewhere to loop a mouse click while the button is being pressed down, but I can't figure out how to do that to a button. For example, if I hold the spacebar, I want the script to recognize this and then start looping a space. Or if I hold the "P" button, I want it to repeat a "click of "P". If anyone can help me, that would be great. :D And here is the mouse click script:

~LButton::

Loop
{
    Sleep 80  ; This is the delay between clicks, in milliseconds.
    GetKeyState, LButtonState, LButton, P
    if LButtonState = U  ; User has physically released the button, so end the loop.
        break 
    MouseClick, Left
}
return


lilalurl.T32
  • Members
  • 391 posts
  • Last active: Jul 05 2011 03:39 PM
  • Joined: 17 May 2007
A quick question: Do you have a specific aim for this, using it for a game for instance?

Because otherwise, in Windows the keys already repeat themselves if you hold them. You can change the delay before repeating starts and the repeating frequency in the Configuration Panel, under the Keyboard entry.
________
Smoke kills

r4nd0m0s1ty
  • Members
  • 14 posts
  • Last active: Oct 21 2009 03:20 AM
  • Joined: 26 Jun 2009
As a matter a fact, I do plan to use this in a game. The space bar does a certain action but must be pressed repeatedly, and I don't want to keep pressing it a bunch, if you know what I mean ;D

lilalurl.T32
  • Members
  • 391 posts
  • Last active: Jul 05 2011 03:39 PM
  • Joined: 17 May 2007
For various reasons (check the FAQ in the help file for details) games can introduce many problems for this kind of looping.

I advise you first to start and see if the game has no issue with AHK sending a key normally (i.e. you press one key/button and the game recognize the key you remapped it to).

Then, you should check the following thread and especially the third piece of code mb777 posted, which should do exactly what you want once you adapt it to your need:
<!-- m -->http://www.autohotke...pic.php?t=40381<!-- m -->
________
Herbal vaporizers

r4nd0m0s1ty
  • Members
  • 14 posts
  • Last active: Oct 21 2009 03:20 AM
  • Joined: 26 Jun 2009
I have used my mouse-click script in there and it works flawlessly. Thank you very much for all of your help. I'm going to try mb777's script and I'll reply back here. Thx :D

r4nd0m0s1ty
  • Members
  • 14 posts
  • Last active: Oct 21 2009 03:20 AM
  • Joined: 26 Jun 2009
Theres just one problem, I am not an expert in recoding so I don't know how to make this to where it works for my purpose. Think you could help?

delaybetweenfkeys:=2
fkeydowndelay:=37

F1::
   loop
   {
      if checkkeyreleased(A_thishotkey)   ; a_thishotkey is a built in variable
         break                             ; that stores whatever the last key pressed was
      presskey("Enter")                       ; which of course will be F1
   }
return

presskey(key,del1 =999999, del2 =999999) ;you cant set variables as default parameters in functions
{                                        ; so this is a way around it
   global delaybetweenfkeys,fkeydowndelay
   del1:=del1=999999 ? fkeydowndelay : del1  ;default value will be value of 'fkeydowndelay' 
   del2:=del2=999999 ? delaybetweenfkeys : del2 ;default value will be value of 'delaybetweenfkeys' 
   sendinput {%key% down}                ;press the key down
   sleep %del1%                         ;hold for this delay
   sendinput {%key% up}                  ;release key
   sleep %del2%                         ; for this delay
return
}


checkkeyreleased(key,checkdelay =2)
{
   
     Sleep, %checkdelay%               ;tiny delay to give computer a chance to see if key is down 
   GetKeyState, state, %key%, P         ;stores the state of the key to variable 'state'
   if state = U  ; The key has been released, so break out of the loop.      
       return true
   else
      return false
}


lilalurl.T32
  • Members
  • 391 posts
  • Last active: Jul 05 2011 03:39 PM
  • Joined: 17 May 2007
Well, the script is mainly made of functions which you should not have to change.

Supposing you want 'to hold spacebar and have it repeat spacebar', you should only have to change this part of the script:

F1::
   loop
   {
      if checkkeyreleased(A_thishotkey)    ; a_thishotkey is a built in variable
      break                              ; that stores whatever the last key pressed was
      presskey("Enter")               ; which of course will be F1       
   }
return


to


[color=red]Space[/color]::
   loop
   {
      if checkkeyreleased(A_thishotkey)  
         break                            
      presskey("[color=red]Space[/color]")                      
   }
return

Not tested but from a quick glance, it should work.

I will be either quite busy or away during a few days, so if you still cannot get it to work and nobody else can help you, PM me on Saturday to remind me of this.


Edit: Maybe using the same key as the one that will be sent could create a problem (auto-aborting).
If that happens, try using F1 (or whatever key you fancy) instead of the first Space in the script. Not exactly what you want, but a workaround nonetheless.
________
Subaru impreza wrx sti

r4nd0m0s1ty
  • Members
  • 14 posts
  • Last active: Oct 21 2009 03:20 AM
  • Joined: 26 Jun 2009
It says when I try to reload the script that this line is invalid:
      if checkkeyreleased(A_thishotkey)
It says its a non-existant function. How do I work around that?

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
Make sure that function exists in your script, if it does re-save the changes to your script then reload it.

r4nd0m0s1ty
  • Members
  • 14 posts
  • Last active: Oct 21 2009 03:20 AM
  • Joined: 26 Jun 2009
Heres EXACTLY what it says when I try and use this for my script:

Script:
Space::
   loop
   {
      if checkkeyreleased(A_thishotkey)  
         break                            
      presskey("Space")                      
   }
return

What it says:
Error: Call to nonexistant function.

Specifically: checkkeyreleased(A_thishotkey)

               Line#
               001: Return
               002: Loop
               003: {
---->        004: if checkkeyreleased)A+thishotkey)
               005: Break
               006: presskey("Space")
               007: }
               008: Return
               008: Exit

This program will exit.

Now how do I fix THAT right there.

lilalurl.T32
  • Members
  • 391 posts
  • Last active: Jul 05 2011 03:39 PM
  • Joined: 17 May 2007
You must use the whole script that mb777 made (i.e. what you posted on Mon Jun 29, 2009 7:15 pm ).

What I posted was only the part that was relevant for you to modify.
However, the remainder of the script is mandatory because it contains user-made functions which are called by the script. Not having them results in the error you mention.


I have also tried it, sadly using space to spend space won't work.
But there is no problem when using another key F1 for instance as mb777 did or LAlt (left alt key, which is next to space and hence should be a good enough workaround).
________
Honda G4 Transmission History

  • Guests
  • Last active:
  • Joined: --
Sorry for late reply.
So the completed script to use should look like this:
delaybetweenfkeys:=2
fkeydowndelay:=37

LAlt::
   loop
   {
      if checkkeyreleased(A_thishotkey)   ; a_thishotkey is a built in variable
         break                             ; that stores whatever the last key pressed was
      presskey("Space")                       ; which of course will be F1
   }
return

presskey(key,del1 =999999, del2 =999999) ;you cant set variables as default parameters in functions
{                                        ; so this is a way around it
   global delaybetweenfkeys,fkeydowndelay
   del1:=del1=999999 ? fkeydowndelay : del1  ;default value will be value of 'fkeydowndelay' 
   del2:=del2=999999 ? delaybetweenfkeys : del2 ;default value will be value of 'delaybetweenfkeys' 
   sendinput {%key% down}                ;press the key down
   sleep %del1%                         ;hold for this delay
   sendinput {%key% up}                  ;release key
   sleep %del2%                         ; for this delay
return
}


checkkeyreleased(key,checkdelay =2)
{
   
     Sleep, %checkdelay%               ;tiny delay to give computer a chance to see if key is down 
   GetKeyState, state, %key%, P         ;stores the state of the key to variable 'state'
   if state = U  ; The key has been released, so break out of the loop.      
       return true
   else
      return false
}

If not, please correct. :D

oculuss
  • Guests
  • Last active:
  • Joined: --
Perhaps you have noticed this already, but a parenthesis on line 4 is backward....

  • Guests
  • Last active:
  • Joined: --
Where might this "backwards" paranthesis be? I don't see any parenthesies on line 4.

r4nd0m0s1ty
  • Members
  • 14 posts
  • Last active: Oct 21 2009 03:20 AM
  • Joined: 26 Jun 2009
Lol sorry peeps, may have been posting as guest because the site didn't auto-log me in. Weird ...