.netkeycodedirectinput

Convert DirectInput codes into keycodes .net


I need to find a way to convert directinput codes into .net keycodes. Forgive me, the whole key input thing baffles me. The example would be if i grab a key via:

Private Sub GetKey(sender As Object, e As PreviewKeyDownEventArgs) Handles SnapKeyName.PreviewKeyDown
        Dim KeyCode = e.KeyCode
End Sub

and press "C" I get a keycode of 67. However, I am developing a plugin for an app that returns keystrokes in directinput format. This returns "C" as 46.

I need a way to convert dinput into .keycode format. Forgive me if my terminology is wrong, but totally confused after hours of googling.


Solution

  • Nevermind. Found a way in the mean-time. Astonished couldn't find an answer to this anywhere!

        <DllImport("user32.dll")>
        Private Shared Function MapVirtualKeyEx(uCode As UInteger, uMapType As MapVirtualKeyMapTypes, dwhkl As IntPtr) As UInteger
        End Function
    
        ''' <summary>
        ''' The set of valid MapTypes used in MapVirtualKey
        ''' </summary>
        Public Enum MapVirtualKeyMapTypes As UInteger
            ''' <summary>
            ''' uCode is a virtual-key code and is translated into a scan code.
            ''' If it is a virtual-key code that does not distinguish between left- and
            ''' right-hand keys, the left-hand scan code is returned.
            ''' If there is no translation, the function returns 0.
            ''' </summary>
            MAPVK_VK_TO_VSC = &H0
    
            ''' <summary>
            ''' uCode is a scan code and is translated into a virtual-key code that
            ''' does not distinguish between left- and right-hand keys. If there is no
            ''' translation, the function returns 0.
            ''' </summary>
            MAPVK_VSC_TO_VK = &H1
    
            ''' <summary>
            ''' uCode is a virtual-key code and is translated into an unshifted
            ''' character value in the low-order word of the return value. Dead keys (diacritics)
            ''' are indicated by setting the top bit of the return value. If there is no
            ''' translation, the function returns 0.
            ''' </summary>
            MAPVK_VK_TO_CHAR = &H2
    
            ''' <summary>
            ''' Windows NT/2000/XP: uCode is a scan code and is translated into a
            ''' virtual-key code that distinguishes between left- and right-hand keys. If
            ''' there is no translation, the function returns 0.
            ''' </summary>
            MAPVK_VSC_TO_VK_EX = &H3
    
            ''' <summary>
            ''' Not currently documented
            ''' </summary>
            MAPVK_VK_TO_VSC_EX = &H4
        End Enum
    

    Example use (using DirectInput/Scancode for "C"):

    debug.writeline("VK for Scancode 46: " & MapVirtualKeyEx(46, MapVirtualKeyMapTypes.MAPVK_VSC_TO_VK, Nothing)"
    

    Produces 67 (being the Virtual Code for "C")