AHK

Ask gaming related questions (AHK v1.1 and older)
bryantan179
Posts: 74
Joined: 29 Mar 2017, 01:55

AHK

11 Dec 2017, 06:25

Let's say i have 3 scripts , A, B and C. the Hotkey for A and B are Lctrl and Lshift respectively . For script C, the hotkey is Capslock. I want the script C to loop {Lctrl down}{Lctrl up} and also {Lshift down}{Lshift up}.which means when i hold/ click capslock , both script A and B will be activated.is this possible ? the reason i dont combine the 2 script is because both scripts are working well with the suitable delay but if i combine them together ,the delay will not be suitable for my game.its more convenient to use 1 button than 2 button.

here are the 2 scripts :
$*Lctrl::
Loop
{
if not GetKeyState("Lctrl","P")
break
Send {Space down}
Send {Space up}
Send {s down}
Send {Space down}
Send {s up}
Send {Space up}
}



$*Lshift::
Loop
{
if not GetKeyState("Lshift","P")
break
Send {2 down}
Send {2 up}
Send {a down}
}
bryantan179
Posts: 74
Joined: 29 Mar 2017, 01:55

Re: AHK

12 Dec 2017, 01:31

or is it possible for 2 or more scripts to use the same hotkey ? how ?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK

12 Dec 2017, 02:43

- Hotkeys can use tilde, ~, to make them not consume the key press that triggered the hotkey, that way both scripts can receive the hotkey, but that may be undesirable for your script.
- You could have a hotkey subroutine in Script A that uses PostMessage to send a message to Script B, Script B receives that message using OnMessage, and then a hotkey subroutine can be launched. You would need to set DetectHiddenWindows to On, so that Script A could find the main window of Script B.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
bryantan179
Posts: 74
Joined: 29 Mar 2017, 01:55

Re: AHK

12 Dec 2017, 03:03

o.o . i dont understand. im just a total newbie in this . so i should just add ~infront of $*Lctrl:: and $*Lshift:: ? Like this ~$*Lctrl:: ? if i do it this way ,will the 2 scripts combine and make the script slower because more input , longer it takes.
bryantan179
Posts: 74
Joined: 29 Mar 2017, 01:55

Re: AHK

14 Dec 2017, 11:01

so it works with 2 separate scripts ~$*capslock:: and $*capslock:: . but how can i put those 2 in 1 script ? i tried and it says duplicated hotkey.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: AHK

14 Dec 2017, 11:27

bryantan179 wrote:or is it possible for 2 or more scripts to use the same hotkey ? how ?
Yes, it is. Here's combining your A and B scripts. Note that I add return to STOP the flow of the hotkey. Without it, when you release the LCtrl key, it would Break from the Loop and then jump to the next command: The LShift Loop. Which wouldn't necessarily be a problem since you check the KeyState of LShift which would in turn Break from the second Loop.

Code: Select all

$*Lctrl::
Loop
{
if not GetKeyState("Lctrl","P")
break
Send {Space down}
Send {Space up}
Send {s down}
Send {Space down}
Send {s up}
Send {Space up}
}
return


$*Lshift::
Loop
{
if not GetKeyState("Lshift","P")
break
Send {2 down}
Send {2 up}
Send {a down}
}
return
Have you read the tutorial?

Though, this may change behavior because AHK is not multi-threaded. One Hotkey is going to be active at a time in a single script. If you press and hold LCtrl, then press LShift, then you'll only see "2" (and "a") being sent. You don't see Space or "s". So you may not want to combine hotkeys, as ultimately you need to rework the logic to put them into one hotkey if you were to try to reduce yourself down to a single script. Which is what I do at the next bolded section. (The following was written before I made this realization and wrote this paragraph well above it):
bryantan179 wrote:so it works with 2 separate scripts ~$*capslock:: and $*capslock:: . but how can i put those 2 in 1 script ? i tried and it says duplicated hotkey.
Can you share the code? Is there a difference between your two capslock scripts? From your original explanation, I see no reason you need two variations of the CapsLock hotkey.

OK, so, your question has a lot larger of an answer than what you want.

Yes, you can do what you want by using a separate script to control the LShift/LCtrl automated presses. However, you may need to use the SendLevel command. And you'll want to get rid of the $ modifier on your hotkeys -- do you know why you've even put the $ modifier on there? It's purpose is to prevent AHK from activating that same hotkey. But then even further, your hotkey is checking the GetKeyState() of LCtrl/LShift physical state, something AHK isn't going to change. So just use GetKeyState("LShift") and same for LCtrl.

After making those changes, you may as well put all 3 hotkeys into the same script.

However, I don't recommend using a hotkey to artificially press a key to activate a hotkey. It's, cumbersome and very roundabout.

I ultimately would recommend using SetTimer to run the hotkeys' actions (hotkeys are also labels). It really depends on how you set it up... I would recommend using SetTimer, $*LShift, -1 and SetTimer, $*LCtrl, -1 in sequence (if you decide to keep the $ or not, make sure the timer uses the accurate label). Then you can use KeyWait to wait for the CapsLock key to be released, and turn off the Timers with SetTimer again. However, this alone won't work, because we're not pressing LCtrl/LShift at all -- real or fake. So we need to change the If logic to include a check for if the CapsLock key is (physically) pressed. (This is separate from Toggle, which applies to CapsLock, NumLock, and ScrollLock specially.) But, even beyond that, because we're on a timer, this could effectively replace the Loop you're doing... In fact, it would probably be neccessary.

Alright, so here's my ultimate recommendation:

You want to produce artificial keystrokes depending on if you press LCtrl or LShift and do both sets if you press CapsLock. You can rework your If logic to be "positive" and take an action if a key is pressed. You can also stack hotkeys to run the same code. In this code, we use the || (OR) operator.

Code: Select all

$*LCtrl::
$*LShift::
$*CapsLock::
Loop
{
If GetKeyState("LCtrl","P") || GetKeyState("CapsLock","P")
{
Send {Space down}
Send {Space up}
Send {s down}
Send {Space down}
Send {s up}
Send {Space up}
}
If GetKeyState("LShift","P") || GetKeyState("CapsLock","P")
{
Send {2 down}
Send {2 up}
Send {a down}
}
If not (GetKeyState("LCtrl","P") || GetKeyState("LShift","P") || GetKeyState("CapsLock","P") ; if all three GetKeyState() come back as false (none are pressed), then the not flips the whole expression to true, which makes us break:
break
}
return
The code is untested, so it may not work, in which case I apologize.

This code may not work as you'd like either, because it is doing alternation between Space/s and 2/a, which could behave differently from two separate scripts. But if you have to go back to two scripts, you can see how you might work in the logic to have a CapsLock hotkey on both and check the KeyState for that too.
bryantan179
Posts: 74
Joined: 29 Mar 2017, 01:55

Re: AHK

14 Dec 2017, 11:57

that script doesnt work. when i hold capslock and other button,it stops working.i just want these 2 scripts to have the same hotkey without combining the 2 scripts which makes the script even slower because there are more input.these 2 are the scripts that i use

#MaxHotkeysPerInterval 9900000000000
#HotkeyInterval 9900000000000
$*capslock::
Loop
{
if not GetKeyState("capslock","P")
break
Send {Space down}
Send {Space up}
Send {s down}
Send {Space down}
Send {s up}
Send {Space up}
}



and



#MaxHotkeysPerInterval 9900000000000
#HotkeyInterval 9900000000000
~$*capslock::
Loop
{
if not GetKeyState("capslock","P")
break
Send {2 down}
Send {2 up}
}

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: sofista and 146 guests