Results 1 to 40 of 77

Thread: How do I... Disable Control Alt Delete?

Threaded View

  1. #11
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    How to swap keyboard keys, ie press A but send Z

    'Here is an example of how to SwapKeys.
    'It's somewhat like SwapMouseButtons, but only for the keyboard.

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            HookKeyboard()
        End Sub
        Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
            UnhookKeyboard()
        End Sub
    
        Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
            On Error Resume Next
    
            If Hookstruct.flags <> 128 Then 'Do this on key down only.  the flag for key up is 0.
                If Hookstruct.vkCode = Keys.A Then 'If a key is pressed
                    apikeybd_event(Keys.Z, 0, 0, 0) 'press any key. (z in this example)
                    Return True
                End If
    
                'Repeat this skeleton for any key, or combo!!!  
                'If Hookstruct.vkCode = Keys.A Then
                '    apikeybd_event(Keys.Z, 0, 0, 0)
                '    Return True
                'End If
            End If
    
            Return False
        End Function
        Public Structure KBDLLHOOKSTRUCT
            Public vkCode, scanCode, flags, time, dwExtraInfo As Int32
        End Structure
        Public Delegate Function KeyboardHookDelegate(ByVal Code As Int32, ByVal wParam As Int32, ByRef lParam As KBDLLHOOKSTRUCT) As Int32
        <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.FunctionPtr)> Private callback As KeyboardHookDelegate
        Public KeyboardHandle As Int32
        Const HC_ACTION As Int32 = 0
        Const WH_KEYBOARD_LL As Int32 = 13
        Const VK_CONTROL As Int32 = 17
        Public Declare Function apiSetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Int32, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Int32, ByVal dwThreadId As Int32) As Int32
        Public Declare Function apiCallNextHookEx Lib "user32" Alias "CallNextHookEx" (ByVal hHook As Int32, ByVal nCode As Int32, ByVal wParam As Int32, ByVal lParam As KBDLLHOOKSTRUCT) As Int32
        Public Declare Function apiUnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" (ByVal hHook As Int32) As Int32
        Public Declare Sub apikeybd_event Lib "user32.dll" Alias "keybd_event" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Int32, ByVal dwExtraInfo As Int32)
        Public Sub HookKeyboard()
            callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
            KeyboardHandle = apiSetWindowsHookEx(WH_KEYBOARD_LL, callback, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.[Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
        End Sub
        Public Sub UnhookKeyboard()
            If KeyboardHandle <> 0 Then apiUnhookWindowsHookEx(KeyboardHandle)
        End Sub
        Public Function KeyboardCallback(ByVal Code As Int32, ByVal wParam As Int32, ByRef lParam As KBDLLHOOKSTRUCT) As Int32
            If Code = HC_ACTION AndAlso IsHooked(lParam) = True Then Return 1
            Return apiCallNextHookEx(KeyboardHandle, Code, wParam, lParam)
        End Function

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