How to lock keyboard and mouse on XP

Introduction

I decided to write this article because it took me a long time to find an answer to this question, and surprisingly, most places I searched gave me the answer that this task is impossible on XP!

Well, it is possible!!!

Locking the keyboard and mouse and using SendKeys to an application window

Here is a simple VB.NET code which demonstrates how to do this:

' must have this in order to use the SendKeys function
Imports System.Windows.Forms
PublicClass WinControl
' This is the function used in order to block the keyboard and mouse:
DeclareFunction BlockInput Lib"User32" _
(ByVal fBlockIt AsBoolean) AsBoolean' This function will block the keyboard and mouse untill a window with
' the specify caption will appear or the given time in seconds has
' past ( 0 seconds - wait forever).
' If the window with the caption appears than the given key is send
' to it and the input block is removed.
PublicSharedFunction Wait2Send(ByVal caption AsString, _
ByVal keys AsString, ByVal seconds AsInteger)
' Indicates if the window with the given caption was found
Dim success AsBoolean = False' Start time of the function
Dim now As DateTime = DateTime.Now
' Begining of keyboard and mouse block
BlockInput(True)
While (success = FalseAnd (DateTime.Now.Subtract(now).Seconds _
< seconds Or seconds = 0))
Try' Activating the window with desired function
' if the window is not found an exception is thrown.
AppActivate(caption)
' Sending desired key stroke to the application window
SendKeys.SendWait(keys)
' Indicates the window was found and keys sent
success = TrueCatch' Assuming window was not found and sleep for 100 miliseconds
System.Threading.Thread.Sleep(100)
EndTryEndWhile' Release the keyboard block
BlockInput(False)
EndFunctionEndClass

Here is an example of using this function:

WinControl.Wait2Send("Calculator","22*22{ENTER}",30);

In this example, the function will wait 30 seconds for some application window with the caption: "Calculator" to appear. While waiting, it locks the keyboard and mouse.

If such an application window exists or will appear in the given 30 seconds, it will be given the key stroke - 22*22 and then the ENTER special key. If you activate your calculator, you will get the result to this calculation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

About the Author

Comments and Discussions

Ive been using the sendkeys class for some time now on different systems and I found that certain systems will send the keystrokes too fast and the application will error out with incorrect keystrokes even when using the sendwait method. In particlular newer dell laptops using wireless connection. Have you found similar results and do you have any suggestions to get around this problem.

Try SendKeys.Flush() immediately after the AppActivate(caption) line as shown below:

While (success = False And (DateTime.Now.Subtract(now).Seconds < seconds Or seconds = 0))
Try
' Activating the window with desired function
' if the window is not found an exception is thrown.
AppActivate(caption)
SendKeys.Flush()
' Sending desired key stroke to the application window
SendKeys.SendWait(keys)
' Indicates the window was found and keys sent
success = True

Catch
' Assuming window was not found and sleep for 100 miliseconds
System.Threading.Thread.Sleep(100)
End Try
End While