Okay, new fun issue of the day.
My script relies on blocking clicks and keyboard input for a certain window. If the window is active, all keyboard input is blocked. If the window isn't active, clicks are monitored globally and clicks on that window are not allowed by the script.
This works well in my uncompiled script. It also works well when compiled.... until a MsgBox appears. There seems to be something different in how the MsgBox is handled after the script is compiled. It ends up locking up the script because once the MsgBox is open, the compiled script prevents ALL clicks (and the CONSUME_WINDOW_CLICKS label is not even launched on click anymore - it stops working)
Here's a test script you can use to reproduce the issue. Open an Untitled - Notepad window, run the script, and press the hotkey. You'll be able to click around the screen just fine and you can type in any window except for the notepad window. However, when the MsgBox appears, the mouse locks up entirely - you can't click the MsgBox or any other window at that point.
Try it uncompiled and it works fine, though. The MsgBox doesn't lock anything up.
Any idea what the issue is?
Code: Select all
^a::
notepadID := WinGetID("Untitled - Notepad ahk_exe NOTEPAD.EXE")
BlockWindowInput(true, notepadID)
Sleep(10000)
Msgbox("Try clicking anywhere now...`n`n(If this is compiled, you can't. Press ENTER to close the MsgBox.)")
BlockWindowInput(false, notepadID)
Return
BlockWindowInput(cmd, winID) ;special function to block R/L clicking over a specific window (whether active or not) and also block keypresses when the window is active
{
global g_winID
If (cmd)
{
g_winID := winID
BlockUserKeys(true, "ahk_id %winID%") ;block keypresses (only when window is active)
Hotkey("IfWinActive") ;global click override
Hotkey("*vk01", "CONSUME_WINDOW_CLICKS", "On") ;left click
Hotkey("*vk02", "CONSUME_WINDOW_CLICKS", "On") ;right click
}
Else
{
BlockUserKeys(false, "ahk_id %winID%") ;unblock keypresses for window
Hotkey("IfWinActive")
Hotkey("*vk01", "CONSUME_WINDOW_CLICKS", "Off") ;left click
Hotkey("*vk02", "CONSUME_WINDOW_CLICKS", "Off") ;right click
g_WinID := ""
}
Return
CONSUME_WINDOW_CLICKS:
hk := A_ThisHotkey
MouseGetPos(_mx, _my, _mwin)
if _mwin != g_WinID
{
TrayTip("Test", "You clicked somewhere else!", 10)
;if mouse isn't over a forbidden window, send virtual click event
Send("{" . SubStr(hk, 2) . "}")
}
else
{
TrayTip("Test", "You clicked on Untitled - Notepad!", 10)
;simulate click by activating window without sending the click event to it
WinActivate("ahk_id %_mwin%")
}
RETURN
}
;block keyboard input while a specific window is active
BlockUserKeys(cmd, activeWindowTitle := "")
{
If (activeWindowTitle)
{
Hotkey("IfWinActive", activeWindowTitle) ;turn on window sensitivity
}
If (cmd)
{
Loop 511 ;Block keystrokes
Hotkey("*sc" . Format("{1:#x}", A_Index), "CONSUME_INPUT", "On")
}
Else
{
Loop 511 ;Unblock keystrokes
Hotkey("*sc" . Format("{1:#x}", A_Index), "CONSUME_INPUT", "Off")
}
If (activeWindowTitle)
{
Hotkey("IfWinActive") ;turn off hotkey window sensitivity
}
CONSUME_INPUT:
RETURN
}