BlockInput

Auto Hotkey

BlockInput

Disables or enables the user's ability to interact with the computer via keyboard and mouse.

BlockInput Mode
Command  Example: BlockInput, "On"
Function Example: BlockInput("Off")

Parameters

Mode

Mode 1: One of the following words:

On or 1 (true): The user is prevented from interacting with the computer (mouse and keyboard input has no effect).

Off or 0 (false): Input is re-enabled.

Mode 2: This mode operates independently of the other two. For example, BlockInput On will continue to block input until BlockInput Off is used, even if one of the below is also in effect.

Send: The user's keyboard and mouse input is ignored while a Send or SendRaw is in progress (the traditional SendEvent mode only). This prevents the user's keystrokes from disrupting the flow of simulated keystrokes. When the Send finishes, input is re-enabled (unless still blocked by a previous use of BlockInput On).

Mouse: The user's keyboard and mouse input is ignored while a Click, MouseMove, MouseClick, or MouseClickDrag is in progress (the traditional SendEvent mode only). This prevents the user's mouse movements and clicks from disrupting the simulated mouse events. When the mouse command finishes, input is re-enabled (unless still blocked by a previous use of BlockInput On).

SendAndMouse: A combination of the above two modes.

Default: Turns off both the Send and the Mouse modes, but does not change the current state of input blocking. For example, if BlockInput On is currently in effect, using BlockInput Default will not turn it off.

Mode 3: This mode operates independently of the other two. For example, if BlockInput On and BlockInput MouseMove are both in effect, mouse movement will be blocked until both are turned off.

MouseMove: The mouse cursor will not move in response to the user's physical movement of the mouse (DirectInput applications are a possible exception). When a script first uses this command, the mouse hook is installed (if it is not already). The mouse hook will stay installed until the next use of the Suspend or Hotkey command, at which time it is removed if not required by any hotkeys or hotstrings (see #Hotstring NoMouse).

MouseMoveOff: Allows the user to move the mouse cursor.

Remarks

Note: BlockInput On might have no effect if UAC is enabled and the script has not been run as administrator. For more information, refer to the FAQ.

In preference to BlockInput, it is often better to use SendMode Input or SendMode Play so that keystrokes and mouse clicks become uninterruptible. This is because unlike BlockInput, those modes do not discard what the user types during the send; instead, those keystrokes are buffered and sent afterward. Avoiding BlockInput also avoids the need to work around sticking keys as described in the next paragraph.

If BlockInput becomes active while the user is holding down keys, it might cause those keys to become "stuck down". This can be avoided by waiting for the keys to be released prior to turning BlockInput on, as in this example:

^!p::
KeyWait Control  ; Wait for the key to be released.  Use one KeyWait for each of the hotkey's modifiers.
KeyWait Alt
BlockInput On
; ... send keystrokes and mouse clicks ...
BlockInput Off
return

Input blocking is automatically and momentarily disabled whenever an ALT event is sent (then re-enabled afterward).

When BlockInput is in effect, user input is blocked but AutoHotkey can simulate keystrokes and mouse clicks. However, pressing Ctrl+Alt+Del will re-enable input due to a Windows API feature.

Certain types of hook hotkeys can still be triggered when BlockInput is on. Examples include MButton (mouse hook) and LWin & Space (keyboard hook with explicit prefix rather than modifiers "$#").

Input is automatically re-enabled when the script closes.

Related

SendMode, Send, Click, MouseMove, MouseClick, MouseClickDrag

Example

Function Syntax

BlockInput("on")
Run("notepad")
WinWaitActive("ahk_class Notepad")
Send("{F5}") ; pastes time and date
BlockInput("off")

Command Syntax

BlockInput, on
Run, notepad
WinWaitActive, ahk_class Notepad
Send, {F5} ; pastes time and date
BlockInput, off