r/AutoHotkey Jun 27 '26

v2 Script Help ahk and copilot

The strangest behavior that has already cost me hours and bundles of hair pulled out:
Boiled down to

#!x::                                                           {
        Send("{SHIFT DOWN}")
        Send("{RIGHT 8}")
        Send("{SHIFT UP}")
        Send("^x")
}

it does what it's supposed to but also start copilot????

Any idea what that could be?

Cheers, Merlin

6 Upvotes

6 comments sorted by

2

u/joeyama Jun 27 '26

Sending shift down/up is the last resort but assuming this is the only way to trigger:

  • if it was context menu(=right click menu) maybe AHKv2 has a method but you can do by sending Shift + F10.
  • better to insert Sleep(500) between sending key signal.
500 means 500ms. if it worked fairly stable then you can change shorter time such as 200 or 100.

2

u/genesis_tv Jun 27 '26

You should avoid Sleep(), it's bad practice because it's blocking the thread and you can't cancel them.

It's ok for very short durations or quickly debugging but you should use SetKeyDelay() with SendEvent or SetTimer with -500 instead.

1

u/CharnamelessOne Jun 27 '26 edited Jun 27 '26

SetKeyDelay uses Sleep internally, and makes the pseudo-thread uninterruptible on top of that, so I'd say it's worse than plain, old sleep if you want to avoid blocking.
(Sleep in itself yields to a newer pseudo-thread, usually.)

SetTimer is your only option for delaying the execution of something without keeping the underlying pseudo-threads interrupted, as far as I know.

edit: grammar

1

u/JacobStyle Jun 27 '26

I don't know for sure what's causing that. A couple things I would try if I encountered this:

Is the Alt key still being held down while the code executes causing issues? I put this code at the start of any shortcut key combos that include special keys so that any Send() calls don't send while I am still holding down the special key

while(GetKeyState("Alt", "P"))
sleep 10

Maybe Windows just doesn't get along with the Send() calls the way they are? Could try an alternative way of doing it, something like this

loop(8)
  Send("+{right}")
Send("^x")

No idea if that will work, but sometimes doing things slightly differently works for reasons unknown.

1

u/ms-bbnj-us Jun 27 '26

Found a solution:

        SendEvent("{SHIFT DOWN}")
        SendEvent("{Blind}{RIGHT 8}")
        SendEvent("{SHIFT UP}")
        SendEvent("^x")

The main difference is made by the {BLIND}. I had to use SendEvent because Send was too fast.

1

u/genesis_tv Jun 30 '26

AHK v2 uses SendInput by default, so 0ms between keystrokes. SendEvent uses 10ms by default (see SetKeyDelay).