Posted: . At: 12:06 PM. This was 2 years ago. Post ID: 16196
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


A nice AutoHotkey script for Windows to hide your screen.


This AutoHotkey script for Windows will hide your screen when you press Windows-H. You may change the settings at the top for the desired effects. it isn’t going to block stuff like win key + d or alt control delete but it will be a good enough panic button if you want to change hot keys use https://www.autohotkey.com/docs/Hotkeys.htm. Windows-H in Windows 11 is bound to Microsoft Speech Synthesis so you might need to change the key bind on that operating system.

;userconfig options
PutMonitorToSleep :=0 ;  will also put your monitor to sleep if set to 1.
HideCursor :=1 ; hide cursor on black background set to hide cursor when screen is black
;end of user config
 
state=0
 
 
#H:: ; hotkey that toggles state
state:=!state
 
Gui, Color, black
Gui +AlwaysOnTop 
Gui -Caption
Gui Hide
 
 
if state {
Gui, Show, x0 y0 w%A_ScreenWidth% h%A_ScreenHeight%
if (HideCursor = True ) {
show_Mouse(!state)
}
if ( PutMonitorToSleep = True ) {
PostMessage, 0x0112, 0xF170, % (i:=!i)?2:-1,, A
}
 } else {
Gui Hide
if ( HideCursor = True ) {
show_Mouse(!state)
}
if ( PutMonitorToSleep = True ) {
PostMessage, 0x0112, 0xF170, % (i:=!i)?2:-1,, A
}
}
 
show_Mouse(bShow := True) { ; show/hide the mouse cursor\\
;-------------------------------------------------------------------------------
    ; WINAPI: SystemParametersInfo, CreateCursor, CopyImage, SetSystemCursor
    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms724947.aspx
    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms648385.aspx
    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms648031.aspx
    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms648395.aspx
    ;---------------------------------------------------------------------------
    static BlankCursor
    static CursorList := "32512, 32513, 32514, 32515, 32516, 32640, 32641"
        . ",32642, 32643, 32644, 32645, 32646, 32648, 32649, 32650, 32651"
    local ANDmask, XORmask, CursorHandle
 
 
 
    If bShow ; shortcut for showing the mouse cursor
 
        Return, DllCall("SystemParametersInfo"
            , "UInt", 0x57              ; UINT  uiAction    (SPI_SETCURSORS)
            , "UInt", 0                 ; UINT  uiParam
            , "Ptr",  0                 ; PVOID pvParam
            , "UInt", 0                 ; UINT  fWinIni
            , "Cdecl Int")              ; return BOOL
 
    If Not BlankCursor { ; create BlankCursor only once
        VarSetCapacity(ANDmask, 32 * 4, 0xFF)
        VarSetCapacity(XORmask, 32 * 4, 0x00)
 
        BlankCursor := DllCall("CreateCursor"
            , "Ptr", 0                  ; HINSTANCE  hInst
            , "Int", 0                  ; int        xHotSpot
            , "Int", 0                  ; int        yHotSpot
            , "Int", 32                 ; int        nWidth
            , "Int", 32                 ; int        nHeight
            , "Ptr", &ANDmask           ; const VOID *pvANDPlane
            , "Ptr", &XORmask           ; const VOID *pvXORPlane
            , "Cdecl Ptr")              ; return HCURSOR
    }
 
    ; set all system cursors to blank, each needs a new copy
    Loop, Parse, CursorList, `,, %A_Space%
    {
        CursorHandle := DllCall("CopyImage"
            , "Ptr",  BlankCursor       ; HANDLE hImage
            , "UInt", 2                 ; UINT   uType      (IMAGE_CURSOR)
            , "Int",  0                 ; int    cxDesired
            , "Int",  0                 ; int    cyDesired
            , "UInt", 0                 ; UINT   fuFlags
            , "Cdecl Ptr")              ; return HANDLE
 
        DllCall("SetSystemCursor"
            , "Ptr",  CursorHandle      ; HCURSOR hcur
            , "UInt", A_Loopfield       ; DWORD   id
            , "Cdecl Int")              ; return BOOL
    }
}

But this is a fine script. This will be very useful for Screen Blanking. But why not just use Windows-L? But hotkeys have their place and therefore the script above should be very useful.

In C++ programming on Windows, this is how you can blank the screen by turning off the monitor.

SendMessage(handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2);

If you press Control-Windows and then the left and right arrow keys you can change the active workspace. This is also a most useful keyboard shortcut.

Here is another code sample, this will move the mouse a little to wake up your PC.

[DllImport("user32.dll")]
static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);
 
private const int MOUSEEVENTF_MOVE = 0x0001;
 
private void Wake(){
    mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, UIntPtr.Zero);
    Sleep(40);
    mouse_event(MOUSEEVENTF_MOVE, 0, -1, 0, UIntPtr.Zero);
}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.