[SOLVED] Turn off the monitor (and block mouse movement) when pressing "Win+Z"

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DSpider
Posts: 5
Joined: 24 Nov 2016, 16:46

[SOLVED] Turn off the monitor (and block mouse movement) when pressing "Win+Z"

24 Nov 2016, 17:35

Hello.

I'm trying to create a script that turns off the monitor when pressing "Win+Z". It also needs to block mouse movement, in order to make sure that the monitor doesn't turn on accidentally when moving the mouse, or if the mouse cursor happens to twitch for a millisecond, or if there's too much light passing through the window and activates the mouse (e.g. the sun coming out of the clouds or something like that). It's an optical mouse.

Here's what I got so far:

Code: Select all

#NotrayIcon

#z::
Sleep 700  ; optional, so it doesn't feel too jarring

BlockInput, MouseMove  ; turn off the mouse movement and mouse buttons
Hotkey, LButton, nothing, On
Hotkey, MButton, nothing, On
Hotkey, RButton, nothing, On
Hotkey, XButton1, nothing, On
Hotkey, XButton2, nothing, On
Hotkey, WheelUp, nothing, On
Hotkey, WheelDown, nothing, On

Sleep 200  ; required, apparently... At least on Windows 10. It just opens the start menu otherwise (probably due to the Win key from the "Win+Z" key combination)...

SendMessage, 0x112, 0xF170, 2,, Program Manager ; 0x112 is WM_SYSCOMMAND, 0xF170 is SC_MONITORPOWER <-- this is where the magic happens :)

; Press any key to continue...
Input, SingleKey, L1, {LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}{BS}{Capslock}{Numlock}{PrintScreen}{Pause}

BlockInput, MouseMoveOff  ; restore mouse movement and the usage of mouse buttons
Hotkey, LButton, nothing, Off
Hotkey, MButton, nothing, Off
Hotkey, RButton, nothing, Off
Hotkey, XButton1, nothing, Off
Hotkey, XButton2, nothing, Off
Hotkey, WheelUp, nothing, Off
Hotkey, WheelDown, nothing, Off
Nothing:
Return
The problem is, I run the script, the monitor turns off, but when I move the mouse (or press any mouse button/use the scroll wheel), the monitor turns on again... Except that it shouldn't. And I have no idea why. The mouse movement is still restricted after the monitor turns on and pressing the mouse buttons has no effect until pressing any key. Why isn't the mouse movement blocked during the monitor power off state? Is there a workaround?
Last edited by DSpider on 29 Dec 2016, 18:17, edited 1 time in total.
Guest

Re: Turn off the monitor (and block mouse movement) when pressing "Win+Z"

25 Nov 2016, 05:20

Helgef wrote:Perhaps run script as administrator.
Unfortunately, no, it still presents the same problem.
Tested at work on a Windows 7, but having the same issue at home on Windows 10.

Is there another way to (temporarily) disable the mouse, system-wise? Unhook some modules, or disable a certain USB port?
DSpider
Posts: 5
Joined: 24 Nov 2016, 16:46

Re: Turn off the monitor (and block mouse movement) when pressing "Win+Z"

25 Nov 2016, 05:23

Helgef wrote:Perhaps run script as administrator.
Unfortunately, no, it still presents the same problem.
Tested at work on a Windows 7, but having the same issue at home on Windows 10.

Is there another way to (temporarily) disable the mouse, system-wise? Unhook some modules, or disable a certain USB port?

PS: Sorry, forgot to log in.
DSpider
Posts: 5
Joined: 24 Nov 2016, 16:46

Re: Turn off the monitor (and block mouse movement) when pressing "Win+Z"

25 Nov 2016, 08:28

The issue also occurs with 1.1.24.03 (the latest version right now), either run as administrator or not.

I was using 1.1.21.00 at work (installed a year and a half ago), and whatever version was available in August or September this year (can't check right now). Same behaviour.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Turn off the monitor (and block mouse movement) when pressing "Win+Z"

25 Nov 2016, 09:54

I can imagine any input signal is interpreted as a wake up event, regardless of how the signal in processed afterwards.
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: Turn off the monitor (and block mouse movement) when pressing "Win+Z"

27 Nov 2016, 21:52

DSpider wrote:Hello.
It also needs to block mouse movement, in order to make sure that the monitor doesn't turn on accidentally when moving the mouse, or if the mouse cursor happens to twitch for a millisecond, or if there's too much light passing through the window and activates the mouse (e.g. the sun coming out of the clouds or something like that). It's an optical mouse.
Is there a specific reason you want to do this through AHK rater than just turning wake on mousemovemt off in your computer? its seem like a rather complicated solution if its just for your own PC

Image
DSpider
Posts: 5
Joined: 24 Nov 2016, 16:46

Re: Turn off the monitor (and block mouse movement) when pressing "Win+Z"

29 Dec 2016, 18:16

The "Allow this device to wake the computer" checkbox is unchecked but even after a reboot, the issue persists. I think it's because... maybe I'm not placing my PC into sleep mode? The mouse still turns the monitor on (even if the mouse is BLOCKED from moving). I don't know why.

Here's my solution:

1. Download "devcon.exe". I found this great post that said to download 787bee96dbd26371076b37b13c405890.cab, extract the file "filbad6e2cce5ebc45a401e19c613d0a28f", and rename it "devcon.exe" (Windows 7/8/8.1/10, 64 bits). I extracted it on the desktop, for testing purposes.

2. Open the Device Manager (Win+R, run "devmgmt.msc"), open "Mice and other pointing devices", right click the name of your mouse -> Properties, click the "Events" tab, look for anything that says something like "Device HID\VID_1532&PID_0043&MI_00\7&2088c668&0&0000 was configured" (obviously, yours will be different).

3. Create these VBScript files on your desktop: "mouse_disable.vbs" and "mouse_enable.vbs":

Code: Select all

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd /c C:\Users\[your_user_name]\Desktop\devcon.exe /r disable *HID\VID_1532*", 0

Code: Select all

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd /c C:\Users\[your_user_name]\Desktop\devcon.exe /r enable *HID\VID_1532*", 0
Naming is important here. Don't use any spaces for the .vbs file names, and obviously don't use the same "HID\VID_1532" (see step #2). Note: .bat files could have been used, but by using .vbs files you don't get that nasty black command prompt window.

4. Open the Task Scheduler (Win+R, run "taskschd.msc"), and create tasks for the above files using the "Run with highest privileges" checkbox checked (so that you're not prompted with that yellow UAC yes/no window every time you use the hotkey). For example:

Image
Image
As you can see, the "Action" tab contains just the path to the .vbs files. Nothing else. Same with the one that enables the mouse.

5. Run the following AutoHotkey script:

Code: Select all

#NotrayIcon

; Press Win+Z to turn off the monitor:

#z::
Run, schtasks.exe /Run /TN "Mouse Disable"
Sleep 700
SendMessage, 0x112, 0xF170, 2,, Program Manager ; 0x112 is WM_SYSCOMMAND, 0xF170 is SC_MONITORPOWER

; Press any key to continue:

Input, SingleKey, L1, {LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}{BS}{Capslock}{Numlock}{PrintScreen}{Pause}

Run, schtasks.exe /Run /TN "Mouse Enable"
Return
Thanks for the help, guys! If you feel that it can be improved in some way or simplified, please let me know. Thanks again!
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: [SOLVED] Turn off the monitor (and block mouse movement) when pressing "Win+Z"

29 Dec 2016, 18:39

Nice write-up of combining AHK+VBS+TaskScheduler+DevCon to get everything just right. :thumbup:
DSpider
Posts: 5
Joined: 24 Nov 2016, 16:46

Re: [SOLVED] Turn off the monitor (and block mouse movement) when pressing "Win+Z"

29 Dec 2016, 19:46

Thanks. :)

I moved the .vbs files somewhere on C: that didn't include spaces in the file path (else it wouldn't work), and moved "devcon.exe" in one of the Windows %PATH% locations (e.g. C:\Windows\System32) - which makes the .vbs files tidier without that long "C:\Users\[your_user_name]\Desktop\devcon.exe" path. Pretty happy with the result.

Too bad AHK's "BlockInput" doesn't prevent the mouse from waking up the computer/turning the monitor back on... It would've taken less time to find an alternative.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: [SOLVED] Turn off the monitor (and block mouse movement) when pressing "Win+Z"

28 Nov 2017, 16:16

Since I got a new Logitech G402 I've also experienced the problem of monitors waking up from standby because of an over-sensitive sensor (tried three mice already). I've adopted your method to automatically run on logon/logoff. My script also runs as admin.

Code: Select all

WM_WTSSESSION_CHANGE(wParam, lParam, Msg, hWnd){
	;Disable mouse while lock workstation and put monitor to sleep
	;Download: https://download.microsoft.com/download/8/1/6/816FE939-15C7-4185-9767-42ED05524A95/wdk/Installers/787bee96dbd26371076b37b13c405890.cab
	;Extract filbad6e2cce5ebc45a401e19c613d0a28f
	;Save to devcon.exe in program files
	;Update VID of your mouse, retrieve value from devmgmt.msc
	static init:=(DllCall( "Wtsapi32.dll\WTSRegisterSessionNotification", UInt, A_ScriptHwnd, UInt, 1) && OnMessage(0x02B1, "WM_WTSSESSION_CHANGE"))
	,_:={base:{__Delete: "WM_WTSSESSION_CHANGE"}}
	
	if !(_)
		DllCall("Wtsapi32.dll\WTSUnRegisterSessionNotification", "UInt", hWnd)
	
	If (wParam=0x6 || wParam=0x7){ ;Logoff or lock
		Run, "%A_ProgramFiles%\devcon.exe" disable HID\VID_046D* ,,Hide ;Disable mouse
		SendMessage,0x112,0xF170,2,,Program Manager ;Monitor Standby
	}Else If (wParam=0x5 || wParam=0x8){ ;Logon or unlock
		Run, "%A_ProgramFiles%\devcon.exe" enable HID\VID_046D* ,,Hide ;Enable mouse
	}
}
popyoung
Posts: 2
Joined: 09 Jan 2023, 04:39
Contact:

Re: [SOLVED] Turn off the monitor (and block mouse movement) when pressing "Win+Z"

09 Jan 2023, 05:03

Nextron wrote:
28 Nov 2017, 16:16
Since I got a new Logitech G402 I've also experienced the problem of monitors waking up from standby because of an over-sensitive sensor (tried three mice already). I've adopted your method to automatically run on logon/logoff. My script also runs as admin.

Code: Select all

WM_WTSSESSION_CHANGE(wParam, lParam, Msg, hWnd){
	;Disable mouse while lock workstation and put monitor to sleep
	;Download: https://download.microsoft.com/download/8/1/6/816FE939-15C7-4185-9767-42ED05524A95/wdk/Installers/787bee96dbd26371076b37b13c405890.cab
	;Extract filbad6e2cce5ebc45a401e19c613d0a28f
	;Save to devcon.exe in program files
	;Update VID of your mouse, retrieve value from devmgmt.msc
	static init:=(DllCall( "Wtsapi32.dll\WTSRegisterSessionNotification", UInt, A_ScriptHwnd, UInt, 1) && OnMessage(0x02B1, "WM_WTSSESSION_CHANGE"))
	,_:={base:{__Delete: "WM_WTSSESSION_CHANGE"}}
	
	if !(_)
		DllCall("Wtsapi32.dll\WTSUnRegisterSessionNotification", "UInt", hWnd)
	
	If (wParam=0x6 || wParam=0x7){ ;Logoff or lock
		Run, "%A_ProgramFiles%\devcon.exe" disable HID\VID_046D* ,,Hide ;Disable mouse
		SendMessage,0x112,0xF170,2,,Program Manager ;Monitor Standby
	}Else If (wParam=0x5 || wParam=0x8){ ;Logon or unlock
		Run, "%A_ProgramFiles%\devcon.exe" enable HID\VID_046D* ,,Hide ;Enable mouse
	}
}
Logitech mouse +1
I turned this script to V2.02, but it doesn't work.
Since I am partly new to AHK, there are some questions in this code.

1. There is a definition of a function but no one calls it. Maybe I need to call WM_WTSSESSION_CHANGE? But it looks like a win32 message and I don't know what those four parameters should be.
2. What is A_ScriptHwnd for?

Code: Select all

WM_WTSSESSION_CHANGE(wParam, lParam, Msg, hWnd){
	;Disable mouse while lock workstation and put monitor to sleep
	;Download: https://download.microsoft.com/download/8/1/6/816FE939-15C7-4185-9767-42ED05524A95/wdk/Installers/787bee96dbd26371076b37b13c405890.cab
	;Extract filbad6e2cce5ebc45a401e19c613d0a28f
	;Save to devcon.exe in program files
	;Update VID of your mouse, retrieve value from devmgmt.msc
	static init:=(DllCall( "Wtsapi32.dll\WTSRegisterSessionNotification", "UInt", A_ScriptHwnd, "UInt", 1) && OnMessage(0x02B1, WM_WTSSESSION_CHANGE))
	,_:={base:{__Delete: "WM_WTSSESSION_CHANGE"}}
	if !(_)
		DllCall("Wtsapi32.dll\WTSUnRegisterSessionNotification", "UInt", hWnd)
	
	If (wParam=0x6 || wParam=0x7){ ;Logoff or lock
		Run('"%A_ProgramFiles%\devcon.exe" disable HID\VID_046D*' ,,"Hide") ;Disable mouse
		SendMessage(0x112,0xF170,2,,"Program Manager") ;Monitor Standby
	}Else If (wParam=0x5 || wParam=0x8){ ;Logon or unlock
		Run('"%A_ProgramFiles%\devcon.exe" enable HID\VID_046D*' ,,"Hide") ;Enable mouse
	}
}
popyoung
Posts: 2
Joined: 09 Jan 2023, 04:39
Contact:

Re: [SOLVED] Turn off the monitor (and block mouse movement) when pressing "Win+Z"

09 Jan 2023, 05:25

I adopted an easy approach to it and succeeded. Anyway, I will still appreciate your answers to my questions.

Code: Select all


KeyWaitAny(Options:="")
{
    ih := InputHook(Options)
    if !InStr(Options, "V")
        ih.VisibleNonText := false
    ih.KeyOpt("{All}", "E")
    ih.Start()
    ih.Wait()
    return ih.EndKey
}
Run('"devcon.exe" disable HID\VID_046D*' ,,"Hide") ;Disable mouse
SendMessage 0x0112, 0xF170, 2,, "Program Manager"  ; 0x0112 is WM_SYSCOMMAND, 0xF170 is SC_MONITORPOWER.
KeyWaitAny("V")
Run('"devcon.exe" enable HID\VID_046D*' ,,"Hide") ;Enable mouse


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, CuriousDad and 108 guests