Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An AutoHotKey script that allows fast window moving/resizing/minimizing/maximizing via mouse keys Mouse5/Mouse4/Wheeldown/WheelUp
#NoEnv
#SingleInstance force
#InstallKeybdHook
#KeyHistory 500
ListLines Off
SetKeyDelay,-1, 1
SetControlDelay, -1
SetMouseDelay, -1
SetWinDelay,-1
SendMode, InputThenPlay
SetBatchLines,-1
CoordMode, Mouse
class WindowHelper {
static _ := WindowHelper := new WindowHelper()
static windowStack := []
static activeWindowID := ""
__Init() {
Menu, Tray, Icon, imageres.dll, 262
}
MoveWindow() {
pressedKey := this.Utilities.getPressedHotkey()
initialMousePosition := this.Utilities.getMousePositionInsideWindow()
this.activeWindowID := initialMousePosition.id
if (this.Utilities.isWindowMaximized(this.activeWindowID))
return
currentWindowPosition := this.Utilities.getWindowPosition(this.activeWindowID)
while(GetKeyState(pressedKey, "P")) {
currentMousePos := this.Utilities.getMousePositionInsideWindow()
currentMousePos.x -= initialMousePosition.x
currentMousePos.y -= initialMousePosition.y
x := (currentWindowPosition.x + currentMousePos.x) ; Apply this offset to the window position.
y := (currentWindowPosition.y + currentMousePos.y)
this.Utilities.moveWindow(this.activeWindowID, x, y)
}
}
ResizeWindow() {
pressedKey := this.Utilities.getPressedHotkey()
initialMousePosition := this.Utilities.getMousePositionInsideWindow()
this.activeWindowID := initialMousePosition.id
if (this.Utilities.isWindowMaximized(this.activeWindowID))
return
currentWindowPosition := this.Utilities.getWindowPosition(this.activeWindowID)
winLeft := initialMousePosition.x < (currentWindowPosition.x + currentWindowPosition.w / 2) ? 1 : -1
winUp := initialMousePosition.y < (currentWindowPosition.y + currentWindowPosition.h / 2) ? 1 : -1
while(GetKeyState(pressedKey, "P")) {
currentMousePos := this.Utilities.getMousePositionInsideWindow()
currentWindowPosition := this.Utilities.getWindowPosition(this.activeWindowID)
currentMousePos.x -= initialMousePosition.x
currentMousePos.y -= initialMousePosition.y
x := currentWindowPosition.x + (winLeft + 1) / 2 * currentMousePos.x
y := currentWindowPosition.y + (winUp + 1) / 2 * currentMousePos.y
w := currentWindowPosition.w - winLeft * currentMousePos.x
h := currentWindowPosition.h - winUp * currentMousePos.y
this.Utilities.moveWindow(this.activeWindowID, x, y, w, h)
initialMousePosition.x += currentMousePos.x
initialMousePosition.y += currentMousePos.y
}
}
MinimizeWindow() {
this.activeWindowID := this.Utilities.getMousePositionInsideWindow().id
this.Utilities.minimizeWindow(this.activeWindowID)
this.windowStack.Push(this.activeWindowID)
}
RestoreWindow() {
lastMinimizedWindow := this.windowStack.Pop()
if (!lastMinimizedWindow)
lastMinimizedWindow := this.Utilities.getMousePositionInsideWindow().id
this.Utilities.restoreWindow(lastMinimizedWindow)
}
class Utilities {
getPressedHotkey() {
RegExMatch(A_ThisHotkey, "\w+", pressedKey)
return pressedKey
}
getMousePositionInsideWindow() {
MouseGetPos, x, y, winID
return { x: x, y: y, id: "ahk_id " . winID }
}
getWindowPosition(winID) {
WinGetPos, x, y, w, h, % winID
return { x: x, y: y, w: w, h: h }
}
moveWindow(winID, x, y, w := "", h := "") {
WinMove, % winID,, % x, % y, % w, % h
}
isWindowMaximized(winID) {
WinGet, windowState, MinMax, % winID
return windowState
}
minimizeWindow(winID) {
PostMessage,0x112,0xf020,,, % winID
}
restoreWindow(winID) {
if this.isWindowMaximized(winID)
WinRestore, % winID
else
WinMaximize, % winID
}
closeWindow(winID) {
WinClose, % winID
}
}
}
#If WinActive("ahk_exe notepad++.exe")
^R:: Reload
#If
!XButton2:: WindowHelper.MoveWindow()
!XButton1:: WindowHelper.ResizeWindow()
!WheelUp:: WindowHelper.RestoreWindow()
!WheelDown:: WindowHelper.MinimizeWindow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment