Skip to content

Instantly share code, notes, and snippets.

@deitrix
Last active March 30, 2026 12:37
Show Gist options
  • Select an option

  • Save deitrix/8be36689bebb65421d535efb73b7ae4e to your computer and use it in GitHub Desktop.

Select an option

Save deitrix/8be36689bebb65421d535efb73b7ae4e to your computer and use it in GitHub Desktop.
Volume and Twinkle Tray with mouse scroll
#Requires AutoHotkey v2.0
#SingleInstance Force
; --- Configuration ---
EdgeThickness := 15
DisplayStep := 5
VolumeStep := 2
OSDTimeout := -1000
CommitDelay := -250 ; Wait 250ms after last scroll to update monitor
; --- Global State ---
TargetBrightness := 50 ; AHK will track this internally
; --- OSD Setup ---
MyGui := Gui("-Caption +AlwaysOnTop +ToolWindow")
MyGui.BackColor := "1A1A1A"
MyProgress := MyGui.Add("Progress", "w200 h15 c0078D7 Background333333", TargetBrightness)
MyText := MyGui.Add("Text", "xp+210 yp cWhite w40", TargetBrightness . "%")
MyGui.SetFont("s10", "Segoe UI")
; --- Hotkeys ---
#HotIf IsMouseAtBottom(EdgeThickness)
WheelUp:: ShowVolumeOSD(1)
WheelDown:: ShowVolumeOSD(-1)
#HotIf
#HotIf IsMouseAtTop(EdgeThickness)
WheelUp:: UpdateBrightness(DisplayStep)
WheelDown:: UpdateBrightness(-DisplayStep)
#HotIf
; --- Logic ---
UpdateBrightness(Amount) {
global TargetBrightness
; 1. INSTANT UI UPDATE (Zero Latency)
TargetBrightness := Max(0, Min(100, TargetBrightness + Amount))
MyProgress.Value := TargetBrightness
MyText.Value := TargetBrightness . "%"
MouseGetPos(&x, &y)
MyGui.Show("x" . (A_ScreenWidth/2 - 125) . " y" . (y > A_ScreenHeight/2 ? y-60 : y+40) . " NoActivate")
; 2. RESET COMMIT TIMER
; Every scroll "kicks" the timer down the road.
; It only fires when you stop for 250ms.
SetTimer(CommitToHardware, CommitDelay)
; Keep OSD visible while scrolling
SetTimer(() => MyGui.Hide(), OSDTimeout)
}
CommitToHardware() {
global TargetBrightness
static hMonitor := 0
try {
if !hMonitor
hMonitor := GetMonitorHandle()
; Only one command sent at the very end of your scroll action
DllCall("dxva2\SetVCPFeature", "Ptr", hMonitor, "Char", 0x10, "UInt", TargetBrightness)
DllCall("dxva2\SetVCPFeature", "Ptr", hMonitor, "Char", 0x12, "UInt", TargetBrightness)
}
}
ShowVolumeOSD(Dir) {
if (Dir > 0)
Send("{Volume_Up}")
else
Send("{Volume_Down}")
curVol := Round(SoundGetVolume())
MyProgress.Value := curVol
MyText.Value := curVol . "%"
MouseGetPos(&x, &y)
MyGui.Show("x" . (A_ScreenWidth/2 - 125) . " y" . (y > A_ScreenHeight/2 ? y-60 : y+40) . " NoActivate")
SetTimer(() => MyGui.Hide(), OSDTimeout)
}
; --- Hardware Helpers ---
GetMonitorHandle() {
static hPhysMon := 0
if hPhysMon
return hPhysMon
hWnd := DllCall("GetDesktopWindow", "Ptr")
hMonitor := DllCall("MonitorFromWindow", "Ptr", hWnd, "UInt", 2, "Ptr")
DllCall("dxva2\GetNumberOfPhysicalMonitorsFromHMONITOR", "Ptr", hMonitor, "UInt*", &num := 0)
physStruct := Buffer(A_PtrSize + 256, 0)
DllCall("dxva2\GetPhysicalMonitorsFromHMONITOR", "Ptr", hMonitor, "UInt", num, "Ptr", physStruct)
return hPhysMon := NumGet(physStruct, 0, "Ptr")
}
IsMouseAtBottom(T) => (MouseGetPos(,&y), y >= A_ScreenHeight - T)
IsMouseAtTop(T) => (MouseGetPos(,&y), y <= T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment