toggle itself

Post gaming related scripts
Evil-e
Posts: 262
Joined: 04 Sep 2018, 11:09

toggle itself

29 Oct 2018, 16:02

I am playing Borderlands 2 and mapped the tilde key to inventory, but requires another key to close it. I am looking to open / close inventory with
the same key for convenience. My experience with similar scripts is to change toggle to hold or toggle between two keys. I tried the latter
here, but the tilde cycles between the two keys

In the famous words of Roger Waters & Bob Ezrin in The Wall's "The Trial".... this will not do :thumbdown:

------------------------------------------------------------------------------
`::
send {i down}<--------------------------------------------------------------- inventory mapped to "i" key
sleep, 20
send {i up}
keywait, ``
send {i down}{send i up}
return
-----------------------------------------------------------------------------
I have tried the above, along with a whole bunch of variations and does open the inventory, but does nothing when it is keyed again.

Please help me Obi Wan..... your my only hope :morebeard:

UPDATE

I found a script that works perfectly....... (credit goes to Xtra for giving it to me :thumbup: )
------------------------------------------------------------------------------------------------
`::SendToggler("i","esc")<----------------------------"i" is mapped to inventory screen and "ESC" is needed to close inventory screen

SendToggler(key1,key2,dur:=100)
{
static key
key := key=key1 ? key2 : key1
Send, {%key% Down}
Sleep dur
Send, {%key% Up}
}
return
---------------------------------------------------------------------------------------------------
Now that I have this working, I would like to do the same for "Map" & "Log" keys that work in exactly the same way as
the "inventory" screen. Unfortunately, duplicating this script creates an error by way of duplicate lines and AHK cannot be executed.

any thoughts on how to make this code work for 3 separate keys? :morebeard:
I have a bit of experience opening and sending commands to game console and CMD.exe... just ask :)
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: toggle itself

03 Nov 2018, 04:28

As i understood you, and based on the 'partial' solution you said you found,you wish to toggle a keybinding for a game that otherwise requires a the key to be held.

As an example here's the script i use for Project Reality, it basically allows toggling crouch using the same key,which otherwise needs to be held,as there is no toggle crouch option.

Code: Select all

#SingleInstance, force
#NoTrayIcon

; Register a function to be called on exit:
OnExit("ReleaseModifiers")

;In Project Reality Ctrl should be held for crouch with no toggle option,this hotkey makes ctrl toggle crouch instead...
#IfWinActive, ahk_exe PRBF2.exe
Ctrl::HotkeyToggle()	;toggles up/down state of paired hotkey
#IfWinActive

HotkeyToggle(key:=""){	;for use with games with no option to toggle,requiring key be held...
	Static
	key := key ? key : A_ThisHotkey		;toggle the hotkey itself. instead of another specified key...
	toggle%key% := !toggle%key% ? 1 : 0
	If toggle%key%
		Send {%key% Down}
	Else
		Send {%key% Up}
}



;only important when modifiers such as ctrl are toggled,so that when script is terminated key's are released...
ReleaseModifiers(){
	Send {Ctrl Up}
	Send {Shift Up}
	Send {Alt Up}
	Send {LWin Up}
	Send {AppsKey Up}
}

For your purposes:

Code: Select all


`::HotkeyToggle("i")	;toggles the inventory key 'i' with current hotkey
m::HotkeyToggle()	;given map hotkey is 'm' it toggles it self.

HotkeyToggle(key:=""){	;for use with games with no option to toggle,requiring key be held...
	Static
	key := key ? key : A_ThisHotkey		;toggle the hotkey itself. instead of another specified key...
	toggle%key% := !toggle%key% ? 1 : 0
	If toggle%key%
		Send {%key% Down}
	Else
		Send {%key% Up}
}

Cheers...
live ? long & prosper : regards
Evil-e
Posts: 262
Joined: 04 Sep 2018, 11:09

Re: toggle itself

03 Nov 2018, 17:31

CyL0N wrote:
03 Nov 2018, 04:28
As i understood you, and based on the 'partial' solution you said you found,you wish to toggle a keybinding for a game that otherwise requires a the key to be held.
Not quite :)

The inventory will open with press / release of key, but requires ESC to close it. The script that I said worked perfectly on my update, proved to be incorrect. As I played the game for a while, it too would cycle between inventory & ESC fairly often.

I have a number of scripts I use to either turn toggle into hold (usually crouch & ADS) or toggle between two weapons. I am looking for
a script that will turn using two separate keys, into toggle open / close with only one.

Here is my first attempt to toggle inventory on / off with a single key
-------------------------------------------------------------------------------------------------------

`::
if key = i
Key = esc
Else
key = i
{
Send {%Key% down}
sleep, 100
Send {%Key% up}
}
return
-----------------------------------------------------------------------------------------------------

This script works great to swap between two weapons, because I always want one or the other. Unfortunately, this script, as well
as the one I posted for UPDATE, will fairly often toggle "ESC" on /off or just toggle between inventory and ESC.

At the time of my "UPDATE" it seemed to be working fine, but AHK wont launch with copies of that script. I was looking for an alternate one that could be duplicated for map & mission logs that also require ESC to close.

Does this make sense.... feels like I'm babbling :wtf:

PS> I did not write either of these script, they were found during searches.... credit wherever it is due, just not to me :)

PPS> I have a whole bunch of really cool scripts to all kinds of things in games. Multiple ways to open console and send
commands for FOV and such and then close it afterward. I got tricky and enabled the move forward key, to quickly launch
the console FOV change to avoid any other keys altogether. Anyway, just willing to pass them on, if you are interested. Most
of these I did write :)
I have a bit of experience opening and sending commands to game console and CMD.exe... just ask :)
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: toggle itself

04 Nov 2018, 22:58

I can't test this because i don't have a game with that kind of mechanics but an alternative approach as in below,is for example to press inventory key for inventory and double tap same key to exit inventory...I find toggling is unreliable for anything beyond changing discrete states...

Code: Select all

`::
   KeyWait, %A_ThisHotkey%
   KeyWait, %A_ThisHotkey%, D T.1
   If !ErrorLevel	;if double tapped, send esc
      goto, dT
	Else	;if single tapped,then send key it self
	  Send, {i down}
return

m::
   KeyWait, %A_ThisHotkey%
   KeyWait, %A_ThisHotkey%, D T.1
   If !ErrorLevel	;if double tapped, send esc
      goto, dT
	Else	;if single tapped,then send key it self
	  Send, {%A_ThisHotkey% down}
return

dT:
	Send, {Esc down}{Esc up}	;for reliability
    ;msgbox, %A_ThisHotkey% Double Tapped....
return
live ? long & prosper : regards
Evil-e
Posts: 262
Joined: 04 Sep 2018, 11:09

Re: toggle itself

07 Nov 2018, 19:12

Thanks.....just now got to this, as I had a lot on my plate for a few days.

I copy / paste your script here (the upper section with tilde as key), but get an error when I try to launch
AHK.exe file. Message says "Goto,dt" is the issue and AHK will now exit.

is there some info I need to add to this line?

:morebeard:
I have a bit of experience opening and sending commands to game console and CMD.exe... just ask :)
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: toggle itself

08 Nov 2018, 23:04

Probably code formatting got it messed up:

Code: Select all

`::	;THIS
KeyWait, %A_ThisHotkey%
KeyWait, %A_ThisHotkey%, D T.1
If !ErrorLevel	;if double tapped, send esc
	dT()
Else	;if single tapped,then send key it self
	Send, {i down}
return

i::	;OR THIS
KeyWait, %A_ThisHotkey%
KeyWait, %A_ThisHotkey%, D T.1
If !ErrorLevel	;if double tapped, send esc
	dT()
Else	;if single tapped,then send key it self
	Send, {%A_ThisHotkey% down}
return

m::
KeyWait, %A_ThisHotkey%
KeyWait, %A_ThisHotkey%, D T.1
If !ErrorLevel	;if double tapped, send esc
	dT()
Else	;if single tapped,then send key it self
	Send, {%A_ThisHotkey% down}
return

dT(){
	Send, {Esc down}{Esc up}	;for reliability
	;msgbox, %A_ThisHotkey% Double Tapped....
}

live ? long & prosper : regards
Evil-e
Posts: 262
Joined: 04 Sep 2018, 11:09

Re: toggle itself

11 Nov 2018, 09:03

Thank you :)

I did some testing with this the other day and had AHK gave error message re: dT() and tried
to understand if I was missing something, but figured I would take a few days and get back to it and try again.

This morning I tried again and found the 4th script the only one to not give me an error when I try to launch AHK.exe file...
---------------------------------------------------------------------------------------------------------------------
`::<-----------------------------------------------------------------;I remapped "inventory" to the tilde key in games menu
KeyWait, %A_ThisHotkey%
KeyWait, %A_ThisHotkey%, D T.1
If !ErrorLevel
dT()
Else
Send, {%A_ThisHotkey% down}
return

dT(){
Send, {Esc down}{Esc up}
;msgbox, %A_ThisHotkey% Double Tapped....
}
return<-------------------------------------------------------------;tried with and without return at the bottom here
---------------------------------------------------------------------------------------------------------------------

This worked exactly one time, then either cycled ESC or did not do anything :(

Thoughts?

:morebeard:
I have a bit of experience opening and sending commands to game console and CMD.exe... just ask :)
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: toggle itself

11 Nov 2018, 11:27

Kay, first it doesn't cycle anything, TAP ONCE it always sends hotkey itself,TAP TWICE it always sends Esc.

For brevity however here's a debug script,high pitch tone for double tap,lower for single tap...
The KeyPresses WILL be detected in game if you hear the tones.

Code: Select all

m::
i::
`::
KeyWait, %A_ThisHotkey%
KeyWait, %A_ThisHotkey%, D T.1
If !ErrorLevel
	dT()
Else{
	Send, {%A_ThisHotkey% down}
	SoundBeep 1000
}
return

dT(){
	Send, {Esc down}{Esc up}
	SoundBeep 5000
}

Cheers.
live ? long & prosper : regards
Evil-e
Posts: 262
Joined: 04 Sep 2018, 11:09

Re: toggle itself

11 Nov 2018, 13:23

I appreciate your persistent help on this issue :)

I loaded up the following script....
---------------------------------------------------------------------------
`::
KeyWait, %A_ThisHotkey%
KeyWait, %A_ThisHotkey%, D T.1
If !ErrorLevel
dT()
Else{
Send, {%A_ThisHotkey% down}
SoundBeep 1000
}
return

dT(){
Send, {Esc down}{Esc up}
SoundBeep 5000
}
-----------------------------------------------------------------------------

"inventory" was mapped to tilde key via Options Menu in game
I never heard any beeping after waiting until the characters stopped talking, so audio is working
The very first time I keyed "tilde" the "Inventory" came up.
I double-tapped and it went off, just as you said it should
waited a few seconds and pressed "tilde" again..... nothing.
waited a few seconds and tried pressing a split second longer.... waited then shorter.... nothing
waited and double tapped and ESC came up and DT again and ESC closed (this was consistent over 4 times in a row)
I then tried every possible option of DT slow, fast, hold split longer between, hold split shorter between...

nothing.... the "Inventory "only came up one time and was on the very first press of "tilde" key :(

I am running AHK v1.1.30.01 on Windows XP SP3 32bit

Your help is very much appreciated, but I don't want to ask anymore of you to help with this. What I already use works
90% of the time 85% of the time :HeHe:

Of the 4 or 5 misc. things I would like to address on AHK, this is on the bottom of the list, so no biggie. I thank you your
diligence in trying to get this working for me, but I am going to let this issue go for now. Maybe when I get the other
questions resolved, I will get back to it and will have better knowledge and hopefully find a resolution.

Thanks, Eric :)

PS> if you are so inclined, any input on the following two questions would be helpful

https://autohotkey.com/boards/viewtopic.php?f=6&t=58774

https://autohotkey.com/boards/viewtopic ... 18&t=58905
I have a bit of experience opening and sending commands to game console and CMD.exe... just ask :)

Return to “Gaming Scripts (v1)”

Who is online

Users browsing this forum: No registered users and 43 guests