Jump to content

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

Toggle key


  • Please log in to reply
8 replies to this topic
Bobspanky
  • Members
  • 3 posts
  • Last active: Jul 04 2009 03:43 PM
  • Joined: 04 Jul 2009
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!

jethrow
  • Moderators
  • 2810 posts
  • Last active:
  • Joined: 24 May 2009
How about this:
x = 1



<alt::

if x {

	sendinput {lalt down}

	x--

}

else {

	sendinput {lalt up}

	x++

}

return


animeaime
  • Members
  • 1045 posts
  • Last active: Jun 18 2011 04:44 AM
  • Joined: 04 Nov 2008
How about this?

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

Yes, they are identical.

YMP
  • Members
  • 424 posts
  • Last active: Apr 05 2012 01:18 AM
  • Joined: 23 Dec 2006
They are not identical, your code works only once. It lacks a pair of parentheses:
<alt::SendInput, % "{LAlt " ((X := !X) ? "Down" : "Up") "}"


Bobspanky
  • Members
  • 3 posts
  • Last active: Jul 04 2009 03:43 PM
  • Joined: 04 Jul 2009
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.

YMP
  • Members
  • 424 posts
  • Last active: Apr 05 2012 01:18 AM
  • Joined: 23 Dec 2006
Not sure if this is exactly what you need, but try:
$c:: SendInput, % "{c " ((Y := !Y) ? "Down" : "Up") "}"

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


Bobspanky
  • Members
  • 3 posts
  • Last active: Jul 04 2009 03:43 PM
  • Joined: 04 Jul 2009
$c:: SendInput, % "{c " ((Y := !Y) ? "Down" : "Up") "}"

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

works exactly how I wanted thanks once again!

animeaime
  • Members
  • 1045 posts
  • Last active: Jun 18 2011 04:44 AM
  • Joined: 04 Nov 2008

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

<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!

obliterationn
  • Members
  • 28 posts
  • Last active: Dec 30 2011 05:00 PM
  • Joined: 22 May 2011

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


Cool! Thanks for this, just what I was looking for