Skip to content

Instantly share code, notes, and snippets.

@deitrix
Last active July 7, 2026 12:56
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 ---
CoordMode "Mouse", "Screen"
global ScrollControl := {
EdgeThickness: 20,
CenterWidth: 400,
DisplayStep: 5,
VolumeStep: 2,
CommitDelay: -250,
OSDOpacityActive: 220, ; Full visibility
OSDOpacityGuide: 60, ; Faint "guide" visibility
; --- Global State & Startup Sync ---
TargetBrightness: 0, ; Will be initialized right below
OSDState: "Hidden", ; Hidden, Guide, or Active
; --- GUI Elements ---
Gui: Gui("-Caption +AlwaysOnTop +ToolWindow"),
Progress: "",
Text: ""
}
; Initialize State & Setup GUI elements inside the namespace
ScrollControl.TargetBrightness := SyncMonitorOnStartup()
ScrollControl.Gui.BackColor := "1A1A1A"
; Progress bar starts greyed out
ScrollControl.Progress := ScrollControl.Gui.Add("Progress", "w200 h15 c333333 Background111111", ScrollControl.TargetBrightness)
ScrollControl.Text := ScrollControl.Gui.Add("Text", "xp+210 yp cWhite w40", ScrollControl.TargetBrightness . "%")
ScrollControl.Gui.SetFont("s10", "Segoe UI")
SetTimer(CheckMousePosition, 50) ; Faster check for smoother transitions
; --- Hotkeys ---
#HotIf IsMouseInZone("Bottom", true) && !AnyButtonDown()
WheelUp:: {
Send("{Volume_Up}")
UpdateOSD(SoundGetVolume(), "Bottom", true)
}
WheelDown:: {
Send("{Volume_Down}")
UpdateOSD(SoundGetVolume(), "Bottom", true)
}
#HotIf
#HotIf IsMouseInZone("Top", true) && !AnyButtonDown()
WheelUp:: AdjustTarget(ScrollControl.DisplayStep)
WheelDown:: AdjustTarget(-ScrollControl.DisplayStep)
#HotIf
; --- Logic ---
CheckMousePosition() {
if AnyButtonDown() {
if (ScrollControl.OSDState != "Hidden") {
ScrollControl.Gui.Hide()
ScrollControl.OSDState := "Hidden"
}
return
}
atTop := (MouseGetPos(,&y), y <= ScrollControl.EdgeThickness)
atBottom := (MouseGetPos(,&y), y >= A_ScreenHeight - ScrollControl.EdgeThickness)
if (atTop or atBottom) {
inCenter := IsMouseInCenter()
pos := atTop ? "Top" : "Bottom"
val := atTop ? ScrollControl.TargetBrightness : SoundGetVolume()
UpdateOSD(val, pos, inCenter)
ScrollControl.OSDState := inCenter ? "Active" : "Guide"
} else {
if (ScrollControl.OSDState != "Hidden") {
ScrollControl.Gui.Hide()
ScrollControl.OSDState := "Hidden"
}
}
}
UpdateOSD(Value, Position, IsActive) {
; Update visual style based on whether mouse is in center
if IsActive {
ScrollControl.Progress.Opt("+c0078D7") ; Blue
WinSetTransparent(ScrollControl.OSDOpacityActive, ScrollControl.Gui)
} else {
ScrollControl.Progress.Opt("+c444444") ; Dim Grey
WinSetTransparent(ScrollControl.OSDOpacityGuide, ScrollControl.Gui)
}
ScrollControl.Progress.Value := Value
ScrollControl.Text.Value := Round(Value) . "%"
fixedY := (Position == "Top") ? 60 : (A_ScreenHeight - 100)
ScrollControl.Gui.Show("x" . (A_ScreenWidth/2 - 125) . " y" . fixedY . " NoActivate")
}
AdjustTarget(Amount) {
ScrollControl.TargetBrightness := Max(0, Min(100, ScrollControl.TargetBrightness + Amount))
UpdateOSD(ScrollControl.TargetBrightness, "Top", true)
SetTimer(CommitToHardware, ScrollControl.CommitDelay)
}
; --- Helpers ---
SyncMonitorOnStartup() {
try {
hMonitor := GetMonitorHandle()
DllCall("dxva2\GetVCPFeatureAndVCPFeatureReply", "Ptr", hMonitor, "Char", 0x10, "Ptr", 0, "UInt*", &curB := 0, "UInt*", &maxB := 0)
DllCall("dxva2\SetVCPFeature", "Ptr", hMonitor, "Char", 0x12, "UInt", curB)
return curB
} catch
return 50
}
CommitToHardware() {
try {
hMonitor := GetMonitorHandle()
DllCall("dxva2\SetVCPFeature", "Ptr", hMonitor, "Char", 0x10, "UInt", ScrollControl.TargetBrightness)
DllCall("dxva2\SetVCPFeature", "Ptr", hMonitor, "Char", 0x12, "UInt", ScrollControl.TargetBrightness)
}
}
IsMouseInZone(Zone, MustBeInCenter := false) {
MouseGetPos(&x, &y)
if (MustBeInCenter && !IsMouseInCenter())
return false
return (Zone == "Top") ? (y <= ScrollControl.EdgeThickness) : (y >= A_ScreenHeight - ScrollControl.EdgeThickness)
}
IsMouseInCenter() {
MouseGetPos(&x)
return (x >= (A_ScreenWidth/2) - ScrollControl.CenterWidth/2) && (x <= (A_ScreenWidth/2) + ScrollControl.CenterWidth/2)
}
AnyButtonDown() => GetKeyState("LButton", "P") or GetKeyState("RButton", "P")
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")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment