AutoHotkey Community

It is currently May 26th, 2012, 9:11 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Toggle key
PostPosted: July 4th, 2009, 5:29 am 
Offline

Joined: July 4th, 2009, 5:24 am
Posts: 3
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 4th, 2009, 7:54 am 
Offline
User avatar

Joined: May 24th, 2009, 5:35 am
Posts: 2099
Location: Iowa, USA
How about this:
Code:
x = 1

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 4th, 2009, 8:40 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 4th, 2009, 11:56 am 
Offline

Joined: December 23rd, 2006, 6:02 pm
Posts: 424
Location: Russia
They are not identical, your code works only once. It lacks a pair of parentheses:
Code:
<alt::SendInput, % "{LAlt " ((X := !X) ? "Down" : "Up") "}"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 4th, 2009, 4:26 pm 
Offline

Joined: July 4th, 2009, 5:24 am
Posts: 3
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 4th, 2009, 6:33 pm 
Offline

Joined: December 23rd, 2006, 6:02 pm
Posts: 424
Location: Russia
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 4th, 2009, 9:27 pm 
Offline

Joined: July 4th, 2009, 5:24 am
Posts: 3
$c:: SendInput, % "{c " ((Y := !Y) ? "Down" : "Up") "}"

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

works exactly how I wanted thanks once again!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 4th, 2009, 9:36 pm 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 10th, 2011, 1:41 am 
Offline

Joined: May 22nd, 2011, 8:26 pm
Posts: 28
Bobspanky wrote:

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


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


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, Exabot [Bot], krajan, patgenn123 and 57 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group