How to: assign two functions to a key based on tap/hold?
#1
Lasiurus
Posted 29 June 2012 - 02:04 AM
- Launch a specific app (a game, in this case)
- Tapping SPACE bar = "G"
- Holding SPACE bar = "SPACE bar"
- Tapping "Right Mouse Button" = "Ctrl"
- Holding "Right Mosue Button" = holding "Right Mouse Button"
- When the app closes, the script closes itself aswell
Thanks in advance, any help is appreciated!
#2
Posted 29 June 2012 - 02:59 AM
#ifwinactive ahk_class Notepad
Run, Notepad,,, OutputVarPID
WinWait, ahk_pid %OutputVarPID%
WinWaitClose, ahk_pid %OutputVarPID%
ExitApp
$space::
Keywait space
if (a_timesincethishotkey <300)
msgbox tap
else
msgbox hold
return
#3
Posted 29 June 2012 - 03:11 AM
Script removed, use code below.
#4
Guests
Posted 29 June 2012 - 04:12 AM
I'm looking at the code trying to sort of reverse engineer it out to understand how to work this kind of scripting by myself, thought honestly, after reading throught it, I feel it completely escapes my understanding! :shock: :lol:
In any case, I tried it out, I replaced the third line with the install path of the game (I assume it ahould include the game's .exe in it?)
However, after double clicking it, it throws the following error:
Error at line 13.
Line Text: #IfWinActive(WinPID)
This line does not contain a recognized action
I tried adding the game's window code (after using AU3_Spy) but it threw the same error.
#5
Posted 29 June 2012 - 06:26 AM
#6
Posted 29 June 2012 - 06:29 AM
Script removed, use code below
#7
Posted 29 June 2012 - 06:15 PM
#SingleInstance Force
YourGamesLocation := ""
StringReplace, YourGamesLocation, YourGamesLocation, \, \, UseErrorLevel
YourGame := SubStr(YourGamesLocation, InStr(YourGamesLocation, "\", True, 1, ErrorLevel)+1)
YourGamesLocation := SubStr(YourGamesLocation, 1, InStr(YourGamesLocation, "\", True, 1, ErrorLevel) - 1)
SetTimer, SeeIfExist, 3000
;Launch a specific app (a game, in this case)
Run, %YourGame%, %YourGamesLocation%, Max, WinPID
#If WinActive("ahk_pid " WinPID)
$Space::
TimeHeld:=0
KeyWait, Space
;Holding SPACE bar = "SPACE bar"
If (TimeHeld > 25)
Send, {Space}
;Tapping SPACE bar = "G"
Else
Send, G
Return
$RButton::
TimeHeld:=0
While GetKeyState("Space", "P")
{
++TimeHeld
Sleep, 10
If (TimeHeld = 26)
{
Overage := True
Send, {RButton Down}
}
}
;Holding "Right Mosue Button" = holding "Right Mouse Button"
If (Overage)
Send, {RButton Up}
;Tapping "Right Mouse Button" = "Ctrl"
Else
Send, {Ctrl}
SeeIfExist:
;When the app closes, the script closes itself aswell
If !WinExist("ahk_pid " WinPID)
ExitApp
Return
#8
Posted 29 June 2012 - 07:10 PM
It is not working for you because you aren't running AHK_L, which i highly recommend. If you refuse, there are workarounds, yet it would be redundant. Download _L here http://l.autohotkey....y_L_Install.exe, paste this code, assign the one variable, then run...
#SingleInstance Force YourGamesLocation := "" StringReplace, YourGamesLocation, YourGamesLocation, \, \, UseErrorLevel YourGame := SubStr(YourGamesLocation, InStr(YourGamesLocation, "\", True, 1, ErrorLevel)+1) YourGamesLocation := SubStr(YourGamesLocation, 1, InStr(YourGamesLocation, "\", True, 1, ErrorLevel) - 1) SetTimer, SeeIfExist, 3000 ;Launch a specific app (a game, in this case) Run, %YourGame%, %YourGamesLocation%, Max, WinPID #If WinActive("ahk_pid " WinPID) $Space:: TimeHeld:=0 KeyWait, Space ;Holding SPACE bar = "SPACE bar" If (TimeHeld > 25) Send, {Space} ;Tapping SPACE bar = "G" Else Send, G Return $RButton:: TimeHeld:=0 While GetKeyState("Space", "P") { ++TimeHeld Sleep, 10 If (TimeHeld = 26) { Overage := True Send, {RButton Down} } } ;Holding "Right Mosue Button" = holding "Right Mouse Button" If (Overage) Send, {RButton Up} ;Tapping "Right Mouse Button" = "Ctrl" Else Send, {Ctrl} SeeIfExist: ;When the app closes, the script closes itself aswell If !WinExist("ahk_pid " WinPID) ExitApp Return
I have similar issue. This should help...
#9
Posted 29 June 2012 - 07:40 PM
Or, what I think might be the real problem, the game has two separate windows which run in parallel whenever you play. The PlayMaxPayne3.exe, which MUST be executed in order for IT to launch the game's actual .exe: MaxPayne3.exe.
Now I'm not sure whether the script takes taht into account. But if it does, then I'd need to ask for a small modification:
- The script should launch "PlayMaxPayne3.exe" and remain running until that .exe closes.
- But the windows the script should be affecting isn't the one from "PlayMaxPayne3.exe", but rather "MaxPayne3.exe", which is located in the same directory.
What should I modify in order to make it work like that?
thx.




