How to allow other script use a hotkey that hooked by this script?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
byzod
Posts: 89
Joined: 21 Jun 2021, 06:46

How to allow other script use a hotkey that hooked by this script?

Post by byzod » 30 Nov 2023, 09:54

I have a v2 script A which blocks annoying F1 help of windows explorer

Code: Select all

; prevent explorer F1
#HotIf WinActive("ahk_exe explorer.exe")
{
	F1::
	{
		; Suspend
	}
}
#HotIf
I have another v1 script B which runs as compiled exe that use F1 as one of its function (specifically, it's a macro recorder that has no v2 version yet)

Code: Select all

F1::
  msgbox 1
Return
Then F1 of this script is not working when explorer actived
ofc I can change the hotkey to F2 for example, but it used F2~F6 already so it will have to shift them all

I wonder if there is any way to allow script B to use F1 on explorer while still prevent default F1 behavior of explorer?

User avatar
mikeyww
Posts: 27341
Joined: 09 Sep 2014, 18:38

Re: How to allow other script use a hotkey that hooked by this script?

Post by mikeyww » 30 Nov 2023, 10:06

Figure out what you want to happen in Explorer, and then adjust your first script accordingly. Better yet, just combine the scripts to simplify your approach.

byzod
Posts: 89
Joined: 21 Jun 2021, 06:46

Re: How to allow other script use a hotkey that hooked by this script?

Post by byzod » 30 Nov 2023, 11:34

mikeyww wrote:
30 Nov 2023, 10:06
Figure out what you want to happen in Explorer, and then adjust your first script accordingly. Better yet, just combine the scripts to simplify your approach.
1. How to adjust it? That's exact my question...
2. How to combine v1 and v2 script? The v1 script is long and complicated as you can see, convert it to v2 is not an option for me

User avatar
mikeyww
Posts: 27341
Joined: 09 Sep 2014, 18:38

Re: How to allow other script use a hotkey that hooked by this script?

Post by mikeyww » 30 Nov 2023, 11:45

You can skip the first script and just run the second one.

byzod
Posts: 89
Joined: 21 Jun 2021, 06:46

Re: How to allow other script use a hotkey that hooked by this script?

Post by byzod » 01 Dec 2023, 02:26

mikeyww wrote:
30 Nov 2023, 11:45
You can skip the first script and just run the second one.
1. the first one is part of my main script which takes me months converting it to v2, so dump it back to v1 is the last option for me
2. Since it's a macro recorder, I call it from the first script only when needed but prevent F1 for explorer is a persistent task, writing it in the second one is not helpful

User avatar
mikeyww
Posts: 27341
Joined: 09 Sep 2014, 18:38

Re: How to allow other script use a hotkey that hooked by this script?

Post by mikeyww » 01 Dec 2023, 08:26

I do not recommend running two different AutoHotkey scripts with the same hotkey. You might get unexpected or unpredictable results. You can design your second script to handle whatever you need to do when the hotkey is triggered, according to the active window.

A script can check for the presence of another script, terminate that script, run it again, etc.

byzod
Posts: 89
Joined: 21 Jun 2021, 06:46

Re: How to allow other script use a hotkey that hooked by this script?

Post by byzod » 01 Dec 2023, 23:29

mikeyww wrote:
01 Dec 2023, 08:26
I do not recommend running two different AutoHotkey scripts with the same hotkey. You might get unexpected or unpredictable results. You can design your second script to handle whatever you need to do when the hotkey is triggered, according to the active window.

A script can check for the presence of another script, terminate that script, run it again, etc.
Well you got a point

I use this now
It looks stupid but works, at least

Code: Select all

#HotIf WinActive("ahk_exe explorer.exe") && !ProcessExist("MacroRecorder2.exe")
{
	F1::
	{
		; Suspend
	}
}
#HotIf

User avatar
mikeyww
Posts: 27341
Joined: 09 Sep 2014, 18:38

Re: How to allow other script use a hotkey that hooked by this script?

Post by mikeyww » 02 Dec 2023, 06:57

You have it!

Braces below #HotIf are not needed and should be removed, as directives do not use bounding braces.

Code: Select all

#Requires AutoHotkey v2.0

#HotIf WinActive('ahk_class CabinetWClass') && !ProcessExist('MacroRecorder2.exe')
F1:: {
 ; Suspend
 MsgBox 123
}
#HotIf

byzod
Posts: 89
Joined: 21 Jun 2021, 06:46

Re: How to allow other script use a hotkey that hooked by this script?

Post by byzod » 02 Dec 2023, 22:19

mikeyww wrote:
02 Dec 2023, 06:57
You have it!

Braces below #HotIf are not needed and should be removed, as directives do not use bounding braces.

Code: Select all

#Requires AutoHotkey v2.0

#HotIf WinActive('ahk_class CabinetWClass') && !ProcessExist('MacroRecorder2.exe')
F1:: {
 ; Suspend
 MsgBox 123
}
#HotIf
It's needed
An1.gif
An1.gif (45.74 KiB) Viewed 419 times

User avatar
mikeyww
Posts: 27341
Joined: 09 Sep 2014, 18:38

Re: How to allow other script use a hotkey that hooked by this script?

Post by mikeyww » 03 Dec 2023, 06:20

I understand why the braces are there, and I do not recommend them. Someone else pointed out that if you comment the brace, it will still work with folding (collapsing of code) in the editor. I have not tested it.

My opinion is that characters in a script should not be regarded as fluff. The entire script is interpreted (perhaps aside from comments, depending on how you look at this). A brace is not an extraneous character in AHK. There are situations where such fluff can actually be interpreted unexpectedly and thus constitute a bug. Including this fluff in a script may also make the script more difficult for others to read, because when they see a brace, it has a particular meaning.

(NOT) Using braces to fold code in a text editor

Code: Select all

#Requires AutoHotkey v2.0
f(1)
{
 ; I would love to fold this code block, but it generates an error instead.
 ; If I remove the braces, the script works.
 MsgBox 999999999
}

f(x)
{
 MsgBox x
}

Post Reply

Return to “Ask for Help (v2)”