Skip to content

Instantly share code, notes, and snippets.

@iyre
Created December 22, 2023 00:48
Show Gist options
  • Save iyre/2607fef09cdd1fc3f9526ea64ba5116c to your computer and use it in GitHub Desktop.
Save iyre/2607fef09cdd1fc3f9526ea64ba5116c to your computer and use it in GitHub Desktop.
AutoHotkey script to change the refresh rate of your primary display
#Requires AutoHotkey v2.0
#SingleInstance
A_IconTip := "Change screen refresh rate"
; Based on an AHK v1 script from this Reddit post
; https://www.reddit.com/r/AutoHotkey/comments/qeqhfs/switch_refresh_rate_using_hotkeys/
; Hotkey modifier symbols: #=Win, !=Alt, ^=Ctrl, +=Shift
; Find and set max supported refresh rate for primary monitor
!1:: {
; Add/remove values as needed
freqs := [500, 360, 280, 240, 180, 175, 170, 165, 160, 144, 120, 75, 60]
for index, freq in freqs {
if SetRefreshRate(freq) {
Return
}
}
TrayTip "Refresh Rate", "Failed to set primary display"
}
; Set 120Hz refresh rate for primary monitor
!2:: {
SetRefreshRate(120)
}
; Set 60Hz refresh rate for primary monitor
!3:: {
SetRefreshRate(60)
}
; Attempt to set the primary display to the specified refresh rate
; Returns True for success, otherwise False
SetRefreshRate(Hz) {
DeviceMode := Buffer(156, 0)
NumPut "UShort", 156, DeviceMode, 36
; Null DeviceName will get settings for default/primary display
DllCall("EnumDisplaySettingsA", "Ptr",0, "UInt",-1, "Ptr",DeviceMode)
NumPut "Ptr", 0x400000, DeviceMode, 40
NumPut "UInt", Hz, DeviceMode, 120
result := DllCall("ChangeDisplaySettingsA", "Ptr",DeviceMode, "UInt",0)
TrayTip ; clear existing TrayTip
if result == 0 {
TrayTip "Refresh Rate", "Set primary display to " Hz "Hz"
Return True
}
Return False
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment