CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Key Block

  1. #1
    Join Date
    Oct 2005
    Posts
    19

    Question Key Block

    How do you block Ctrl+Alt+Del, Alt+Tab and the windows logo button? Im making a small program called desktopShield that blocks access to the desktop and only allow access to whatever form is tied to 'Esc'.

    Framework 2.0
    Last edited by nim52; October 15th, 2006 at 11:52 AM.

  2. #2
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Key Block

    That is only possible using either,

    1. Windows GINA.
    2. Keyboard driver.

    Both of them can not be programmed in .NET langs, only C/C++ can do that.
    Regards,
    Ramkrishna Pawar

  3. #3
    Join Date
    Oct 2005
    Posts
    19

    Re: Key Block

    I found some code in the link below that blocks Alt+Tab. I've tried to put in the Ctrl+Alt+Del combination but it doesn't seem to work. And it doesn't have a const for the windows logo key.

    http://www.codeguru.com/vb/gen/vb_sy...cle.php/c4831/

  4. #4
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Key Block

    Here is an update I made from that example.
    This really doesnt block the combo, but I have been able to modify this so you can change the ctl alt delete combo. Yes it exists in VB.NET.
    Your on your own, if you need to block the combo.

    Code:
    Imports System.Drawing
    Imports System.Threading
    Imports System.Reflection
    Imports System.Runtime.InteropServices
    Imports Microsoft.Win32
    Friend Class Form1
        Inherits System.Windows.Forms.Form
    
    ' Windows Form Designer generated code
    
     Public Structure KBDLLHOOKSTRUCT
            Public vkCode As Integer
            Public scanCode As Integer
            Public flags As Integer
            Public time As Integer
            Public dwExtraInfo As Integer
        End Structure
        Public Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
        <MarshalAs(UnmanagedType.FunctionPtr)> Private callback As KeyboardHookDelegate
        Public KeyboardHandle As Integer
        Const HC_ACTION As Integer = 0
        Const WH_KEYBOARD_LL As Integer = 13&
        Const SPI_SCREENSAVERRUNNING = 97
        Const VK_ESCAPE As Integer = &H1B
        Const VK_SHIFT As Integer = &H10
        Const VK_CONTROL As Integer = &H11
        Const VK_MENU As Integer = &H12
        Const VK_DELETE As Integer = &H2E
        Const VK_TAB As Integer = &H9 
        Const VK_LWIN As Integer = &H5B
        Const VK_RWIN As Integer = &H5C 
        Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
        Private Declare Function SystemParametersInfoByVal Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As Object, ByVal fuWinIni As Integer) As Integer
        Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
        Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As KBDLLHOOKSTRUCT) As Integer
        Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
        Dim clt() As Process = Process.GetProcessesByName("taskmgr")
        Dim t As New Thread(AddressOf KillTask)
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            On Error Resume Next
            If RadioButton1.Checked = True Then
                Call SystemParametersInfoByVal(SPI_SCREENSAVERRUNNING, 1, "1", 0)
            Else
                HookKeyboard()
            End If
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            On Error Resume Next
            If RadioButton1.Checked = True Then
                Call SystemParametersInfoByVal(SPI_SCREENSAVERRUNNING, 0, "1", 0)
            Else
                t.Abort()
                UnhookKeyboard()
            End If
        End Sub
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            On Error Resume Next
            Dim regKey As RegistryKey
            Dim regVal As String = ""
            Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Policies\System")
            regKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Policies\System", True)
            regKey.SetValue("DisableTaskMgr", 1)
            regKey.Close()
            regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
            regVal = regKey.GetValue("ScreenSaveActive")
            If regVal = "1" Then
                regKey.SetValue("ScreenSaveActive", "0")
            End If
            regKey.Close()
        End Sub
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            On Error Resume Next
            Dim regKey As RegistryKey
            Dim regVal As String
            Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Policies\System")
            regKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Policies\System", True)
            regKey.SetValue("DisableTaskMgr", 0)
            regKey.Close()
            regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
            regVal = regKey.GetValue("ScreenSaveActive")
            If regVal = "0" Then
                regKey.SetValue("ScreenSaveActive", "1")
            End If
            regKey.Close()
        End Sub
        Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
            On Error Resume Next
            If RadioButton1.Checked = True Then
                Call SystemParametersInfoByVal(SPI_SCREENSAVERRUNNING, 0, "1", 0)
            Else
                t.Abort()
                UnhookKeyboard()
            End If
        End Sub
        Public Sub KillTask()
            On Error Resume Next
            Do
                Dim clt() As Process = Process.GetProcessesByName("taskmgr")
                For Each p As Process In clt
                    p.Kill()
                Next
                System.Threading.Thread.Sleep(1)
            Loop
        End Sub
        Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
            On Error Resume Next
             'Capture combos
            If (Hookstruct.vkCode = VK_DELETE) And CBool(GetAsyncKeyState(VK_MENU) And &H8000) And CBool(GetAsyncKeyState(VK_CONTROL) And &H8000) Then
                t.Start()
                Return True
            End If
            If (Hookstruct.vkCode = VK_ESCAPE) And CBool(GetAsyncKeyState(VK_CONTROL) And &H8000) And CBool(GetAsyncKeyState(VK_SHIFT) And &H8000) Then
                t.Start()
                Return True
            End If
    
             'Capture your Alt + Tab combo here on your own, and return true
    
             'Capture all your winlogo combos, and return true for each if/end if.
    
            Return False
        End Function
        Public Sub HookKeyboard()
            callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
            KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
        End Sub
        Public Sub UnhookKeyboard()
            If (Hooked()) Then
                Call UnhookWindowsHookEx(KeyboardHandle)
            End If
        End Sub
        Private Function Hooked()
            Hooked = KeyboardHandle <> 0
        End Function
        Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
            If (Code = HC_ACTION) Then
                If (IsHooked(lParam)) Then
                    Return 1
                End If
            End If
            Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam)
        End Function
    Last edited by TT(n); October 15th, 2006 at 02:17 PM. Reason: missing constants

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Key Block

    Krishnaa, You can use Windows GINA with .NET. You can even use it with VB6, the only problem with it, is that some anti virus programs will think a Trojan is messing with it when you try to use it.
    SystemParametersInfo is the way to go..
    Last edited by HanneSThEGreaT; October 15th, 2006 at 04:55 PM.

  6. #6
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Arrow Re: Key Block

    You could always load Calc.exe as the shell interface. Talk about a desktop shield!
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  7. #7
    Join Date
    Oct 2005
    Posts
    19

    Re: Key Block

    I decided to block all keys except for the ones I specify. It works really well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured