Skip to content

Commit

Permalink
fixed notification option in menu being always checked at start
Browse files Browse the repository at this point in the history
made all notification options available in menu
reworked menu toggle detection
replaced some subroutines with functions
added startup parameters to readme.md
  • Loading branch information
lightproof committed Jun 8, 2023
1 parent 54ce201 commit d039457
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 88 deletions.
215 changes: 130 additions & 85 deletions UAC-Focus.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,44 @@
;
;
; Startup parameters:
; -notify - display notification when UAC window gets focused by the script
; -notifyall - also display notification when UAC window appears already focused
; -notify start with "Notify on focus" option enabled
; -notifyall start with "Notify on everything" option enabled
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


#SingleInstance Prompt
#SingleInstance Force


; Vars
version = v0.5.0
appname = UAC-Focus by lightproof
repo = https://github.com/lightproof/UAC-Focus
RunMode = silent
Version = v0.5.2
App_Name = UAC-Focus by lightproof
App_Icon = %A_ScriptDir%/assets/icon.ico
global Repo = "https://github.com/lightproof/UAC-Focus"

if not RunMode = silent
notify_opt = 1
Else
notify_opt = 0


; Set app icon
IfExist, %App_Icon%
Menu, Tray, Icon, %App_Icon%

; Command line arguments
arg = %1%

if arg = -notify
RunMode = notify

if arg = -notifyall
RunMode = notifyall
; Set notification levels
Arg = %1%

Notify_Lvl = 0 ; default

if Arg = -notify
Notify_Lvl = 1

; App icon
appicon = %A_ScriptDir%/assets/icon.ico
if Arg = -notifyall
Notify_Lvl = 2

IfExist, %appicon%
Menu, Tray, Icon, %appicon%

Lvl_Name_0 = Never
Lvl_Name_1 = On focus
Lvl_Name_2 = On everything



Expand All @@ -58,52 +58,69 @@


; Elevation check
if not A_IsAdmin
{
Gosub Elevation_check

try
{

if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" %arg% /restart

else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" %arg%
; Messagebox Text
AboutText =
(LTrim
%App_Name% %Version%

} catch {
An AutoHotKey script that automatically focuses UAC window for quick control with keyboard shortcuts.

MsgBox, 48, UAC-Focus, The program needs to be run as Administrator!
%Repo%
)

}

ExitApp
HelpText =
(LTrim
Startup parameters:

}
-notify Start with "Notify on focus" option enabled. This will display notification each time the UAC window gets focused.

-notifyall Start with "Notify on everything" option enabled. Same as above, but also display notification if the UAC window has been already focused by the OS.
)



; Tray menu

Menu, Tray, Click, 2
Menu, Tray, Nostandard

Menu, Tray, Add, &About, About
Menu, Tray, Default, &About
Menu, Tray, Add

Menu, OptionID, Add, %Lvl_Name_0%, Notify_Toggle
Menu, OptionID, Add, %Lvl_Name_1%, Notify_Toggle
Menu, OptionID, Add, %Lvl_Name_2%, Notify_Toggle
Menu, Tray, Add, &Notify, :OptionID

Menu, Tray, Add, Notify on focus, Notify_Toggle

If Notify_Opt = 1
Menu, Tray, Check, Notify on focus

Menu_item_name := Lvl_Name_%Notify_Lvl%
Menu, OptionID, Check, %Menu_item_name%

Menu, Tray, Add
Menu, Tray, Add, &Open file location, Open_Location



Open_Location()
{
run, explorer %A_ScriptDir%
}


Menu, Tray, Add, &Help, Help_Msg

Menu, Tray, Add
Menu, Tray, Add, E&Xit, Quit
Menu, Tray, Add, E&xit, Exit


Exit()
{
ExitApp
}



; Main detect and refocus loop
Expand All @@ -117,14 +134,14 @@

WinActivate

if (RunMode = "notify" or RunMode = "notifyall")
if (Notify_Lvl = "1" or Notify_Lvl = "2")
TrayTip, UAC-Focus, Window focused, 3, 1

}
else
Else
{

if RunMode = notifyall
if Notify_Lvl = 2
TrayTip, UAC-Focus, Already in focus, 3, 1

}
Expand All @@ -136,84 +153,112 @@


; Subroutines
; -------------------------------------
Set_Tray_Tooltip:
if RunMode = silent
Loop, 3
{
Menu, Tray, Tip, UAC-Focus %version%
} Else {
Menu, Tray, Tip, UAC-Focus %version%`n( mode: %RunMode% )
Indx = %A_Index%
Indx := Indx - 1

Menu_item_name := Lvl_Name_%Indx%

if Notify_Lvl = %Indx%
Menu, Tray, Tip, UAC-Focus %Version%`nNotify: %Menu_item_name%
}
return



Elevation_check:
if not A_IsAdmin
{

try
{

if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" %Arg% /restart

else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" %Arg%

}
catch
{

MsgBox, 0x30, UAC-Focus, The program needs to be run as Administrator!
ExitApp

}

}
return



Notify_Toggle:
Menu, Tray, ToggleCheck, Notify on focus
Loop, 3
{
Indx = %A_Index%
Indx := Indx - 1

notify_opt := !notify_opt
Menu_item_name := Lvl_Name_%Indx%

if notify_opt = 1
RunMode = notify
Else
RunMode = silent

if A_ThisMenuItem = %Menu_item_name%
{
Menu, OptionID, Check, %Menu_item_name%
Notify_Lvl = %Indx%
}
else
{
Menu, OptionID, Uncheck, %Menu_item_name%
}
}

Gosub Set_Tray_Tooltip
return



Help_Msg:
MsgBox, 64, %appname%,
(LTrim
Startup parameters:

-notify -- Enables "Notify on focus" option by default. The program will display notification each time the UAC window gets focused.

-notifyall -- Same as above, but also displays notification if the UAC window have been already focused by the OS.
)
IfWinNotExist, Help ahk_class #32770
{
MsgBox, 0x20, Help, %HelpText%
}
return



About:
OnMessage(0x53, "WM_HELP")
Gui +OwnDialogs

SetTimer, Button_Rename, 10

MsgBox, 0x4040, About,
(LTrim
%appname% %version%

An AutoHotKey script that automatically focuses UAC window for quick control with keyboard shortcuts.
IfWinNotExist, About ahk_class #32770
{
MsgBox, 0x4040, About, %AboutText%

%repo%
)
}


WM_HELP() {
Gosub, GitHub
WM_HELP()
{
run, %Repo%
WinClose, About ahk_class #32770
}
return



Button_Rename:
IfWinNotExist, About aHK_class #32770
IfWinNotExist, About ahk_class #32770
return

SetTimer, Button_Rename, Off
WinActivate
ControlSetText, Button2, &GitHub
return


GitHub:
run, %repo%
return


Open_Location:
run, explorer %A_ScriptDir%
return


Quit:
ExitApp
15 changes: 12 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

### ABOUT

This is a simple AutoHotKey script that makes it easier to control User Account Control (UAC) prompts with the use of keyboard shortcuts (`Alt`+`Y` / `Alt`+`N`)[^1]. It have been tested to work on Windows 10 21H2, but should probably also work on Windows 11, 8/8.1 and maybe even 7 or Vista.
This is a simple AutoHotKey script that makes it easier to control User Account Control (UAC) prompts with the use of keyboard shortcuts (<kbd>Alt</kbd>+<kbd>Y</kbd> / <kbd>Alt</kbd>+<kbd>N</kbd>)[^1]. It have been tested to work on Windows 10, but should probably also work on Windows 11, 8/8.1 and maybe even 7 or Vista.

#### Rationale:

By default Windows UAC is set up to run on a secure (dimmed) desktop. Consequently, the UAC prompt window is always in focus when it appears, and is easy to control using the keyboard. If, however, UAC is set up to run on a regular desktop, the UAC Prompt window doesn't always come up in focus, making it inconvenient to control with the keyboard, forcing the user to explicitly switch to UAC window first using either mouse or `Alt`+`Tab`.
By default Windows UAC is set up to run on a secure (dimmed) desktop. Consequently, the UAC prompt window is always in focus when it appears, and is easy to control using the keyboard. If, however, UAC is set up to run on a regular desktop, the UAC Prompt window doesn't always come up in focus, making it inconvenient to control with the keyboard, forcing the user to explicitly switch to UAC window first using either mouse or <kbd>Alt</kbd>+<kbd>Tab</kbd>.

#### Solution:

Expand All @@ -18,4 +18,13 @@ Run the script as Administrator.

The UAC prompt host process `consent.exe` is run by `NT Authority\System` user and cannot be accessed by a non-elevated process for security reasons. Therefore the script must be run with Administrator privileges to work and it will prompt for them upon start when run without elevation.

[^1]: this is a default for a non-localized (i.e. english) Windows UI. If the UI is in another language, the letter corresponding to each option can be found out by pressing `Alt` twice while UAC window is open.
### Startup parameters:
`-notify`

Start with "Notify on focus" option enabled. This will display notification each time the UAC window gets focused.

`-notifyall`

Start with "Notify on everything" option enabled. Same as above, but also display notification if the UAC window has been already focused by the OS.

[^1]: this is a default for a non-localized (i.e. english) Windows UI. If the UI is in another language, the letter corresponding to each option can be found out by pressing <kbd>Alt</kbd> twice while UAC window is open.

0 comments on commit d039457

Please sign in to comment.