Turn off monitor from commandline

Windows

Powershell

Create a ps1 file with the content,

(Add-Type '[DllImport("user32.dll")]public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)

A keyboard shortcut can be mapped to the script with tool such as powertoys. In powertoy, map a keyboard shortcut to run program C:\Program Files\PowerShell\7\pwsh.exe with argument -File C:\Users\voras\Documents\PowerShell\Scripts\turnoffmonitor.ps1.

C program

Instead of a powershell script, we can write a C program doing the same thing.

// main.c
// To compile
// > cl main.c user32.lib

#define WIN 32_LEAN_AND_MEAN
#include <windows.h>

int WINAPI WinMain(HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine, int nCmdShow)
{
  return SendMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
}

Download: binary

Linux - X server

Map a keyboard shortcut to

xset dpms force off