Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Toggle Screen Rotation (Windows)

This project provides a native Python script to toggle the orientation of a secondary monitor (landscape/portrait) on a Windows environment via a keyboard shortcut.

Technical Features

  • Zero third-party dependencies: Exclusive use of the standard ctypes library to interact directly with the Windows API (user32.dll).
  • Broad compatibility: Operational on both stable and development versions of Python 3.x (tested on 3.15.0a5).
  • Silent execution: Using the .pyw extension ensures background execution without instantiating a console window.

Prerequisites

  • Operating System: Windows (10, 11).
  • Python 3.x interpreter.
  • Hardware configuration: At least two connected monitors recognized by the system.

Installation

  1. Download screen_rotation.pyw
  2. Place the file in a permanent system directory.

Python Installation

A Python 3.x interpreter is required to execute this script on Windows.

Standard Installation

  1. Navigate to the official Python distribution page: https://www.python.org/downloads/windows/

  2. Download the Windows installer for the latest stable release (64-bit).

  3. Execute the downloaded installer.

  4. Critical Step: On the initial setup interface, explicitly check the box labeled Add Python 3.x to PATH at the bottom of the window before proceeding.

  5. Click Install Now and allow the process to complete.

Alternative Installation (pyenv-win)

For environments requiring multiple Python versions, pyenv-win can be utilized.

  1. Install pyenv-win via PowerShell:
    Invoke-WebRequest -UseBasicParsing -Uri "[https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1](https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1)" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"
    

Keyboard Shortcut Configuration

Assigning a key combination requires creating an intermediate system shortcut.

  1. Right-click the screen_rotation.pyw file and select Create shortcut.
  2. Move this shortcut to an accessible location (e.g., the Desktop).
  3. Open the shortcut's Properties (Right-click > Properties).
  4. In the Shortcut tab, modify the Target field to use the GUI-less Python executable (pythonw.exe), followed by the absolute path to the script.
    • Syntax: C:\Path\To\pythonw.exe "C:\Path\To\screen_rotation.pyw"
  5. Select the Shortcut key field and execute the desired physical key combination on the keyboard (e.g., Ctrl + Alt + F12).
  6. Validate by clicking Apply then OK.

API Operation

  1. EnumDisplayDevicesW: Identifies active displays and isolates the hardware identifier of the secondary monitor.
  2. EnumDisplaySettingsW: Extracts the DEVMODEW structure containing the current resolution and orientation parameters of the target monitor.
  3. ChangeDisplaySettingsExW: Applies the new orientation (0 or 90 degrees), inverts the resolution axes (width/height), and forces a Windows registry update (CDS_UPDATEREGISTRY).

About

Python script to toggle the orientation of a secondary monitor (landscape/portrait) on a Windows environment via a keyboard shortcut.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages