Skip to content

Instantly share code, notes, and snippets.

@sampollard
Last active February 24, 2024 08:30
Show Gist options
  • Save sampollard/9548908ac084c40f56458676f1ac5b2f to your computer and use it in GitHub Desktop.
Save sampollard/9548908ac084c40f56458676f1ac5b2f to your computer and use it in GitHub Desktop.
Add some convenience keybindings
; How to view scancodes in AHK v1 (I can't figure this out for v2)
; Right-click, "run this script" on this file (not the .exe)
; Right click and select "Open"
; View -> Key History and Script Info
; Now you can type things, press F5 to refresh; they appear bottom-up
SendMode("Input") ; Tutorials say this is good
Capslock::LCtrl ; Map Capslock to Control
RCtrl & Capslock::Capslock ; Map Right control + Capslock to Capslock
; Hibernate on ctrl+alt+win+h
; On Razer blades, the power button default is sleep
; Docs: https://learn.microsoft.com/en-us/windows/win32/api/powrprof/nf-powrprof-setsuspendstate
^#!h::{
DllCall("PowrProf\SetSuspendState", "int", 1, "int", 0, "int", 0)
}
ns := 0
^#!n::{
global ns := !ns
if (ns) {
MsgBox "Preventing sleep.", "Ctrl-Alt-Win-N to cancel"
SetTimer UselessKey, 60*1000
} else {
MsgBox "Sleep back to default", "Sleep", "T1"
SetTimer UselessKey, 0 ; 0 instead of "Off" for v1
}
}
; Can query refreshrate with
; wmic PATH Win32_videocontroller get currentrefreshrate
; Another potentially useful link
; https://www.reddit.com/r/ultrawidemasterrace/comments/ogkiho/autohotkey_script_for_quickly_changing/
; Set Refresh rate to 165Hz (f = fast)
psfast := "C:\Path\to\165hz.ps1"
psslow := "C:\Path\to\60hz.ps1"
^#!f::{
RunWait("powershell -ExecutionPolicy Bypass -Windowstyle Hidden -File " . psfast)
MsgBox "Refresh rate set to 165Hz", "165Hz", "T1"
}
; Set Refresh rate to 60Hz (r = really slow)
^#!r::{
RunWait("powershell -ExecutionPolicy Bypass -Windowstyle Hidden -File " . psslow)
MsgBox "Refresh rate set to 60Hz", "60Hz", "T1"
}
; Any key that is unbound
UselessKey()
{
Send("{F15}")
return
}
@sampollard
Copy link
Author

sampollard commented Feb 24, 2024

In order to get this working, you need two files: 165hz.ps1 and 60hz.ps1 (adjust accordingly for other refresh rates).

The steps are as follows:

  1. Save the first file somewhere as capslock.ahk
  2. Edit the file path to where your xxxHz.ps1 are located
  3. Run autohotkeydash and compile capslock.ahk
  4. If you want this to run at startup. C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

NOTES:

  • I recommend making a shortcut to the capslock.exe in the startup folder, that way if you update it you don't have to copy things over.
  • Do not make a shortcut to a ps1 file. For some reason Windows appends a .lnk to things with shortcuts and this causes Powershell to fail because it expects a .ps1 file (Security:tm:)
  • Autohotkey sometimes makes anticheat software angry (most recently Helldivers 2). I've found mapping capslock to escape had no problem, but capslock to control did. Who knows why.

Here is 60hz.ps1, to get other hz rename the file and change the last line. It would be much more convenient to just call the Set-ScreenRefreshRate 60 inside autohotkey or wherever, but to do that you first have to source the file, i.e. . 60Hz.ps1 (the one thing that seem to be shared between POSIX shells and powershell). But I don't know how to do that in advance of autohotkey running, and you're not going to be switching between many refresh rates in any case.

function Set-ScreenRefreshRate
{ 
    param ( 
        [Parameter(Mandatory=$true)] 
        [int] $Frequency
    ) 

    $pinvokeCode = @"         
        using System; 
        using System.Runtime.InteropServices; 

        namespace Display 
        { 
            [StructLayout(LayoutKind.Sequential)] 
            public struct DEVMODE1 
            { 
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 
                public string dmDeviceName; 
                public short dmSpecVersion; 
                public short dmDriverVersion; 
                public short dmSize; 
                public short dmDriverExtra; 
                public int dmFields; 

                public short dmOrientation; 
                public short dmPaperSize; 
                public short dmPaperLength; 
                public short dmPaperWidth; 

                public short dmScale; 
                public short dmCopies; 
                public short dmDefaultSource; 
                public short dmPrintQuality; 
                public short dmColor; 
                public short dmDuplex; 
                public short dmYResolution; 
                public short dmTTOption; 
                public short dmCollate; 
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 
                public string dmFormName; 
                public short dmLogPixels; 
                public short dmBitsPerPel; 
                public int dmPelsWidth; 
                public int dmPelsHeight; 

                public int dmDisplayFlags; 
                public int dmDisplayFrequency; 

                public int dmICMMethod; 
                public int dmICMIntent; 
                public int dmMediaType; 
                public int dmDitherType; 
                public int dmReserved1; 
                public int dmReserved2; 

                public int dmPanningWidth; 
                public int dmPanningHeight; 
            }; 

            class User_32 
            { 
                [DllImport("user32.dll")] 
                public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode); 
                [DllImport("user32.dll")] 
                public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags); 

                public const int ENUM_CURRENT_SETTINGS = -1; 
                public const int CDS_UPDATEREGISTRY = 0x01; 
                public const int CDS_TEST = 0x02; 
                public const int DISP_CHANGE_SUCCESSFUL = 0; 
                public const int DISP_CHANGE_RESTART = 1; 
                public const int DISP_CHANGE_FAILED = -1; 
            } 

            public class PrimaryScreen  
            { 
                static public string ChangeRefreshRate(int frequency) 
                { 
                    DEVMODE1 dm = GetDevMode1(); 

                    if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm)) 
                    { 
                        dm.dmDisplayFrequency = frequency;

                        int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST); 

                        if (iRet == User_32.DISP_CHANGE_FAILED) 
                        { 
                            return "Unable to process your request. Sorry for this inconvenience."; 
                        } 
                        else 
                        { 
                            iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY); 
                            switch (iRet) 
                            { 
                                case User_32.DISP_CHANGE_SUCCESSFUL: 
                                { 
                                    return "Success"; 
                                } 
                                case User_32.DISP_CHANGE_RESTART: 
                                { 
                                    return "You need to reboot for the change to happen.\n If you feel any problems after rebooting your machine\nThen try to change resolution in Safe Mode."; 
                                } 
                                default: 
                                { 
                                    return "Failed to change the resolution"; 
                                } 
                            } 
                        } 
                    } 
                    else 
                    { 
                        return "Failed to change the resolution."; 
                    } 
                } 

                private static DEVMODE1 GetDevMode1() 
                { 
                    DEVMODE1 dm = new DEVMODE1(); 
                    dm.dmDeviceName = new String(new char[32]); 
                    dm.dmFormName = new String(new char[32]); 
                    dm.dmSize = (short)Marshal.SizeOf(dm); 
                    return dm; 
                } 
            } 
        } 
"@ # don't indent this line

    Add-Type $pinvokeCode -ErrorAction SilentlyContinue

    [Display.PrimaryScreen]::ChangeRefreshRate($frequency) 
}

function Get-ScreenRefreshRate
{
    $frequency = Get-WmiObject -Class "Win32_VideoController" | Select-Object -ExpandProperty "CurrentRefreshRate"

    return $frequency
}

Set-ScreenRefreshRate 60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment