How to trigger hotkey conditionally for a 3rd party program

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
byzod
Posts: 87
Joined: 21 Jun 2021, 06:46

How to trigger hotkey conditionally for a 3rd party program

Post by byzod » 23 Sep 2022, 03:45

I'm working with some personal automation for some program, but most of them are not expose controls to ahk (the focused control is always blank with built-in window spy), so I have to check if it's in the right page I want to trigger the code

for example (pseudo code)

Code: Select all

#if winactive("ahk_exe chrome.exe")
1::
{
  imagesearch X,Y, a, b, c, d, "symbol-only-on-page-A.jpg"
  if(X) {
      doSomethingForPageA();
  } else {
     ; do nothing
  }
  return
}
#if
The problem is the key 1 is occupied, press 1 on pageA will run doSomethingForPageA() which is as expected, but press 1 anywhere else is not working at all, you can't input 12 yo in a forum reply box for example

A quick fix is chang 1:: to ~1::
But then another problem occurs, pressing 1 on pageA not only triggers doSomethingForPageA(), it also input a 1
If doSomethingForPageA() is press 2, 3, 4 for corresponding button, then it becomes 1, 2, 3, 4 which is wrong and may cause problems


Any help? Is it possible to trigger hotkey with custom condition(s) and won't mess with vanilla input if not applicable?
Is there something like e.prebentDefault() in JavaScript?

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: How to trigger hotkey condionally for a 3rd party program

Post by WalkerOfTheDay » 23 Sep 2022, 05:35

I think you may need

Code: Select all

#ifwinactive

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

Re: How to trigger hotkey condionally for a 3rd party program

Post by mikeyww » 23 Sep 2022, 05:46

I don't know about your last question, but you would want to check to see if your cursor is in an input field, or just use a different hotkey such as F3. The forum has some scripts to detect the former, though some are not always reliable.

Braces that bound a hotkey routine have no role in AHK v1, and should be removed.

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

Re: How to trigger hotkey condionally for a 3rd party program

Post by byzod » 24 Sep 2022, 00:45

WalkerOfTheDay wrote:
23 Sep 2022, 05:35
I think you may need

Code: Select all

#ifwinactive
As I said
most of them are not expose controls to ahk
#ifwinactive only works for different window, there's no way to tell ahk the difference of two page in the same application. Or maybe I just didn't know how, please correct me if I'm wrong




mikeyww wrote:
23 Sep 2022, 05:46
I don't know about your last question, but you would want to check to see if your cursor is in an input field, or just use a different hotkey such as F3. The forum has some scripts to detect the former, though some are not always reliable.
Some of my action is designed to working in input area or control that has focus, for example the 1,2,3 button I mentioned above, or press v to send a emoji in pageA but not in anywhere else
Change the hotkey to Fn is an option but not always applicable. F1~F7 are occupied in, for example, my browser. A key without conflict it will be far from my hand and harder to press, if possible I prefer press v to do that action instead of Num7...everyone use ahk for maximum convenience right?
Another fact is that I add many small improvements for target application in different pages with the same hotkey, feels like polymorphic hotkey or something (when in doubt, press v)
If some of them use different hotkey, it will be harder to use as I got bad memory ("press v...OK it's not working, what hotkey should I press in this page then?...")

mikeyww wrote:
23 Sep 2022, 05:46
Braces that bound a hotkey routine have no role in AHK v1, and should be removed.
Yes it has a role, for better code readability

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

Re: How to trigger hotkey condionally for a 3rd party program

Post by mikeyww » 24 Sep 2022, 06:39

You have indicated that you would like a keyboard key to behave, within one window, according to whether a form field has the keyboard focus. It seems to me that you then want to determine whether a form field has the keyboard focus. That is the nature of my previous post.

Some have used the cursor appearance (type) as a proxy, though this is not completely reliable for the purpose at hand.

You might be able to use JavaScript; would search the forum for such a solution, as there are other posts about this topic.

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

Re: How to trigger hotkey condionally for a 3rd party program

Post by byzod » 24 Sep 2022, 22:18

mikeyww wrote:
24 Sep 2022, 06:39
You have indicated that you would like a keyboard key to behave, within one window, according to whether a form field has the keyboard focus. It seems to me that you then want to determine whether a form field has the keyboard focus. That is the nature of my previous post.

Some have used the cursor appearance (type) as a proxy, though this is not completely reliable for the purpose at hand.

You might be able to use JavaScript; would search the forum for such a solution, as there are other posts about this topic.
Textarea can be detected with cursor states, but focused control won't chagne cursor states (for example, buttons)
btw what's the point to detect the focus states? Even I know it's focused, I can't prevent the default key action (when using ~)?
About the JavaScript thing, unfortunately, no. I talked to the author of Rufaydium, it's impossible to interact with the window created already by user

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

Re: How to trigger hotkey condionally for a 3rd party program

Post by mikeyww » 25 Sep 2022, 05:10

Approaches to identifying controls include Acc and UIA, scripts available on the forum. Perhaps UIA includes a way to determine keyboard focus; would search @Descolada's scripts.

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

Re: How to trigger hotkey condionally for a 3rd party program

Post by byzod » 26 Sep 2022, 04:47

mikeyww wrote:
25 Sep 2022, 05:10
Approaches to identifying controls include Acc and UIA, scripts available on the forum. Perhaps UIA includes a way to determine keyboard focus; would search @Descolada's scripts.
You mean this?
That's a long post I'll take a look at it when I got time
But what's the point to detect focus? I think it won't help prevent the default action?

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

Re: How to trigger hotkey conditionally for a 3rd party program

Post by mikeyww » 26 Sep 2022, 05:47

Yes. As I mentioned, if your goal is to act according to the control type, then you need to determine the control type.

Post Reply

Return to “Ask for Help (v1)”