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

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



Joined: 04 Jul 2009
Posts: 3

PostPosted: Sat Jul 04, 2009 4:29 am    Post subject: Toggle key Reply with quote

I am new to autohotkey and I used all the searching I could before posting this. I play BF2142 which requires you to hold down a button for your player to crouch, in every other game i play the crouch toggles. I am unable to get used to the nno toggle crouch in BF so I am looking for a script to make me crouch with the first press and then to uncrouch with the next press of the same key. The only script I have found was this



~LAlt up:: ;use LAlt "up" so it doesn't get triggered when you hold ctrl
Goto, Crouch

Crouch:
; Changed: Trigger was changed to "~LAlt" as ~ allows normal operation of key.
If (a_tickCount-lasttime < 400) ;Check when we released ctrl the last time if < 400ms initiate Crouch
{
Loop
{
Send, {LAlt down} ; Initiate 'Crouch'
If IsKeyPressed("LAlt") ; Check if 'LCtrl' pressed. If pressed & released, Break loop.
Send, {LAlt up} ; Release 'Crouch'
Break
}
}
lasttime:=a_tickCount
Return

IsKeyPressed(v_KeyName)
; Returns 1 if %v_KeyName% is currently being pressed, otherwise 0
{
GetKeyState, state, %v_KeyName%, P
If state = D ; The key has been pressed
{
Return 1
}
Return 0
}

The only problem with this is that it requires me to double tap the Left Alt key to crouch and then when I press it once more then I return to standing. They did it this way to keep the original funtion of the key so that you can still hold left alt and crouch as it is held. I do not care about the original function. I just want to press once and be crouched and once more un-crouched. I appreciate any help on this.Thanks!
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Sat Jul 04, 2009 6:54 am    Post subject: Reply with quote

How about this:
Code:
x = 1

<alt::
if x {
   sendinput {lalt down}
   x--
}
else {
   sendinput {lalt up}
   x++
}
return
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
animeaime



Joined: 04 Nov 2008
Posts: 1045

PostPosted: Sat Jul 04, 2009 7:40 am    Post subject: Reply with quote

How about this?

Code:
<alt::SendInput, % "{LAlt " (X := !X ? "Down" : "Up") "}"


Yes, they are identical.
_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.
Back to top
View user's profile Send private message Send e-mail
YMP



Joined: 23 Dec 2006
Posts: 418
Location: Russia

PostPosted: Sat Jul 04, 2009 10:56 am    Post subject: Reply with quote

They are not identical, your code works only once. It lacks a pair of parentheses:
Code:

<alt::SendInput, % "{LAlt " ((X := !X) ? "Down" : "Up") "}"
Back to top
View user's profile Send private message
Bobspanky



Joined: 04 Jul 2009
Posts: 3

PostPosted: Sat Jul 04, 2009 3:26 pm    Post subject: Reply with quote

Thanks everyone both scripts work perfectly!


x = 1

<alt::
if x {
sendinput {lalt down}
x--
}
else {
sendinput {lalt up}
x++
}
return

works 100%
and


<alt::SendInput, % "{LAlt " ((X := !X) ? "Down" : "Up") "}"
this is the one with extra parenthesis works 100%

very much appreciated.

Would it be possible to make this work for the "c" key and also include a thing to where if I press the left ctrl, the spacebar or shift that it comes out of the "c toggle" state to the c up position. This way when I am crouched if I go to sprint or lay prone or stand I do not first have to press c to untoggle crouch that it will just exit out of the toggle state. That way the game will perform exactly like all the other games usually do. Thanks again for such fast responses and help.
Back to top
View user's profile Send private message
YMP



Joined: 23 Dec 2006
Posts: 418
Location: Russia

PostPosted: Sat Jul 04, 2009 5:33 pm    Post subject: Reply with quote

Not sure if this is exactly what you need, but try:
Code:

$c:: SendInput, % "{c " ((Y := !Y) ? "Down" : "Up") "}"

~Shift::
~LCtrl::
~Space::
  SendInput, {c Up}
  Y =
Return
Back to top
View user's profile Send private message
Bobspanky



Joined: 04 Jul 2009
Posts: 3

PostPosted: Sat Jul 04, 2009 8:27 pm    Post subject: Reply with quote

$c:: SendInput, % "{c " ((Y := !Y) ? "Down" : "Up") "}"

~Shift::
~LCtrl::
~Space::
SendInput, {c Up}
Y =
Return

works exactly how I wanted thanks once again!
Back to top
View user's profile Send private message
animeaime



Joined: 04 Nov 2008
Posts: 1045

PostPosted: Sat Jul 04, 2009 8:36 pm    Post subject: Reply with quote

YMP wrote:
They are not identical, your code works only once. It lacks a pair of parentheses:
Code:

<alt::SendInput, % "{LAlt " ((X := !X) ? "Down" : "Up") "}"

Wow, thanks. I never knew the ternary operation had higher priority than assignment. However, it seems this holds true in other languages as well. Thanks a ton!
_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.
Back to top
View user's profile Send private message Send e-mail
obliterationn



Joined: 22 May 2011
Posts: 28

PostPosted: Sat Dec 10, 2011 12:41 am    Post subject: Reply with quote

Bobspanky wrote:


<alt::SendInput, % "{LAlt " ((X := !X) ? "Down" : "Up") "}"


Cool! Thanks for this, just what I was looking for
Back to top
View user's profile Send private message
Display posts from previous:   
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