将DirectInput代码转换为密钥代码.net

时间:2017-05-15 21:10:29

标签: .net keycode directinput

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

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

并按" C"我得到一个67的密钥代码。但是,我正在开发一个应用程序的插件,它以直接输入格式返回击键。这将返回" C"作为46.

我需要一种方法将dinput转换为.keycode格式。请原谅我,如果我的术语是错误的,但在谷歌搜索后几个小时完全混淆。

1 个答案:

答案 0 :(得分:0)

没关系。在平均时间找到了一条路。惊讶无法在任何地方找到答案!

    <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

使用示例(使用DirectInput / Scancode代替“C”):

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

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