CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2025
    Posts
    1

    Lightbulb How to turn 'a' key into 'b' key (only if 'a' is pressed more than 300ms)?

    Hi!

    I?m very new to Visual Basic, and I?m trying to accomplish something that seems quite difficult for me.
    I want to make a script so that when I press the ?A? key briefly, it behaves normally, but if I hold down the A key for more than 300 ms, it should act as if I am holding the ?B? key instead (for as long as A is pressed).

    So far, I haven?t gotten a good result, so I?ll just show where I started, hoping that someone more experienced can help me. Thanks!

    Code:
    Imports System.Runtime.InteropServices
    Module Module1
        Private Const WH_KEYBOARD_LL As Integer = 13
        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 Delegate Function LowLevelKeyboardProc(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
        Private hookID As IntPtr = IntPtr.Zero
    
        <DllImport("user32.dll")>
        Private Function SetWindowsHookEx(idHook As Integer, lpfn As LowLevelKeyboardProc, hMod As IntPtr, dwThreadId As UInteger) As IntPtr
        End Function
    
        <DllImport("user32.dll")>
        Private Function CallNextHookEx(hhk As IntPtr, nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
        End Function
    
        <DllImport("user32.dll")>
        Private Function UnhookWindowsHookEx(hhk As IntPtr) As Boolean
        End Function
    
        <DllImport("kernel32.dll")>
        Private Function GetModuleHandle(lpModuleName As String) As IntPtr
        End Function
    
        <DllImport("user32.dll")>
        Private Sub keybd_event(bVk As Byte, bScan As Byte, dwFlags As Integer, dwExtraInfo As Integer)
        End Sub
    
        Sub Main()
            hookID = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf HookCallback, GetModuleHandle(Nothing), 0)
            Windows.Forms.Application.Run()
            UnhookWindowsHookEx(hookID)
        End Sub
    
        Private Function HookCallback(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
            If nCode >= 0 Then
                Dim vkCode As Integer = Marshal.ReadInt32(lParam)
    
                If vkCode = &H41 Then ' A key
                    If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
                        keybd_event(&H42, 0, 0, 0) ' B down
                        Return New IntPtr(1) ' Block A down
                    ElseIf wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
                        keybd_event(&H42, 0, 2, 0) ' B up
                        Return New IntPtr(1) ' Block A up
                    End If
                End If
            End If
    
            Return CallNextHookEx(hookID, nCode, wParam, lParam)
        End Function
    End Module

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: How to turn 'a' key into 'b' key (only if 'a' is pressed more than 300ms)?

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Dec 2025
    Location
    17875 Von Karman Ave Suite 150, Irvine, CA 92614
    Posts
    3

    Re: How to turn 'a' key into 'b' key (only if 'a' is pressed more than 300ms)?

    To do this, you can use a keyboard hook to track when the "A" key is pressed and how long it?s held. If you hold it for more than 300ms, you can simulate the "B" key instead. You just need to set up a Windows hook to monitor key presses and switch the behavior based on the duration.

    Hope that helps! Let me know if you need more details.

Tags for this Thread

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