none
bloqueio de teclas

    Question

  • Olá, gostaria de saber como posso bloquear as teclas ALT+TAB, CTRL+ALT+DEL e ALT+F4 em um determinado form da minha aplicacao.....

     

    é VS 2005 linguagem VB

     

    Grato

    Wednesday, June 27, 2007 5:44 PM

Answers

  • Aquilo é C#. E é só converter com a ajuda de um conversor on-line como:

     

    http://www.carlosag.net/Tools/CodeTranslator/Default.aspx

     

    Aqui:

     

    Option Strict On
    Option Explicit On
     
    Imports System
    Imports System.Reflection
    Imports System.Diagnostics
    Imports System.Runtime.InteropServices
     
    Public Class KeyboardHook
        Private Structure KBDLLHOOKSTRUCT
            Public vkCode As Integer
            Public scanCode As Integer
            Public flags As Integer
            Public time As Integer
            Public dwExtraInfo As Integer
        End Structure
     
        <DllImport("user32.dll")> _
        Private Shared Function SetWindowsHookEx( _
          ByVal idHook As Integer, _
          ByVal lpfn As KeyboardHookDelegate, _
          ByVal hInstance As IntPtr, _
          ByVal threadId As UInteger) As IntPtr
        End Function
     
        <DllImport("user32.dll")> _
        Public Shared Function UnhookWindowsHookEx( _
            ByVal hhk As IntPtr) As Boolean
        End Function
     
        <DllImport("user32.dll")> _
        Private Shared Function GetAsyncKeyState( _
            ByVal vKey As Integer) As Short
        End Function
     
        <DllImport("User32.dll", CharSet:=CharSet.Auto)> _
        Private Shared Function CallNextHookEx( _
              ByVal hhk As IntPtr, _
              ByVal nCode As Integer, _
              ByVal wParam As IntPtr, _
              ByRef lParam As KBDLLHOOKSTRUCT) As Integer
        End Function
     
        Private Delegate Function KeyboardHookDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
     
        Private Const HC_ACTION As Integer = &H0
     
        Private Const LLKHF_EXTENDED As Integer = &H1
        Private Const LLKHF_INJECTED As Integer = &H10
        Private Const LLKHF_ALTDOWN As Integer = &H20
        Private Const LLKHF_UP As Integer = &H80
     
        Private Const VK_TAB As Integer = &H9
        Private Const VK_CONTROL As Integer = &H11
        Private Const VK_ALT As Integer = &H12
        Private Const VK_ESCAPE As Integer = &H1B
        Private Const VK_DELETE As Integer = &H2E
        Private Const VK_F4 As Integer = &H73
     
        Private Const WM_KEYDOWN As Integer = &H100
        Private Const WM_KEYUP As Integer = &H101
        Private Const WM_SYSKEYDOWN As Integer = &H104
        Private Const WM_SYSKEYUP As Integer = &H105
     
        Private Const WH_KEYBOARD_LL As Integer = &HD
     
        Private KeyboardHandle As IntPtr = IntPtr.Zero
     
        <MarshalAs(UnmanagedType.FunctionPtr)> _
        Private callback As KeyboardHookDelegate
     
        ' Callback
        Private Function LowLevelKeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
            If (nCode = HC_ACTION) Then
                Select Case CType(wParam, Integer)
                    Case WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP
                        ' ALT+F4 
                        If ((lParam.flags And LLKHF_ALTDOWN) <> 0) AndAlso (lParam.vkCode = VK_F4) Then
                            Debug.WriteLine("ALT+F4")
                            Return 1 ' Engole mensagem
                        End If
                        ' ALT+TAB 
                        If ((lParam.flags And LLKHF_ALTDOWN) <> 0) AndAlso (lParam.vkCode = VK_TAB) Then
                            Debug.WriteLine("ALT+TAB")
                            Return 1
                        End If
                End Select
            End If
            Return CallNextHookEx(KeyboardHandle, nCode, wParam, lParam)
        End Function
     
        Public Sub HookKeyboard()
            callback = New KeyboardHookDelegate(AddressOf LowLevelKeyboardProc)
            KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly().GetModules()(0)), CType(0, UInteger))
            If KeyboardHandle = IntPtr.Zero Then
                Throw New Exception("Hook Falhou")
            End If
        End Sub
     
        Public Sub UnhookKeyboard()
            If KeyboardHandle <> IntPtr.Zero Then
                UnhookWindowsHookEx(KeyboardHandle)
            End If
        End Sub
    End Class

     

    Para usar o exemplo em modo de depuração você *precisa* desmarcar a opção "Enable the Visual Studio hosting process" nas propriedades do projeto aba Debug (só VS2005).

     

    Para estabelecer o hook, declare no escopo do form principal:

     

    Private kh As KeyboardHook

     

    No evento Load do form principal:

     

    kh = New KeyboardHook()
    kh.HookKeyboard()

     

    No evento FormClosed do form principal:

     

    kh.UnhookKeyboard()

     

     

     

     

     

     

     

    Wednesday, June 27, 2007 7:52 PM

All replies

  • Ola Angnus

    O Código que eu vi la é para C, e estou utilizando o vb...

    é aquilo mesmo q eu quero, tem alguma coisa pro vb????

    grato

    Wednesday, June 27, 2007 6:19 PM
  • Aquilo é C#. E é só converter com a ajuda de um conversor on-line como:

     

    http://www.carlosag.net/Tools/CodeTranslator/Default.aspx

     

    Aqui:

     

    Option Strict On
    Option Explicit On
     
    Imports System
    Imports System.Reflection
    Imports System.Diagnostics
    Imports System.Runtime.InteropServices
     
    Public Class KeyboardHook
        Private Structure KBDLLHOOKSTRUCT
            Public vkCode As Integer
            Public scanCode As Integer
            Public flags As Integer
            Public time As Integer
            Public dwExtraInfo As Integer
        End Structure
     
        <DllImport("user32.dll")> _
        Private Shared Function SetWindowsHookEx( _
          ByVal idHook As Integer, _
          ByVal lpfn As KeyboardHookDelegate, _
          ByVal hInstance As IntPtr, _
          ByVal threadId As UInteger) As IntPtr
        End Function
     
        <DllImport("user32.dll")> _
        Public Shared Function UnhookWindowsHookEx( _
            ByVal hhk As IntPtr) As Boolean
        End Function
     
        <DllImport("user32.dll")> _
        Private Shared Function GetAsyncKeyState( _
            ByVal vKey As Integer) As Short
        End Function
     
        <DllImport("User32.dll", CharSet:=CharSet.Auto)> _
        Private Shared Function CallNextHookEx( _
              ByVal hhk As IntPtr, _
              ByVal nCode As Integer, _
              ByVal wParam As IntPtr, _
              ByRef lParam As KBDLLHOOKSTRUCT) As Integer
        End Function
     
        Private Delegate Function KeyboardHookDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
     
        Private Const HC_ACTION As Integer = &H0
     
        Private Const LLKHF_EXTENDED As Integer = &H1
        Private Const LLKHF_INJECTED As Integer = &H10
        Private Const LLKHF_ALTDOWN As Integer = &H20
        Private Const LLKHF_UP As Integer = &H80
     
        Private Const VK_TAB As Integer = &H9
        Private Const VK_CONTROL As Integer = &H11
        Private Const VK_ALT As Integer = &H12
        Private Const VK_ESCAPE As Integer = &H1B
        Private Const VK_DELETE As Integer = &H2E
        Private Const VK_F4 As Integer = &H73
     
        Private Const WM_KEYDOWN As Integer = &H100
        Private Const WM_KEYUP As Integer = &H101
        Private Const WM_SYSKEYDOWN As Integer = &H104
        Private Const WM_SYSKEYUP As Integer = &H105
     
        Private Const WH_KEYBOARD_LL As Integer = &HD
     
        Private KeyboardHandle As IntPtr = IntPtr.Zero
     
        <MarshalAs(UnmanagedType.FunctionPtr)> _
        Private callback As KeyboardHookDelegate
     
        ' Callback
        Private Function LowLevelKeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
            If (nCode = HC_ACTION) Then
                Select Case CType(wParam, Integer)
                    Case WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP
                        ' ALT+F4 
                        If ((lParam.flags And LLKHF_ALTDOWN) <> 0) AndAlso (lParam.vkCode = VK_F4) Then
                            Debug.WriteLine("ALT+F4")
                            Return 1 ' Engole mensagem
                        End If
                        ' ALT+TAB 
                        If ((lParam.flags And LLKHF_ALTDOWN) <> 0) AndAlso (lParam.vkCode = VK_TAB) Then
                            Debug.WriteLine("ALT+TAB")
                            Return 1
                        End If
                End Select
            End If
            Return CallNextHookEx(KeyboardHandle, nCode, wParam, lParam)
        End Function
     
        Public Sub HookKeyboard()
            callback = New KeyboardHookDelegate(AddressOf LowLevelKeyboardProc)
            KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly().GetModules()(0)), CType(0, UInteger))
            If KeyboardHandle = IntPtr.Zero Then
                Throw New Exception("Hook Falhou")
            End If
        End Sub
     
        Public Sub UnhookKeyboard()
            If KeyboardHandle <> IntPtr.Zero Then
                UnhookWindowsHookEx(KeyboardHandle)
            End If
        End Sub
    End Class

     

    Para usar o exemplo em modo de depuração você *precisa* desmarcar a opção "Enable the Visual Studio hosting process" nas propriedades do projeto aba Debug (só VS2005).

     

    Para estabelecer o hook, declare no escopo do form principal:

     

    Private kh As KeyboardHook

     

    No evento Load do form principal:

     

    kh = New KeyboardHook()
    kh.HookKeyboard()

     

    No evento FormClosed do form principal:

     

    kh.UnhookKeyboard()

     

     

     

     

     

     

     

    Wednesday, June 27, 2007 7:52 PM
  • vo tentar
    Thiii =)
    Monday, April 11, 2011 11:02 AM