Do X if Enter is pressed in a specific window Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Rok
Posts: 63
Joined: 11 Jan 2016, 08:50

Do X if Enter is pressed in a specific window

Post by Rok » 10 Jun 2023, 14:40

Among several other hotkeys, I want to MouseMove if the Enter key is pressed within a specific window (so I used #HotIf already to check if the window is active, and AFAIK we can't nest #HotIf blocks). So, how do I do this?

I already tried, for example:

Code: Select all

#HotIf WinActive("ahk_exe Notepad.exe")

if GetKeyState("Enter", "P")
{
    MouseMove 0, 100, 50, "R"
}

#HotIf
It didn't work. And I've already been told that we can't use if contextually and we have to use #HotIf instead, despite me seeing examples on the GetKeyState help page that suggest otherwise, after having added the braces to the if statement as per the v2 If help page.

Also, what is a correct example of using if in v2 currently?

By the way, I also tried:

Code: Select all

#HotIf WinActive("ahk_exe Notepad.exe") && GetKeyState("Enter", "P")

MouseMove 0, 100, 50, "R"

#HotIf
That also didn't work. In fact, the mouse cursor just moved downward immediately upon running the script, despite me not being in Notepad and Enter having not been pressed.

User avatar
boiler
Posts: 17076
Joined: 21 Dec 2014, 02:44

Re: Do X if Enter is pressed in a specific window  Topic is solved

Post by boiler » 10 Jun 2023, 14:52

#HotIf applies to hotkeys (and hotstrings), which you need to trigger your code anyway. #HotIf alone doesn't cause code to run when its conditions are met. It determines whether a hotkey is active or not. And using If doesn't mean "whenever this condition is true, run the code below." It means, at the time this line is executed, if its condition is met, then execute the line(s) below. You have no event triggering your code whenever the Enter key is pressed. That's what hotkeys are for:

Code: Select all

#Requires AutoHotkey v2.0
#HotIf WinActive("ahk_exe Notepad.exe")
Enter::MouseMove 0, 100, 50, "R"

Rok
Posts: 63
Joined: 11 Jan 2016, 08:50

Re: Do X if Enter is pressed in a specific window

Post by Rok » 10 Jun 2023, 15:38

Thank you for explaining these things.

Regarding the code you provided (it's missing the closing #HotIf, I believe), the problem is that I do not want the default behavior of the Enter key to change; I want the Enter key to function normally and MouseMove.

Hmm. When I put it that way, the current code came to mind after adding $ to the hotkey to avoid the looping problem when a hotkey triggers itself (if I'm phrasing this correctly):

Code: Select all

#Requires AutoHotkey v2.0
#HotIf WinActive("ahk_exe Notepad.exe")
$Enter:: {
    Send "{Enter}"
    MouseMove 0, 100, 10, "R"
}
#HotIf

And it works for what I want!

Thank you for the inspiration. And it's my bad that I haven't been more specific with what I wanted to accomplish, so I'll pick your answer anyway.

Update: In case it helps anyone else who has a similar need, the following code works even better for what I wanted to accomplish because it keeps the Enter key working perfectly normally in the target window (using Notepad in this example) while only changing the behavior of Ctrl+Enter to make it simulate pressing only Enter, moving the mouse cursor downward exactly 77.5 pixels relative to the cursor's current location, and clicking:

Code: Select all

#Requires AutoHotkey v2.0
#HotIf WinActive("ahk_exe Notepad.exe")
$^Enter:: {
  Send "{Enter}"
  Sleep 25
  MouseMove 0, 77.5, 10, "R"
  Sleep 25
  Click
}
#HotIf

User avatar
boiler
Posts: 17076
Joined: 21 Dec 2014, 02:44

Re: Do X if Enter is pressed in a specific window

Post by boiler » 10 Jun 2023, 15:57

Rok wrote: Regarding the code you provided (it's missing the closing #HotIf, I believe)
No, it’s not missing anything. The blank one isn’t a close. It’s just a new one that would apply to hotkeys below it. If you wanted to apply a new/different condition with a new #HotIf on hotkeys below, then the blank one would be meaningless. If you have no hotkeys below it, it is also meaningless.

Rok wrote: the problem is that I do not want the default behavior of the Enter key to change; I want the Enter key to function normally and MouseMove.
Just put the tilde ~ modifier before the hotkey in the code I posted. That’s exactly what it’s for. No need to send an Enter when you do that, and it’s better because it’s the natural function of the key, not eating the key and then sending a virtual one.

Rok
Posts: 63
Joined: 11 Jan 2016, 08:50

Re: Do X if Enter is pressed in a specific window

Post by Rok » 29 Jun 2023, 09:28

boiler wrote:
10 Jun 2023, 15:57
Rok wrote: Regarding the code you provided (it's missing the closing #HotIf, I believe)
No, it’s not missing anything. The blank one isn’t a close. It’s just a new one that would apply to hotkeys below it. If you wanted to apply a new/different condition with a new #HotIf on hotkeys below, then the blank one would be meaningless. If you have no hotkeys below it, it is also meaningless.

Rok wrote: the problem is that I do not want the default behavior of the Enter key to change; I want the Enter key to function normally and MouseMove.
Just put the tilde ~ modifier before the hotkey in the code I posted. That’s exactly what it’s for. No need to send an Enter when you do that, and it’s better because it’s the natural function of the key, not eating the key and then sending a virtual one.
Thank you for the tip about adding the tilde, boiler. I happened to find this key double-press detection code today, which uses the tilde too, and testing it helped me understand better how it works. It is indeed a brilliant solution for some case scenarios that I can think of.

And regarding your clarification about the blank #HotIf, I understand now that ideally, I should add any "global" hotkeys toward the top of my script before any #HotIf statements specific to windows, then start adding any window-specific #HotIfs below the global hotkeys. Then, if my script is too big, I can't be bothered organizing it, and I happen to want to put any more global hotkeys between window-specific #HotIfs, then I can add a blank #HotIf just before those global hotkeys. In short, I understand from your clarification that a blank #HotIf is not a "closing statement" for the #HotIf body, as I thought, but it can mark the beginning of global hotkeys.

Post Reply

Return to “Ask for Help (v2)”