Solved

Lock Keyboard and Mouse

Posted on 2006-06-16
6
1,317 Views
Last Modified: 2008-01-09
I just want to lock keyboard and mouse via network..

Just like Internet Cafe Applications..

Thanks..
0
Comment
Question by:el_hey
  • 3
6 Comments
 
LVL 2

Expert Comment

by:cmjwebservices
ID: 16925717
An adaptation taken from http://www.codeproject.com/vb/net/LockKeyboard.asp.  Add the following class to your form:

Public Class clsLockKeyboard

    ' 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 until 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
0
 
LVL 2

Expert Comment

by:cmjwebservices
ID: 16925730
Then add the following form to implement the above (a simple test):

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Imports ProgressForm.clsLockKeyboard
Imports System.Threading

Public Class frmLockKeyboard
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnLock As System.Windows.Forms.Button
    Friend WithEvents txtTest As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnLock = New System.Windows.Forms.Button
        Me.txtTest = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'btnLock
        '
        Me.btnLock.Location = New System.Drawing.Point(104, 8)
        Me.btnLock.Name = "btnLock"
        Me.btnLock.TabIndex = 0
        Me.btnLock.Text = "Lock"
        '
        'txtTest
        '
        Me.txtTest.Location = New System.Drawing.Point(8, 48)
        Me.txtTest.Name = "txtTest"
        Me.txtTest.Size = New System.Drawing.Size(272, 20)
        Me.txtTest.TabIndex = 1
        Me.txtTest.Text = "Click 'Lock' and try to type here ;)"
        '
        'frmLockKeyboard
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 78)
        Me.Controls.Add(Me.txtTest)
        Me.Controls.Add(Me.btnLock)
        Me.Name = "frmLockKeyboard"
        Me.Text = "frmLockKeyboard"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnLock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLock.Click
        ProgressForm.clsLockKeyboard.BlockInput(True)
        System.Threading.Thread.Sleep(8000)
        ProgressForm.clsLockKeyboard.BlockInput(False)
        Me.txtTest.Text = "Keyboard and mouse unlocked."
    End Sub

End Class

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Good luck!
0
 
LVL 1

Author Comment

by:el_hey
ID: 16948632
I have tested your codes and found out that it can be break by a ctrl-alt-delete function.
0
 
LVL 2

Accepted Solution

by:
cmjwebservices earned 300 total points
ID: 16950794
Ctrl-Alt-Del is a SAS (Secure Attention Sequence) and cannot be disabled without writing a GINA DLL.  If you know C++ then this can be done.  Otherwise, there is no way to use a keyboard hook and trap Ctrl-Alt-Del...
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now