Two unfinished attempts at a Windows utility that would pin a window on top with a keyboard shortcut.
Neither one works. This repository is published as-is, from February 2023, as a record of the attempt. Read the next section before you try to run either file.
win32gui.EnumWindows(always_on_top, None)EnumWindows walks every top-level window exactly once, synchronously, and then the process exits. The callback checks keyboard.is_pressed("ctrl + space") during that single instantaneous sweep, so unless the keys happen to be held down at the moment of launch, nothing is ever pinned — and if they were held, it would pin every window on the desktop rather than the focused one.
There is no hotkey listener and no event loop. A working version needs keyboard.add_hotkey (or a Win32 message loop) plus GetForegroundWindow to target one window.
It also imports win32gui, win32con (both from pywin32) and keyboard. Neither package is declared anywhere in this repo — there is no requirements.txt, setup.py, or pyproject.toml — so the usual result of running the file is an ImportError traceback.
$keyboardHook = [System.Windows.Forms.KeyboardHook]::Start(...)[System.Windows.Forms.KeyboardHook] does not exist in any version of .NET. The script dies on line 29 with a type-not-found error. A global hotkey in PowerShell needs RegisterHotKey from user32.dll via Add-Type, or a low-level WH_KEYBOARD_LL hook.
Two further problems:
- Line 10 uses
ContextMenu, which was removed in .NET Core 3.0 and later — so on PowerShell 7 it fails there first. (ContextMenuStripis the replacement.) - The logic toggles
TopMoston theNotifyIcon. A tray icon has noTopMostproperty and is not a window, so even with a working hotkey it would not pin anything.
Line 7 also calls ExtractAssociatedIcon on (Get-Process | Select-Object -First 1).Path, which is $null for system processes and throws non-fatally.
Windows only. Both files use Windows-specific APIs and neither runs on macOS or Linux.
For the Python file:
pip install pywin32 keyboard
Note that keyboard requires administrator privileges on Windows to capture global input.
Unmaintained. Kept for reference rather than use.
If you want to build a working version, the SetWindowPos(hwnd, HWND_TOPMOST, ...) call in the Python file is the correct primitive — that one line is sound. Everything around it needs replacing.
MIT — see LICENSE.