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

Thread: Key Block

Threaded View

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

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