Jump to content


How to: assign two functions to a key based on tap/hold?


  • Please log in to reply
8 replies to this topic

#1 Lasiurus

Lasiurus
  • Guests

Posted 29 June 2012 - 02:04 AM

Hi, I'd like to know if it would be possible to create an ahk script that did the following:

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

None
  • Members
  • 3199 posts

Posted 29 June 2012 - 02:59 AM

This will get you started.
#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 dylan904

dylan904
  • Members
  • 706 posts

Posted 29 June 2012 - 03:11 AM

Here, you would just paste your game's location within the quotations on line 3, the rest should work itself out.
Script removed, use code below.


#4 Guests

  • Guests

Posted 29 June 2012 - 04:12 AM

First of all, thanks both of you for the quick response!

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 dylan904

dylan904
  • Members
  • 706 posts

Posted 29 June 2012 - 06:26 AM

Correction in red, i don't know what you don't understand but you may either pm me or go to webchat.freenode.com and joining channel #ahk. You may find me there. But yes, any further questions are welcome.

#6 dylan904

dylan904
  • Members
  • 706 posts

Posted 29 June 2012 - 06:29 AM

Correction in red, i don't know what you don't understand but you may either pm me or go to http://www.webchat.freenode.com and joining channel #ahk. You may find me there. But yes, any further questions are welcome.
Script removed, use code below


#7 dylan904

dylan904
  • Members
  • 706 posts

Posted 29 June 2012 - 06:15 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


#8 WittyET

WittyET
  • Members
  • 1 posts

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 Lasiurus

Lasiurus
  • Members
  • 1 posts

Posted 29 June 2012 - 07:40 PM

Ok I downloaded AHK_L installed and pasted the script. The game now launches, however, it seems the script isn't affecting the game at all. I can think of two reasons... either the game functions don't give enough time to allow two set of actions to be mapped to one key.


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.