3

On Windows log on, I'm starting AutoHotkey automatically with:

"C:\path\to\AutoHotkeyU64.exe" "C:\path\to\AutoHotkey.ahk"

with either a registry key in HKLM\Software\Microsoft\Windows\CurrentVersion\Run or a shortcut in C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.

Then, this AutoHotkey script intercepts my keyboard shortcut successfully, except when a program ran as administrator has the focus.

How to make AutoHotkey work even if a program ran as admin has focus?

Is it needed to run AutoHotkey as admin on startup? To do so, the only method I know is using Task scheduler - I know how to do this.

Question: for my culture of Windows 10 internals, is there another way than having to create a TaskScheduler task? I have no specific reason to avoid TaskScheduler, I just want to know if it is the only way or not, for better knowledge of Windows 10, UAC, etc.

I read about enabling/disabling AdminApprovalMode, but I don't see exactly how to apply this here. Could this be an alternative solution?

Linked: AutoHotkey Keyboard Shortcuts Not Captured When Certain Programs Are Focused

Not a dupe of How to run AutoHotKey script as an admin every time a user logs in? ; here I'm interested for non-task-scheduler solutions.

Destroy666
  • 12,350
Basj
  • 2,143

2 Answers2

4

Is it needed to run AutoHotkey as admin on startup?

You don't run AutoHotkey as admin on startup, you run the script as admin. Scripts are standalone processes that interact with the system. And it's the best solution by far due to how UAC works. Lower elevation programs have much harder time interacting with higher elevation ones.


I read about enabling/disabling Admin Approval Mode, but I don't see exactly how to apply this here. Could this be an alternative solution?

It's a bad idea regardless. Disabling a security measure just to get one script running is not a recommendation I'd give at all. AHK devs don't recommend it either.


is there another way than having to create a TaskScheduler task?

Yes. If you want for it to also work with some reload keyboard shortcut, which you haven't mentioned initially, then the script itself can attempt to relaunch itself with admin priviledges.

First, use A_IsAdmin condition around the top of the script to check if it was ran as admin. Then, as the variable description says:

To have the script restart itself as admin (or show a prompt to the user requesting admin), use Run *RunAs. However, note that running the script as admin causes all programs launched by the script to also run as admin. For a possible alternative, see the FAQ.

So basically relaunch itself elevated and exit. With something like:

Run *RunAs "%A_ScriptFullPath%" /restart

The FAQ linked above also lists another workaround:

Enable the Add 'Run with UI Access' to context menus option in AutoHotkey Setup. This option can be enabled or disabled without reinstalling AutoHotkey by re-running AutoHotkey Setup from the Start menu. Once it is enabled, launch your script file by right-clicking it and selecting Run with UI Access, or use a command line like "AutoHotkeyU32_UIA.exe" "Your script.ahk" (but include full paths).

You can use that executable with Run instead - it has the benefit of avoiding UAC popups, in case you got them enabled.

Destroy666
  • 12,350
1

If you did not install AutoHotkey as administrator, you may encounter issues when running scripts that require elevated privileges.

In that case try to re-install AutoHotkey as administrator (after uninstalling it and restarting your computer).

To run a single script in that mode in

AHK v1

add this to the auto-execute section (top of the script):

#Requires AutoHotkey v1.1

; If the script is not elevated, relaunch as administrator and kill current instance:

full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)")) { try ; leads to having the script re-launching itself as administrator { if A_IsCompiled Run RunAs "%A_ScriptFullPath%" /restart else Run RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" } ExitApp }

For AHK v2 add this:

#Requires AutoHotkey v2

; If the script is not elevated, relaunch as administrator and kill current instance

full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)")) { try ; leads to having the script re-launching itself as administrator { if A_IsCompiled Run("RunAs "" A_ScriptFullPath "" /restart") else Run("RunAs "" A_AhkPath "" /restart "" A_ScriptFullPath """) } ExitApp() }

Relax
  • 3,785