Click here to Skip to main content
15,860,861 members
Articles / Programming Languages / Visual Basic
Article

How to lock keyboard and mouse on XP

Rate me:
Please Sign up or sign in to vote.
3.79/5 (18 votes)
6 Sep 2004 253K   57   19
This is a simple explanation - how to lock the keyboard and mouse.

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:

VB
' must have this in order to use the SendKeys function
Imports System.Windows.Forms
  
Public Class WinControl

   ' This is the function used in order to block the keyboard and mouse:
    Declare Function BlockInput Lib "User32" _
           (ByVal fBlockIt As Boolean) As Boolean
    ' 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.
    Public Shared Function Wait2Send(ByVal caption As String, _
              ByVal keys As String, ByVal seconds As Integer)

    ' Indicates if the window with the given caption was found
        Dim success As Boolean = False

    ' Start time of the function
        Dim now As DateTime = DateTime.Now

    ' Begining of keyboard and mouse block
        BlockInput(True)

        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)

                ' 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
    ' Release the keyboard block
        BlockInput(False)

    End Function

End Class

Here is an example of using this function:

C#
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.

You can get further help on:

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.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionlock keyboard and mouse Pin
bsoni1818-Nov-11 0:54
bsoni1818-Nov-11 0:54 
Generalmouse lock Pin
trevia29-May-09 6:00
trevia29-May-09 6:00 
GeneralI want to disable keyboard and mouse & enable again with keystroke combination Pin
Jimmy Fingers27-Jul-08 7:34
Jimmy Fingers27-Jul-08 7:34 
GeneralA small fix! Pin
bigshervin17-Jul-07 15:22
bigshervin17-Jul-07 15:22 
GeneralPreventing CTRL+ALT+DEL Pin
Fiziks19-May-07 20:53
Fiziks19-May-07 20:53 
GeneralUse this code to block CTRL+ALT+DEL Pin
sexygeek66610-Nov-06 6:32
sexygeek66610-Nov-06 6:32 
GeneralLine not recognized Pin
nachtegaal999925-Apr-06 9:30
nachtegaal999925-Apr-06 9:30 
GeneralLocking keyboard input for other applications Pin
Himanshu Pokhariya6-Apr-06 9:56
Himanshu Pokhariya6-Apr-06 9:56 
Questionhow can i make a keylogger?? Pin
ihsan.net14-Jan-06 4:01
ihsan.net14-Jan-06 4:01 
QuestionHow lock ctrl + alt + del keys Pin
agostinhojr13-Sep-05 1:38
agostinhojr13-Sep-05 1:38 
AnswerRe: How lock ctrl + alt + del keys Pin
Anonymous16-Sep-05 9:14
Anonymous16-Sep-05 9:14 
AnswerRe: How lock ctrl + alt + del keys Pin
Rotta23-Dec-05 9:42
Rotta23-Dec-05 9:42 
GeneralXP sends keys too fast Pin
Anonymous23-May-05 11:35
Anonymous23-May-05 11:35 
GeneralRe: XP sends keys too fast Pin
rbowall30-Apr-07 3:38
rbowall30-Apr-07 3:38 
QuestionWhat about other windows versions? Pin
jungleboy11112-Jan-05 3:35
jungleboy11112-Jan-05 3:35 
AnswerRe: What about other windows versions? Pin
bartman1c3-Mar-05 12:15
bartman1c3-Mar-05 12:15 
AnswerRe: What about other windows versions? Pin
James Mendolera17-Sep-08 16:45
James Mendolera17-Sep-08 16:45 
QuestionHow to unlock keyboard and mouse on XP Pin
Ashleyflynn5-Jan-05 5:02
Ashleyflynn5-Jan-05 5:02 
AnswerRe: How to unlock keyboard and mouse on XP Pin
ncanen21-Jul-06 11:32
ncanen21-Jul-06 11:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.