Skip to content

Instantly share code, notes, and snippets.

@brianpeiris
Last active September 30, 2021 13:07
Show Gist options
  • Save brianpeiris/a770c89a74464418d3ba to your computer and use it in GitHub Desktop.
Save brianpeiris/a770c89a74464418d3ba to your computer and use it in GitHub Desktop.
An AutoHotKey script for positioning and resizing windows
WindowsHelpers-Run-As-Admin.lnk
; ---
; WindowsHelpers.ahk
; ---
;
; Window management helpers. Plus a few extras.
;
; Note: You'll have to run this AHK script as an Administrator in order to
; move elevated windows and override the default workstation lock hotkey.
; I usually create a shortcut to the script with the "Run as administrator"
; property enabled.
move(x, y, w, h){
windowId:=WinActive("A")
SysGet, WorkArea, MonitorWorkArea
WinMove
, ahk_id %windowId%
, , A_ScreenWidth * x, WorkAreaBottom * y
, A_ScreenWidth * w, WorkAreaBottom * h
}
; Need to use subroutines for RegWrite because apparently functions don't
; get called quickly enough.
DisableLock:
RegWrite
, REG_DWORD, HKEY_CURRENT_USER
, Software\Microsoft\Windows\CurrentVersion\Policies\System
, DisableLockWorkstation, 1
Return
EnableLock:
RegWrite
, REG_DWORD, HKEY_CURRENT_USER
, Software\Microsoft\Windows\CurrentVersion\Policies\System
, DisableLockWorkstation, 0
Return
; ---
; Key layout:
; ---
; u i o
; j k l
; m , .
; ---
; Halves - Win + <key>
; ---
#u::
move(0, 0, 1/2, 1/2)
return
#i::
move(1/4, 0, 1/2, 1/2)
return
#o::
move(1/2, 0, 1/2, 1/2)
return
#j::
move(0, 0, 1/2, 1)
return
#k::
move(1/4, 0, 1/2, 1)
return
#l::
GoSub, DisableLock
move(1/2, 0, 1/2, 1)
return
#m::
move(0, 1/2, 1/2, 1/2)
return
#,::
move(1/4, 1/2, 1/2, 1/2)
return
#.::
move(1/2, 1/2, 1/2, 1/2)
return
; ---
; Thirds - Win + Shift + <key>
; ---
#+u::
move(0, 0, 1/3, 1/2)
return
#+i::
move(1/3, 0, 1/3, 1/2)
return
#+o::
move(2/3, 0, 1/3, 1/2)
return
#+j::
move(0, 0, 1/3, 1)
return
#+k::
move(1/3, 0, 1/3, 1)
return
#+l::
move(2/3, 0, 1/3, 1)
return
#+m::
move(0, 1/2, 1/3, 1/2)
return
#+,::
move(1/3, 1/2, 1/3, 1/2)
return
#+.::
move(2/3, 1/2, 1/3, 1/2)
return
; ---
; Quarters - Win + Ctrl + Shift + <key>
; ---
#^+u::
move(0, 0, 1/4, 1/2)
return
#^+o::
move(3/4, 0, 1/4, 1/2)
return
#^+j::
move(0, 0, 1/4, 1)
return
#^+l::
move(3/4, 0, 1/4, 1)
return
#^+m::
move(0, 1/2, 1/4, 1/2)
return
#^+.::
move(3/4, 1/2, 1/4, 1/2)
return
; ---
; Misc
; ---
#s::
winset, alwaysontop, , A
return
#;::
GoSub, EnableLock
Sleep, 200
DllCall("LockWorkStation")
Sleep, 200
GoSub, DisableLock
return
#PgUp::
Send {Volume_Up}
return
#PgDn::
Send {Volume_Down}
return
#End::
Send {Volume_Mute}
return
@bookyakuno
Copy link

Thank you!
very helpful.

@johnagius
Copy link

This works on most windows. I have a particular windows program that does not obey the commands fully. I've been trying to solve it for ages. Want to have a look?

@brianpeiris
Copy link
Author

@johnagius what program is it?

@johnagius
Copy link

It's not an off the shelf program. It's a government software for dispensing medication. It behaves really weird when resizing. You can resize to make a restored down window bigger, but when you try to resize back down, it won't allow it. Also pressing the alt on the keyboard makes the window grow slightly bigger. Trying to describe this here makes me look mad. I'd have to show you this via TeamViewer or anydesk if possible. I've never seen anything like it.

@brianpeiris
Copy link
Author

Sure, there's an email address on my profile page, contact me there: https://github.com/brianpeiris

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