Blocking Mouse Action (AutoHotkey BlockInput Tip)

Clarification of Earlier AutoHotkey BlockInput Command Tips

While working on a couple of chapters in my new book, I noticed that I had effectively blocked inadvertent mouse movement while running a demonstration of a Windows Paint automation routine. In the script DrawSquiggle.ahk, AutoHotkey turns off the mouse while it executes various other mouse movements. Otherwise, any accidental manual movement of the mouse cursor might screw up the final result. Most importantly, the command to block mouse action worked without raising user privilege levels or running the script as an administrator.

The DrawSquiggle.ahk script demonstrates how AutoHotkey can control the selection of tools and mouse actions in Windows Paint.

DrawSquiggle.ahk is one a few short scripts written to demonstrate how to control Windows programs using Windows Paint as the example app. In this script, the function DrawSquiggle() selects the curve drawing tool (Alt, h, s, h, plus right arrow) and manipulates a line based upon the function’s parameters.

Library Benefits

This script reminded me of the rigamarole I explained in recent blogs about getting the BlockInput command operational with Windows User Account Control (UAC): Stop Accidental Deletions with the BlockInput Command (AutoHotkey Tip—Part One) and Stop Accidental Deletions with the BlockInput Command (AutoHotkey Tip—Part Two). While the extra procedures described in those pieces make blocking of all keyboard and mouse input possible, you don’t need them to merely block the mouse. You’ll find two mouse-related BlockInput command modes which don’t require elevated permissions or deactivating UAC: BlockInput, Mouse and BlockInput, MouseMove.

BlockInput, Mouse Command

The AutoHotkey BlockInput, Mouse command ignores “the user’s keyboard and mouse input while a Click, MouseMove, MouseClick, or MouseClickDrag is in progress.” You can put this mode into effect whenever you need to execute various mouse commands, yet want the freedom to manually move the cursor between actions. Turn off this mouse blocking action with BlockInput, Default.

You might find this useful in scripts which require repositioning of the mouse between actions. However, this approach remains susceptible to human error. I prefer the second option, BlockInput, MouseMove.

BlockInput, MouseMove Command

While in effect, the AutoHotkey BlockInput, MouseMove turns off the mouse completely—although AutoHotkey mouse commands continue working. This prevents mouse accidents while non-mouse commands run. Use BlockInput, MouseMoveOff to enable the mouse again. From the DrawSquiggle.ahk script:

  BlockInput, MouseMove
  SendInput {Alt}hsh
  Sleep 200
  SendInput {Home}{Right}{Enter}
  Sleep 200
  Click Down %XPos%,%YPos%
  MouseMove %MoveX%, %MoveY%, , R
  Click Up
  BlockInput, MouseMoveOff

The function DrawSquiggle() initially blocks all mouse input (BlockInput, MouseMove) and maintains that mode until completing its work (BlockInput, MouseMoveOff). Again, you don’t need to “Run as administrator.”

I find this approach easier since I don’t need to figure out which commands (e.g. Sleep 200) might succumb to incidental manual mouse movement.

No Special Actions Required

After working on this section of the book, I felt it necessary to clarify when you don’t need to elevate privilege levels and/or use Task Scheduler to run scripts using the BlockInput command. Simply blocking mouse action works great on its own.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s