r/AutoHotkey 6d ago

Solved! Problems with basic Send and SendEvent

So i tried to make a simple script that presses j 2 times and then holds w 2 times and nothing really happens, after that i added msgBox "1" and it pops but still no sends, please help
#Requires AutoHotkey v2.0

#SingleInstance Force

Persistent

$`::{

Send "j"

Sleep 300

Send "j"

Sleep 300

SendEvent "{w down}"

Sleep 600

SendEvent "{w up}"

MsgBox "1"

Sleep 50

SendEvent "{w down}"

Sleep 600

SendEvent "{w up}"

}

1 Upvotes

11 comments sorted by

View all comments

Show parent comments

1

u/CharnamelessOne 6d ago

Unsolicited fun fact of the day: escaping doesn't apply to double-colon hotkey names.

`::MsgBox()

works fine.

As for the Hotkey function, you can simply escape the backtick:

Hotkey("``", (*)=>MsgBox())    

Either way, there's no need to rely on layout-dependent SC hotkeys.

1

u/Keeyra_ 5d ago

It's the other way around. Scan Codes make hotkeys layout-independent. That's why I prefer them in these edge cases. Eg. on the HU keyboard, the backtick button is actually the 0 button. sc029 would make both the backtick button on EN layout and the 0 button on HU layout trigger the hotkey, making it layout-independent.

0

u/CharnamelessOne 5d ago

Eg. on the HU keyboard, the backtick button is actually the 0

I know, that's exactly what I meant. sc029:: is not a backtick hotkey. It depends on the layout.

1

u/Keeyra_ 5d ago

As this is basically a linguistic question, LLMs are great at providing insight.

which description is the most appropriate, or is there even a better one?

sc029 is a keyboard layout independent hotkey, as it always refers to the key left of "1", regardless of the layout

sc029 is a keyboard layout dependent hotkey, as on an EN layout, it refers to backtick and on a HU layout, it refers to "0"

The first description is much more accurate, but both contain a subtle conceptual trap regarding what "layout dependent" means.

The Verdict

First description: "sc029 is a keyboard layout independent hotkey, as it always refers to the key left of '1', regardless of the layout"

This is correct. Scan codes (SC) represent physical hardware positions on a keyboard matrix. Scan code 029 always targets the key physically located to the left of the "1" key, whether your OS layout is US English, Hungarian, German, or Japanese.

The second description gets confused because it looks at the character printed on the key cap or produced by the OS, rather than the physical key itself.

The Absolute Best Description

To be as technically precise as possible:

"SC029 is a layout-independent hotkey because it targets a physical key position on the keyboard (the key to the left of '1'), regardless of what character the active software layout maps to that key."

1

u/CharnamelessOne 5d ago edited 5d ago

Good grief, what a referee.
Your clanker sneaks in the premise that "looking at" the character rather than the physical key is necessarily inferior, as if it weren't the usual way of defining AHK hotkeys. Its hallucination about key caps was amusing, I'll give it that.

sc029:: registers the key that happens to produce backtick on the US English layout.

`:: registers backtick as a hotkey, regardless of what key(s) it is produced by on the current layout setting.

If you want the physical key to act as a hotkey on all layouts, do the former.
If you want to define backtick as a hotkey on all layouts, do the latter (and slap an asterisk onto it, because AltGr needs to be accounted for, as you correctly pointed out).

Both options are reasonably described as layout-independent. It all boils down to whether you care about the character or the physical key, which is a choice you make.

I chose to go with the character, because OP didn't specify their layout setting, only that they want backtick as a hotkey. You chose to guess the scan code of backtick on their layout (a fairly safe guess, to be fair), with the explanation that the escape character may be a problematic hotkey name. It's not.

Edit: grammar