AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Toggle key hold down on and off

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
lordmarcus1st



Joined: 30 Jun 2008
Posts: 2

PostPosted: Mon Jun 30, 2008 3:49 pm    Post subject: Toggle key hold down on and off Reply with quote

Here is an example of what I would like to do:

When I press the key XButton1 it will hold down key w until I press XButton1 again.
Basically I want this for an fps so when I press XButton1 it will make me move forward until I press XButton1 again. Another thing which could be useful if it’s not too hard to do is if w or s is pressed while auto run is toggled then it will stop the auto run.

Here is what I have come up with so fare but it doesn't work.

XButton1::Send {w down}
loop
{
if XButton1
{
send, {w UP}
}
}
return

Any help would be appreciated.
Back to top
View user's profile Send private message
quicktest



Joined: 30 Jul 2004
Posts: 42

PostPosted: Mon Jun 30, 2008 6:25 pm    Post subject: Reply with quote

See if this helps:

Code:


W_DOWN = 0

XButton1::
If W_DOWN = 0
{
  Send {w down}
  W_DOWN = 1
}
Else
{
  Send {w up}
  W_DOWN = 0
}
return


I'm sure there were much more elegant solutions given by the experts on this board before. Try do a search on toggling hotkey.
Back to top
View user's profile Send private message
lordmarcus1st



Joined: 30 Jun 2008
Posts: 2

PostPosted: Tue Jul 01, 2008 8:32 am    Post subject: Reply with quote

A friend of mine helped me work it out! Smile
Heres the working code.

Code:
XButton1::
Loop
{
Send {w down}
Sleep, 100
GetKeyState, state, XButton1, P
if state = D
  {
  Send {w up}
  break
  }
}
return
Back to top
View user's profile Send private message
Guest






PostPosted: Tue Jul 01, 2008 9:19 am    Post subject: Reply with quote

Not tested (in a game), but looks good...

Code:
XButton1::
;//*** NOTE *** moved out of Loop, unless game really needs to keep receiving down events...
Send, {w down}
stoprunning=
;//*** NOTE *** wait for release of this hotkey before Loop starts...
KeyWait, %A_ThisHotkey%
Loop {
   ;//*** NOTE *** testing in Notepad doesn't show w-flood, so Tooltip for debugging...
   ;//Tooltip, loop(%A_Index%)
   if (stoprunning || GetKeyState(A_ThisHotkey, "P")) {
      Send {w up}
      break
   }
   Sleep, 19
}
return

~w::stoprunning=1
~s::stoprunning=1

...I should stop there, but here's another version using KeyWait instead of Sleep...

Code:
XButton1::
;//*** NOTE *** moved out of Loop, unless game really needs to keep receiving down events...
Send, {w down}
stoprunning=
;//*** NOTE *** wait for release of this hotkey before Loop starts...
KeyWait, %A_ThisHotkey%
Loop {
   ;//*** NOTE *** testing in Notepad doesn't show w-flood, so Tooltip for debugging...
   ;//Tooltip, loop(%A_Index%)
   ;//*** NOTE *** using KeyWait, if you're "Sleeping" anyway, might as well wait for something...
   KeyWait, %A_ThisHotkey%, D T.019
   if (stoprunning || !errorlevel) {
      Send {w up}
      break
   }
}
return

~w::stoprunning=1
~s::stoprunning=1

...remember to move Send, {w down} back inside the Loop, if your game needs it (of course test without moving it 1st)...
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group