Click here to Skip to main content
6,595,854 members and growing! (18,395 online)
Email Password   helpLost your password?
Languages » VB.NET » HowTo     Intermediate

How to lock keyboard and mouse on XP

By Innovathis

This is a simple explanation - how to lock the keyboard and mouse.
VB, VC7.NET 1.1, WinXPVS.NET2003, Dev
Posted:6 Sep 2004
Views:143,981
Bookmarked:45 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 4.75 Rating: 3.79 out of 5
4 votes, 22.2%
1

2
2 votes, 11.1%
3
1 vote, 5.6%
4
11 votes, 61.1%
5

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
  
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:

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

About the Author

Innovathis


Member

Occupation: Web Developer
Location: Israel Israel

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
Generalmouse lock Pinmembertrevia7:00 29 May '09  
GeneralI want to disable keyboard and mouse & enable again with keystroke combination PinmemberJimmy Fingers8:34 27 Jul '08  
GeneralA small fix! Pinmemberbigshervin16:22 17 Jul '07  
GeneralPreventing CTRL+ALT+DEL PinmemberFiziks21:53 19 May '07  
GeneralUse this code to block CTRL+ALT+DEL Pinmembersexygeek6667:32 10 Nov '06  
GeneralLine not recognized Pinmembernachtegaal999910:30 25 Apr '06  
GeneralLocking keyboard input for other applications PinmemberHimanshu Pokhariya10:56 6 Apr '06  
Generalhow can i make a keylogger?? Pinmemberihsan.net5:01 14 Jan '06  
QuestionHow lock ctrl + alt + del keys Pinmemberagostinhojr2:38 13 Sep '05  
AnswerRe: How lock ctrl + alt + del keys PinsussAnonymous10:14 16 Sep '05  
AnswerRe: How lock ctrl + alt + del keys PinmemberRotta10:42 23 Dec '05  
GeneralXP sends keys too fast PinsussAnonymous12:35 23 May '05  
GeneralRe: XP sends keys too fast Pinmemberrbowall4:38 30 Apr '07  
GeneralWhat about other windows versions? Pinmemberjungleboy1114:35 12 Jan '05  
GeneralRe: What about other windows versions? Pinmemberbartman1c13:15 3 Mar '05  
GeneralRe: What about other windows versions? PinmemberJames Mendolera17:45 17 Sep '08  
GeneralHow to unlock keyboard and mouse on XP PinsussAshley Flynn6:02 5 Jan '05  
GeneralRe: How to unlock keyboard and mouse on XP Pinmemberncanen12:32 21 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Sep 2004
Editor: Smitha Vijayan
Copyright 2004 by Innovathis
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project