将Keys.keycode转换为Virtualkeycode

时间:2015-12-30 11:30:07

标签: vb.net keydown keycode

绕着这一圈走了一圈。无法发布任何可靠的代码,因为尝试了很多不同的方法。基本上,尝试通过(示例代码)获取击键代码(虚拟和键):

Private Sub DGVKeyHook(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs) Handles EmuKeyRemapDGV.PreviewKeyDown

        dim VCode as integer
        dim VCodeName as string
        dim KCode as integer
        dim KCodeName as String 

        Dim kc As New KeysConverter

        KCode = e.keycode
        KCodeName = kc.ConvertToString(e.KeyCode)
        'How to get the rest?

end sub

我需要在Shift,Ctrl,Win + Alt上保留左右区别。我知道Key.KeyCode无法做到这一点。如果可能,也希望获得虚拟键码名称(例如" VK_LSHIFT")。

这里有一些我为了尝试实现这个而搞砸的代码,但是所有这些代码都很混乱 - 不确定这些代码与试图实现的内容有什么关系:

<DllImport("user32.dll", EntryPoint:="VkKeyScanEx", CharSet:=CharSet.Unicode)>
Friend Shared Function GetKeyCode(
                   ByVal c As Char,
                   Optional ByVal KeyboardLayout As UIntPtr = Nothing
     ) As Short
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function MapVirtualKey(ByVal uCode As Integer, ByVal nMapType As Integer) As Integer
End Function

Public Shared Function GetLoByte(ByVal value As Short) As Byte
    Return BitConverter.GetBytes(value).First
End Function

 ' and in keys block:

        Dim RawVCode As Integer = GetKeyCode(ChrW(e.KeyCode))
        Dim LowVCode As Integer = GetLoByte(GetKeyCode(ChrW(e.KeyCode)))

        cfdb.echo("Raw Vcode: " & RawVCode)
        cfdb.echo("Low Vcode: " & LowVCode)
        cfdb.echo("MAPVK_VK_TO_VSC>>>>>>" & MapVirtualKey(e.KeyCode, 0))

帮助我真的很感激这个泥潭!

0 个答案:

没有答案