Monitor And Control Off - isabling visual ouput and physical input

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Monitor And Control Off - isabling visual ouput and physical input

Post by A Keymaker » 19 Nov 2023, 16:17

After much hardship, consulting on this forum and testing on my system, I am finally able to publish a working fail-safe script that turns off screen and blocks input while also allowing at the same time to control player software with media keys

To regain control user must press quickly two times the Middle Mouse Button. Some more info is within the code itself added as comments

Monitor And Control Off 3.5.1p.ahk
(24.32 KiB) Downloaded 20 times
Monitor And Control Off 3.5.0p.ahk
(24.22 KiB) Downloaded 32 times

Monitor And Control Off 3.5.0p CMD Info.jpg
Monitor And Control Off 3.5.0p CMD Info.jpg (79.87 KiB) Viewed 329 times

Code: Select all

; ███████████████████████████████████████████████████████████████████████████████████████████████████
; ███  Monitor And Control Off ██████████████████████████████████████████████████████████████████████
; ███████████████████████████████████████████████████████████████████████████████████████████████████

/*

version: 3.5.1p [2023 11 20]
author: A Keymaker
forum link: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=123252

*/

; ███████████████████████████████████████████████████████████████████████████████████████████████████
; ███  Monitor And Control Off ███ info ███

/*

This script has been written and then thoroughly tested only on a Windows 10 Enterprise 20H2 x64 with a single monitor setup connected via HDMI - hence results on other setups may vary

Known limitations:

    ● Input from the system wide keyboard shortcut of >>Alt + Control + Delete<< is still not blocked, due to it being rooted too deeply

    ● Input from keys >>Wake Up<<, >>Sleep<<, and >>Power<< is not blocked. If needed, behavior of those keys [including their complete deactivation] user can define for operating system set at: Windows 10 > Control Panel > Power Options > Choose what the power buttons do > Power and sleep buttons settings

    ● Rarely the monitor half-wakes: it turns itself on but fails to receive a signal - and thus makes a search through each possible signal input method [assuming the monitor is capable of doing so and is set by the user to behave like so]. The root cause of this incomplete waking up remains unknown ▶▶▶ THIS IS WHAT THE >>NEWLY ADDED TEST<< IS FOR IN THIS FILE

*/

; ███████████████████████████████████████████████████████████████████████████████████████████████████
; ███  Monitor And Control Off ███ CMD with information ████

/*

To display this info aesthetically this AHK file requires:

    ● usage of 21:9 aspect ratio screen

    ● setting of: Windows 10 > Settings > System > Display > Scale and layouts > Change the size of text, apps, and other items > 150%

    ● setting of: Windows 10 > Settings > System > Display > Scale and layouts > Display resolution > 3840 x 1600

    ● setting of: Windows 10 > Settings > System > Display > Scale and layouts > Display orientation > Landscape

    ● setting of: Command Prompt > Properties > Font > Size > 17

    ● [most likely] usage of old Windows Console [conhost.exe] as it has not been tested yet with its successor Windows Terminal [windowsterminal.exe]

*/

e=
    (Ltrim join&
    @echo off

    color 08

    title TURNING OFF MONITORS + BLOCKING INPUT

    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.                   ███    ███      ██████      ███    ██     ██     ████████      ██████      ██████                  ██              ██████      ██████      ███    ██     ████████     ██████       █████       ██
    echo.                   ████  ████     ██    ██     ████   ██     ██        ██        ██    ██     ██   ██                ██              ██          ██    ██     ████   ██        ██        ██   ██     ██    ██     ██
    echo.                   ██ ████ ██     ██    ██     ██ ██  ██     ██        ██        ██    ██     ██████                ██               ██          ██    ██     ██ ██  ██        ██        ██████      ██    ██     ██
    echo.                   ██  ██  ██     ██    ██     ██  ██ ██     ██        ██        ██    ██     ██   ██              ██                ██          ██    ██     ██  ██ ██        ██        ██   ██     ██    ██     ██
    echo.                   ██      ██      ██████      ██   ████     ██        ██         ██████      ██   ██             ██                  ██████      ██████      ██   ████        ██        ██   ██      ██████      ███████
    echo.
    echo.
    echo.
    echo.                                                                                                      ██████      ███████     ███████
    echo.                                                                                                     ██    ██     ██          ██
    echo.                                                                                                     ██    ██     █████       █████
    echo.                                                                                                     ██    ██     ██          ██
    echo.                                                                                                      ██████      ██          ██
    echo.
    echo.
    echo.
    echo.
    echo.
    echo.                                                                                                  Turning off monitors and blocking input
    echo.
    echo.                                                                                                          PRESS ANY KEY TO CANCEL
    echo.
    echo.
    echo.
    echo.                                                                                                          To revert this later on
    echo.
    echo.                                                                                                     PRESS MIDDLE MOUSE BUTTON QUICKLY
    echo.                                                                                                                   ► 2 ◄
    echo.                                                                                                                   TIMES
    echo.
    echo.
    echo.
    echo.

    timeout /t 5 | findstr /r ".0$" >nul
    exit
    )

RunWait, powershell.exe -Command "Start-Process cmd.exe -ArgumentList '/k %e%' -Verb RunAs -WindowStyle Maximized",, Hide

; ███████████████████████████████████████████████████████████████████████████████████████████████████
; ███  Monitor And Control Off ███ fail-safe / abort ███

; ███████████████████████████████████████████████████████████████████████████████████████████████████
; ███  Monitor And Control Off ███ fail-safe / abort ███ countdown ███

SetTimer, Fail_Safe_For_Closing_Of_This_AHK_File_Before_Its_Further_Execution, 4000

; ███████████████████████████████████████████████████████████████████████████████████████████████████
; ███  Monitor And Control Off ███ fail-safe / abort ███ CMD window detection ███

#Persistent

SetTitleMatchMode, 2
SetTitleMatchMode, Slow
Name_Of_Program_To_Monitor := "cmd.exe"
Title_Of_Window_To_Search_For_#1 := "TURNING OFF MONITORS + BLOCKING INPUT"
Title_Of_Window_To_Search_For_#2 := "Administrator:  TURNING OFF MONITORS + BLOCKING INPUT"
Title_Of_Window_To_Search_For_#3 := "Administrator: TURNING OFF MONITORS + BLOCKING INPUT"

SetTimer, Checkup_Process, 444

Return

Checkup_Process:
    Sleep, 111
    Count := 0
    WinGet, id, list, , , ahk_exe %Name_Of_Program_To_Monitor%

    Loop, %id%
        {
        this := "ahk_id " id%A_Index%
        WinGetTitle, Title_Of_Current_Window, % this

        If (Title_Of_Current_Window = Title_Of_Window_To_Search_For_#1 || Title_Of_Current_Window = Title_Of_Window_To_Search_For_#2 || Title_Of_Current_Window = Title_Of_Window_To_Search_For_#3)
            {
            Count++

            If (Count > 1)
                {
                Loop, %id%
                    {
                    this := "ahk_id " id%A_Index%
                    WinGetTitle, Title_Of_Current_Window, % this
                    If (Title_Of_Current_Window = Title_Of_Window_To_Search_For_#1 || Title_Of_Current_Window = Title_Of_Window_To_Search_For_#2 || Title_Of_Current_Window = Title_Of_Window_To_Search_For_#3)
                    WinClose, % this
                    }
                ExitApp
                }
            }
        }

    If (Count = 0)
        ExitApp

Return

; ███  Monitor And Control Off ███ fail-safe / abort ███ Space key replacement with any key ███

#Inputlevel 1

; ███  Monitor And Control Off ███ fail-safe / abort ███ Space key replacement with any key ███ keyboard layout info inducing ones ███

Problematic_Keys_In_Main_File := "£¤§¨´åæø"

For I, A_Key_In_Main_File In StrSplit(Problematic_Keys_In_Main_File)
    {
    HotKey, %A_Key_In_Main_File%, Problematic_Hotkeys_In_Main_File, UseErrorLevel
    }
Return

Problematic_Hotkeys_In_Main_File:
Switch A_ThisHotkey
    {
    Case "£", "¤", "§", "¨", "´", "å", "æ", "ø":
    Goto Problematic_Key_In_Main_File_Was_Pressed
    }

; ███  Monitor And Control Off ███ fail-safe / abort ███ Space key replacement with any key ███ straightforward ones ███

!::Space
"::Space
#::Space
$::Space
%::Space
&::Space
'::Space
(::Space
)::Space
*::Space
+::Space
,::Space
-::Space
.::Space
/::Space
0::Space
1::Space
2::Space
3::Space
4::Space
5::Space
6::Space
7::Space
8::Space
9::Space
:::
`;::
SC034::Space
SC027::Space
<::Space
=::Space
>::Space
?::Space
@::Space
Alt::Space
AppsKey::Space
Backspace::Space
Break::Space
Browser_Back::Space
Browser_Favorites::Space
Browser_Forward::Space
Browser_Home::Space
Browser_Refresh::Space
Browser_Search::Space
Browser_Stop::Space
CapsLock::Space
Control::Space
CtrlBreak::Space
Delete::Space
Down::Space
End::Space
Enter::Space
Esc::Space
F1::Space
F10::Space
F11::Space
F12::Space
F13::Space
F14::Space
F15::Space
F16::Space
F17::Space
F18::Space
F19::Space
F2::Space
F20::Space
F21::Space
F22::Space
F23::Space
F24::Space
F3::Space
F4::Space
F5::Space
F6::Space
F7::Space
F8::Space
F9::Space
Help::Space
Home::Space
Insert::Space
LAlt::Space
LControl::Space
LShift::Space
LWin::Space
Launch_App1::Space
Launch_App2::Space
Launch_Mail::Space
Launch_Media::Space
Left::Space
Media_Next::Space
Media_Play_Pause::Space
Media_Prev::Space
Media_Stop::Space
NumLock::Space
Numpad0::Space
Numpad1::Space
Numpad2::Space
Numpad3::Space
Numpad4::Space
Numpad5::Space
Numpad6::Space
Numpad7::Space
Numpad8::Space
Numpad9::Space
NumpadAdd::Space
NumpadClear::Space
NumpadDel::Space
NumpadDiv::Space
NumpadDot::Space
NumpadDown::Space
NumpadEnd::Space
NumpadEnter::Space
NumpadHome::Space
NumpadIns::Space
NumpadLeft::Space
NumpadMult::Space
NumpadPgDn::Space
NumpadPgUp::Space
NumpadRight::Space
NumpadSub::Space
NumpadUp::Space
Pause::Space
PgDn::Space
PgUp::Space
PrintScreen::Space
RAlt::Space
RControl::Space
RShift::Space
RWin::Space
Right::Space
ScrollLock::Space
Shift::Space
Sleep::Space
Space::Space
Tab::Space
Up::Space
Volume_Down::Space
Volume_Mute::Space
Volume_Up::Space
[::Space
\::Space
]::Space
_::Space
`::Space
a::Space
b::Space
c::Space
d::Space
e::Space
f::Space
g::Space
h::Space
i::Space
j::Space
k::Space
l::Space
m::Space
n::Space
o::Space
p::Space
q::Space
r::Space
s::Space
t::Space
u::Space
v::Space
w::Space
x::Space
y::Space
z::Space
{::Space
|::Space
}::Space
~::Space
€::Space

Return

; ███  Monitor And Control Off ███ fail-safe / abort ███ Space key replacement with any key ███ Space key instructions ███

#Inputlevel 0

Space::
     Send {Esc Down}{Esc Up}

Problematic_Key_In_Main_File_Was_Pressed:
    {
    SetTitleMatchMode, 2
    WinGet, List_Of_CMD_Windows, List, TURNING OFF MONITORS + BLOCKING INPUT

    Loop, %List_Of_CMD_Windows%
        {
        ID_Of_This := List_Of_CMD_Windows%A_Index%
        WinClose, ahk_id %ID_Of_This%
        }
    }

ExitApp

Return

; ███  Monitor And Control Off ███ fail-safe / abort ███ label name ███

Fail_Safe_For_Closing_Of_This_AHK_File_Before_Its_Further_Execution:

; ███████████████████████████████████████████████████████████████████████████████████████████████████
; ███  Monitor And Control Off ███ the Off code [in temp file] ███

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ initialization ███

Name_Of_The_Temporary_File := "Block Most Of The Input.ahk"

FileRecycle, %Name_Of_The_Temporary_File%

FileAppend,   ; Part / Section #1
(
; ███  Monitor And Control Off ███ the Off code [in temp file] ███ pre-run cleanup ███

; FileRecycle, %Name_Of_The_Temporary_File%

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ overall settings ███

#Persistent

#MaxHotkeysPerInterval 999999999

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ variables ███

MMB_Clicks_Counter := 0
MMB_Is_Down := 0
Close_This_Script := 0

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ monitor off ███

Loop
    {
    If GetKeyState("MButton", "P")   ; Stops this loop upon pressing of Middle Mouse Button
        break

    SendMessage, 0x112, 0xF170, 2,, Program Manager
    Sleep, 444

    BlockInput, MouseMove

Sleep 111

MouseMove, -1, 0, 0, R
Sleep 11
MouseMove, 1, 0, 0, R
Sleep 11

MouseMove, 1, 0, 0, R
Sleep 11
MouseMove, -1, 0, 0, R
Sleep 11

MouseMove, 0, -1, 0, R
Sleep 11
MouseMove, 0, 1, 0, R
Sleep 11

MouseMove, 0, 1, 0, R
Sleep 11
MouseMove, 0, -1, 0, R
Sleep 11

    }

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ mouse input blockade break-off + temp file removal after ███

$MButton::
    MMB_Is_Down := 1
    Return

$MButton Up::

    If (MMB_Is_Down = 1 and Close_This_Script = 0)
        {
        MMB_Clicks_Counter++

       If (MMB_Clicks_Counter = 2)
            {
            FileRecycle, %Name_Of_The_Temporary_File%
        ;;; FileDelete, %Name_Of_The_Temporary_File%

            Close_This_Script := 1
            ExitApp
            }
        }

    MMB_Is_Down := 0
    Return

ExitApp

), %Name_Of_The_Temporary_File%, UTF-8
FileAppend,
(

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ keyboard ███

Input_Blockade_Definitions_For_Keyboard:

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ keyboard ███ keyboard layout pop-up info inducing ones ███

Problematic_Keys_In_Temp_File := "£¤§¨´åæø"

For I, A_Key_In_Temp_File In StrSplit(Problematic_Keys_In_Temp_File)
    {
    HotKey, `%A_Key_In_Temp_File`%, Problematic_Hotkeys_In_Temp_File, UseErrorLevel
    }
Return

Problematic_Hotkeys_In_Temp_File:
Switch A_ThisHotkey
    {

;;; Case "£", "¤", "§", "¨", "´", "å", "æ", "ø":

    Case "£": Send {}
    Case "¤": Send {}
    Case "§": Send {}
    Case "¨": Send {}
    Case "´": Send {}
    Case "å": Send {}
    Case "æ": Send {}
    Case "ø": Send {}
}
Return

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ keyboard ███ alphanumeric keys ███

*a::Return
*b::Return
*c::Return
*d::Return
*e::Return
*f::Return
*g::Return
*h::Return
*i::Return
*j::Return
*k::Return
*l::Return
*m::Return
*n::Return
*o::Return
*p::Return
*q::Return
*r::Return
*s::Return
*t::Return
*u::Return
*v::Return
*w::Return
*x::Return
*y::Return
*z::Return

*0::Return
*1::Return
*2::Return
*3::Return
*4::Return
*5::Return
*6::Return
*7::Return
*8::Return
*9::Return

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ keyboard ███ control and modifiers keys ███

*Alt::Return
*AppsKey::Return
*Backspace::Return
*Break::Return
*CapsLock::Return
*Control::Return
*CtrlBreak::Return
*Delete::Return
*Down::Return
*End::Return
*Enter::Return
*Esc::Return
*Help::Return
*Home::Return
*Insert::Return
*LAlt::Return
*LControl::Return
*LShift::Return
*LWin::Return
*Launch_App1::Return
*Launch_App2::Return
*Launch_Mail::Return
*Launch_Media::Return
*Left::Return
*Pause::Return
*PgDn::Return
*PgUp::Return
*PrintScreen::Return
*RAlt::Return
*RControl::Return
*RShift::Return
*RWin::Return
*Right::Return
*ScrollLock::Return
*Shift::Return
*Sleep::Return
*Space::Return
*Tab::Return
*Up::Return

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ keyboard ███ function keys ███

*F1::Return
*F2::Return
*F3::Return
*F4::Return
*F5::Return
*F6::Return
*F7::Return
*F8::Return
*F9::Return
*F10::Return
*F11::Return
*F12::Return
*F13::Return
*F14::Return
*F15::Return
*F16::Return
*F17::Return
*F18::Return
*F19::Return
*F20::Return
*F21::Return
*F22::Return
*F23::Return
*F24::Return

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ keyboard ███ navigation keys ███

*Browser_Back::Return
*Browser_Favorites::Return
*Browser_Forward::Return
*Browser_Home::Return
*Browser_Refresh::Return
*Browser_Search::Return
*Browser_Stop::Return
;;; *Media_Next::   ; Omitted for the purpose of retaining control over playback
;;; *Media_Play_Pause::   ; Omitted for the purpose of retaining control over playback
;;; *Media_Prev::   ; Omitted for the purpose of retaining control over playback
;;; *Media_Stop::   ; Omitted for the purpose of retaining control over playback
;;; *Volume_Down::   ; Omitted for the purpose of retaining control over playback
;;; *Volume_Mute::   ; Omitted for the purpose of retaining control over playback
;;; *Volume_Up::   ; Omitted for the purpose of retaining control over playback

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ keyboard ███ numpad keys ███

*NumLock::Return
*Numpad0::Return
*Numpad1::Return
*Numpad2::Return
*Numpad3::Return
*Numpad4::Return
*Numpad5::Return
*Numpad6::Return
*Numpad7::Return
*Numpad8::Return
*Numpad9::Return

*NumpadAdd::Return
*NumpadClear::Return
*NumpadDel::Return
*NumpadDiv::Return
*NumpadDot::Return
*NumpadDown::Return
*NumpadEnd::Return
*NumpadEnter::Return
*NumpadHome::Return
*NumpadIns::Return
*NumpadLeft::Return
*NumpadMult::Return
*NumpadPgDn::Return
*NumpadPgUp::Return
*NumpadRight::Return
*NumpadSub::Return
*NumpadUp::Return

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ keyboard ███ special keys ███

*!::Return
*"::Return
*#::Return
*$::Return
`%::Return
*&::Return
*'::Return
*(::Return
`*)::Return
**::Return
*+::Return
*,::Return
*-::Return
*/::Return
*=::Return
*<::Return
*?::Return
*@::Return
*[::Return
*\::Return
*]::Return
*_::Return
``::Return
*{::Return
*|::Return
*}::Return
*~::Return
*€::Return

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ keyboard ███ special keys ███ Alt + Control + Delete ███

/*

It is currently [in version 1.1.37.00 of AutoHotkey] impossible to prevent that system wide shortcut from doing its intended action - except when using AutoHotInterception wrapper by forum user evilC [https://github.com/evilC/AutoHotInterception]

*/

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ keyboard ███ special keys ███ . > ███

.::Return
>::Return

!.::Return
<!.::Return
>!.::Return

^.::Return
<^.::Return
>^.::Return

+.::Return
<+.::Return

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ keyboard ███ special keys ███ ; : ███

`:::Return
``;::Return

SC034::Return
SC027::Return

!`;::Return
<!`;::Return
>!`;::Return

^`;::Return
<^`;::Return
>^`;::Return

+`;::Return
<+`;::Return

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ input blockade definitions ███ mouse ███

LButton::Return
;;; MButton::Return   ; The fail-safe / abort switch
RButton::Return
WheelDown::Return
WheelLeft::Return
WheelRight::Return
WheelUp::Return
XButton1::Return
XButton2::Return



), %Name_Of_The_Temporary_File%, UTF-8

FileAppend,
(

; ███  Monitor And Control Off ███ the Off code [in temp file] ███ conclusion / closure ███

ExitApp

), %Name_Of_The_Temporary_File%, UTF-8

Run, %Name_Of_The_Temporary_File%

; ███████████████████████████████████████████████████████████████████████████████████████████████████
; ███  Monitor And Control Off ███ closure ███

SetTimer, Fail_Safe_For_Closing_Of_This_AHK_File_Before_Its_Further_Execution, Off

ExitApp

; ███████████████████████████████████████████████████████████████████████████████████████████████████
; ███  Monitor And Control Off ██████████████████████████████████████████████████████████████████████
; ███████████████████████████████████████████████████████████████████████████████████████████████████

Return to “Scripts and Functions (v1)”