Skip to content

Instantly share code, notes, and snippets.

@stevedsun
Last active August 29, 2024 14:34
Show Gist options
  • Save stevedsun/319f0c05b02e739207743dd441b6239a to your computer and use it in GitHub Desktop.
Save stevedsun/319f0c05b02e739207743dd441b6239a to your computer and use it in GitHub Desktop.
Windows Efficient Setup
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#Include %A_ScriptDir%\DesktopSwitch.ahk
#Include %A_ScriptDir%\InputSwitch.ahk
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; Path to the DLL, relative to the script
VDA_PATH := A_ScriptDir . "\VirtualDesktopAccessor.dll"
hVirtualDesktopAccessor := DllCall("LoadLibrary", "Str", VDA_PATH, "Ptr")
GetDesktopCountProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "GetDesktopCount", "Ptr")
GoToDesktopNumberProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "GoToDesktopNumber", "Ptr")
GetCurrentDesktopNumberProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "GetCurrentDesktopNumber", "Ptr")
IsWindowOnCurrentVirtualDesktopProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "IsWindowOnCurrentVirtualDesktop", "Ptr")
IsWindowOnDesktopNumberProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "IsWindowOnDesktopNumber", "Ptr")
MoveWindowToDesktopNumberProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "MoveWindowToDesktopNumber", "Ptr")
IsPinnedWindowProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "IsPinnedWindow", "Ptr")
GetDesktopNameProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "GetDesktopName", "Ptr")
SetDesktopNameProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "SetDesktopName", "Ptr")
CreateDesktopProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "CreateDesktop", "Ptr")
RemoveDesktopProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "RemoveDesktop", "Ptr")
; On change listeners
RegisterPostMessageHookProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "RegisterPostMessageHook", "Ptr")
UnregisterPostMessageHookProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "UnregisterPostMessageHook", "Ptr")
GetDesktopCount() {
global GetDesktopCountProc
count := DllCall(GetDesktopCountProc, "Int")
return count
}
MoveCurrentWindowToDesktop(desktopNumber) {
global MoveWindowToDesktopNumberProc, GoToDesktopNumberProc
WinGet, activeHwnd, ID, A
DllCall(MoveWindowToDesktopNumberProc, "Ptr", activeHwnd, "Int", desktopNumber, "Int")
DllCall(GoToDesktopNumberProc, "Int", desktopNumber)
}
GoToPrevDesktop() {
global GetCurrentDesktopNumberProc, GoToDesktopNumberProc
current := DllCall(GetCurrentDesktopNumberProc, "Int")
last_desktop := GetDesktopCount() - 1
; If current desktop is 0, go to last desktop
if (current = 0) {
MoveOrGotoDesktopNumber(last_desktop)
} else {
MoveOrGotoDesktopNumber(current - 1)
}
return
}
GoToNextDesktop() {
global GetCurrentDesktopNumberProc
current := DllCall(GetCurrentDesktopNumberProc, "Int")
last_desktop := GetDesktopCount() - 1
; If current desktop is last, go to first desktop
if (current = last_desktop) {
MoveOrGotoDesktopNumber(0)
} else {
MoveOrGotoDesktopNumber(current + 1)
}
return
}
GoToDesktopNumber(num) {
global GoToDesktopNumberProc
DllCall(GoToDesktopNumberProc, "Int", num, "Int")
return
}
MoveOrGotoDesktopNumber(num) {
; If user is holding down Mouse left button, move the current window also
if (GetKeyState("LButton")) {
MoveCurrentWindowToDesktop(num)
} else {
GoToDesktopNumber(num)
}
return
}
GetDesktopName(num) {
global GetDesktopNameProc
utf8_buffer := ""
utf8_buffer_len := VarSetCapacity(utf8_buffer, 1024, 0)
ran := DllCall(GetDesktopNameProc, "Int", num, "Ptr", &utf8_buffer, "Ptr", utf8_buffer_len, "Int")
name := StrGet(&utf8_buffer, 1024, "UTF-8")
return name
}
SetDesktopName(num, name) {
; NOTICE! For UTF-8 to work AHK file must be saved with UTF-8 with BOM
global SetDesktopNameProc
VarSetCapacity(name_utf8, 1024, 0)
StrPut(name, &name_utf8, "UTF-8")
ran := DllCall(SetDesktopNameProc, "Int", num, "Ptr", &name_utf8, "Int")
return ran
}
CreateDesktop() {
global CreateDesktopProc
ran := DllCall(CreateDesktopProc)
return ran
}
RemoveDesktop(remove_desktop_number, fallback_desktop_number) {
global RemoveDesktopProc
ran := DllCall(RemoveDesktopProc, "Int", remove_desktop_number, "Int", fallback_desktop_number, "Int")
return ran
}
; SetDesktopName(0, "It works! 🐱")
; How to listen to desktop changes
DllCall(RegisterPostMessageHookProc, "Ptr", A_ScriptHwnd, "Int", 0x1400 + 30, "Int")
OnMessage(0x1400 + 30, "OnChangeDesktop")
OnChangeDesktop(wParam, lParam, msg, hwnd) {
Critical, 100
OldDesktop := wParam + 1
NewDesktop := lParam + 1
Name := GetDesktopName(NewDesktop - 1)
; Use Dbgview.exe to checkout the output debug logs
OutputDebug % "Desktop changed to " Name " from " OldDesktop " to " NewDesktop
}
!1:: MoveOrGotoDesktopNumber(0)
!2:: MoveOrGotoDesktopNumber(1)
!3:: MoveOrGotoDesktopNumber(2)
!4:: MoveOrGotoDesktopNumber(3)
!5:: MoveOrGotoDesktopNumber(4)
CapsLock::
KeyWait, CapsLock, T0.45
if (ErrorLevel)
SetCapsLockState, % (t:=!t) ? "On" : "Off"
else
Send ^{Space}
KeyWait, CapsLock
return

Windows Efficient Setup

  • Environment: Windows 11

Screenshot

  • Tool: Flameshot

Using PrintScreen to shot screen.

launcher Bar

  • Tool: PowerToys

Keyboard Layout

  • Tool: AutoHotKey, PowerToys
Shortcut Description
Alt + Number Switch between Apps
Win + Number Switch between Desktops
Caplock Switch between Input Methods
Caplock(hold) Switch between Upper/Lower
  1. Download VirtualDesktopAccessor.dll
  2. Put the .dll and .ahk files in one folder, create a shortcut for Config.ahk.
  3. Copy the shortcut to path C:\Users\<YourName>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup to make it auto startup.
  4. Install PowerToys, remap LWin and LAlt.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment