将 DirectInput 代码转换为键码 .net

Convert DirectInput codes into keycodes .net

我需要找到一种将直接输入代码转换为 .net 键码的方法。原谅我,整个按键输入让我感到困惑。例如,如果我通过以下方式获取密钥:

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

并按 "C" 我得到一个键码 67。但是,我正在为 returns 以直接输入格式击键的应用程序开发一个插件。这returns"C"为46。

我需要一种将 dinput 转换为 .keycode 格式的方法。如果我的术语有误,请原谅我,但经过数小时的谷歌搜索后完全混淆了。

没关系。同时找到了一种方法。很惊讶在任何地方都找不到这个问题的答案!

    <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

使用示例(对 "C" 使用 DirectInput/Scancode):

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

生成 67(作为 "C" 的虚拟代码)