Stop Accidental Deletions with the BlockInput Command (AutoHotkey Tip—Part One)

Ever Wonder Why You Might Want to Block Keyboard and Mouse Input? Here’s One Reason to Use the BlockInput AutoHotkey Command, Plus the Associated Problems

I added the BlockInput command to both the BackupText.ahk and IncrementalSaveText.ahk scripts. I did this to prevent the accidental deletion of the target text by an errant press of a key.

To check out whether the command operated or not, I added a time delay to the script looking for the halting of keyboard and mouse action with BlockInput On. It didn’t work! My experiment demonstrated that the BlockInput command blocked nothing. There’s a good reason for this.

AutoHotkey Library Deal
AutoHotkey Library Deal

Microsoft designed Windows UAC (User Account Control) to prevent the unauthorized access to the inner workings and hidden mechanism of Windows—especially by nefarious scammers hiding Trojans in software or e-mail links. As it turns out, the AutoHotkey BlockInput command activates the alarms in your UAC service. Since you can use the command to stop all keyboard and mouse input, it makes sense that it would be on the list of no-nos. However, if you want to deliberately stop input from those devices to prevent accidental loss—short of turning off UAC—you must take certain steps to make BlockInput work.

The BlockInput Command

Initially, the AutoHotkey BlockInput command comes across as counterintuitive. Why would you ever want to block your keyboard and mouse? If you include the command:

BlockInput, On

both the keyboard and mouse no longer operate. If you issue this command without later adding to the same script the corresponding:

BlockInput, Off

it may force you to hard boot your computer to restore keyboard and mouse control—although, the Windows CTRL+ALT+DELETE key combination should continue active and cancel the block. (You can see the danger.)

However, when either a key press or mouse action could inadvertently cause damage, the BlockInput command comes in handy. In both of the backup script mentioned above, there’s a split second when all the text gets highlighted (Send ^a). If you happen to hit a key at that exact instant—however short—all the text gets deleted. BlockInput offers the only protection against this eventuality. But first, you need to get it functioning by giving the script higher level access. To run this type of higher level access script and get BlockInput working, you must increase the script’s privileges to Administrator.

Run As Administrator

When you open the AHK file’s Properties window (right-click on the file name and select Properties), you’ll find that the file does not offer the option to “Run this program as an Administrator.” (In fact, even if you try to run an AHK file directly as administrator, you’ll get an error.) You must first compile the script into an executable program (EXE) using the Compile Script option in the right-click menu. Then, open the Properties window for the EXE file and select the Compatibility tab.

RunAsAdmistrator
Select the Compatibility tab in the Properties window for the EXE file and check the “Run this program as an administrator” box. Click Apply and OK to close the window.

Check the “Run this program as an administrator” box. Click Apply, then OK to close the window. Now, when you run the EXE program (e.g. BackupText.exe), the BlockInput command functions properly. However, every time you run the script, UAC presents a pop-up warning similar to the following:

UACWarning

After a while, this alarm window popping up can get pretty annoying. Worse of all, the computer sits and waits until you click Yes to continue loading. Other than turning UAC off, you do have another option for eliminating the warning pop-up—although it takes a number of steps to implement: Run the script with Windows Task Scheduler.

Windows Task Scheduler

When properly setup, Windows Task Scheduler allows you to run programs at administrator level while bypassing the UAC warning window. Then, you can run the script by setting up a Windows shortcut, loading the script with the Run dialog (microsoft_key+R), or adding it to an AutoHotkey script (even an AHK file) using the AutoHotkey Run command—by either loading the Task Scheduler command directly or calling a shortcut to Task Scheduler in the script.

First open Task Scheduler (Control Panel\All Control Panel Items\Administrative Tools). In Windows 10 (and most other versions), click on the Start menu, type Task, then select Task Scheduler from the list.

Next, right-click on Task Scheduler Library and add a new folder for all your special needs AutoHotkey scripts. I cleverly called mine AutoHotkey.

Then, right-click on the new folder and select Create Task. (You could use Create Basic Task, but, later, you would need to go back and edit Properties to elevate privileges.)

TaskSchedulerCreateTask

In the General tab, give the task a name and click the “Run with highest privileges” box.

TaskSchedulerPrivileges

In the Triggers tab, click New… and select At startup if you want the script to load every time you boot the computer. (If you like, you can leave the Triggers tab empty.)

TaskSchedulerTrigger

In the Actions tab, click New… and select Start a program. Browse for the target file path to the EXE file (e.g. C:\AutoHotkey\BackupText.exe).

TaskSchedulerAction

In the Conditions tab, I unchecked everything so that the script would run without any conditions.

In the Settings tab, make sure to check the “Allow task to be run on demand” box. Make any other selections as appropriate for you situation.

TaskSchedulerOnDemand

Now, you can run the AutoHotkey EXE script without encountering the Windows warning pop-up. From the Windows Run dialog (microsoft_key+R), enter:

schtasks.exe /run /tn "\AutoHotkey\BackupText"

You might see a slight flicker of Command Prompt window when you load the file, but, when it works, the appropriate AutoHotkey icon will appear in the Windows System Tray.

To add the launch to an AutoHotkey script, insert the following line:

run C:\Windows\System32\schtasks.exe /run /tn "\AutoHotkey\BackupText"

You can also set up a Windows shortcut to run the Task Scheduler program.

Running Scheduled Task On Demand with a Windows Shortcut

By creating a Windows shortcut, you can load the script at any time through Task Scheduler with a double-click of the shortcut icon or file name without encountering the UAC pop-up warning window.

Right-click in the File Explorer windows and select New ⇒ ShortcutArrow Shortcut. Browse for a path to any file (maybe the original BackupText.exe file), click Next, then enter a name for the shortcut (e.g. BackupText). (Note: The first file path can be any file as long as it exists since you will change it in the next step.)

Once you’ve created the shortcut, right-click on it and select Properties. In the Shortcut tab, enter the corrected Target for Task Scheduler and click Apply:

C:\Windows\System32\schtasks.exe /run /tn "\AutoHotkey\BackupText"

You can also change the Run menu to Minimize which prevents the flicker of the Command Prompt window opening and closing.

TaskSchedulerShortcut

Now, a double-click of the shortcut loads the script without encountering the warning window or you can run the shortcut directly in an AutoHotkey script using the Run command:

run "C:/AutoHotkey/BackupText.lnk"

Notice that (just to make it stand out) I changed the shortcut icon to a tree.

Another BlockInput Problem

While this approach deals with the BlockInput UAC problem, that’s not the only issue with the AutoHotkey BlockInput command which needs attention. If you don’t take precautions, keys pressed during the Hotkey activation may get stuck in the down position. But, that’s a topic for next time.

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s