Skip to content

Instantly share code, notes, and snippets.

@makerj
Last active March 8, 2020 04:34
Show Gist options
  • Save makerj/bb93e23cc2db2948094fa08786da3e3e to your computer and use it in GitHub Desktop.
Save makerj/bb93e23cc2db2948094fa08786da3e3e to your computer and use it in GitHub Desktop.
"""
Maximize currently focused window to vertically half of the screen
Win+Pgup hotkey performs top-half maximization
Win+Pgdn hotkey performs bottom-half maximization
NOTE: This script requires keyboard and pywin32 packages
Tested on my Microsoft Windows 10 PC with dual-monitor
"""
import keyboard
import win32gui, win32con
import time
import os
import datetime
import traceback
def log(message):
try:
f = open(os.path.join(os.path.normpath(os.path.expanduser("~/Desktop")), "window_vertical_toggler.log"), "a")
print("[{}] {}".format(datetime.datetime.now(), str(message)), file=f)
f.close()
except:
pass
def maximize_focused_window(direction):
try:
window = win32gui.GetForegroundWindow()
"""
Maximize & Restore focused window to detect current screen dimension
"""
win32gui.ShowWindow(window, win32con.SW_MAXIMIZE)
time.sleep(0.064)
start_x, start_y, end_x, end_y = win32gui.GetWindowRect(window)
win32gui.ShowWindow(window, win32con.SW_RESTORE)
time.sleep(0.032)
monitor_width = end_x - start_x
monitor_height = end_y - start_y
"""
Move focused window to desired position
"""
if direction == "top":
win32gui.MoveWindow(window, start_x, start_y, monitor_width, monitor_height // 2, True)
elif direction == "bottom":
win32gui.MoveWindow(window, start_x, start_y + monitor_height // 2, monitor_width, monitor_height // 2, True)
except Exception as e:
log(traceback.format_exc())
try:
log("Window Vertical Toggler Started")
keyboard.add_hotkey("win+pgup", lambda: maximize_focused_window("top"))
keyboard.add_hotkey("win+pgdown", lambda: maximize_focused_window("bottom"))
keyboard.wait()
except Exception as e:
log(traceback.format_exc())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment