我使用以下代码从Character
获取System.Windows.Forms.Keys
。
<DllImport("user32.dll")> _
Private Function MapVirtualKey(uCode As UInteger, uMapType As UInteger) As UInteger
End Function
<Extension> _
Public Function GetVirtualKeyAsChar(ByRef keyData As Keys) As Char
' 2 is used to translate into an unshifted character value
Return Convert.ToChar(MapVirtualKey(CUInt(keyData), 2))
End Function
但根据键盘布局或移位/控制状态,它不会返回正确的字符。例如,所有返回的字符都是大写的。如果我在文本框中使用其他输入语言,它仍会返回默认的A-Z字母。
我需要这会覆盖PreProcessMessage()函数中的某些按键行为。我知道我可以使用覆盖OnKeyPress()函数来实现正确的结果,但我真的需要在PreProcessMessage()函数中完成我的进程:
Public Overrides Function PreProcessMessage(ByRef m As Message) As Boolean
Select Case m.Msg
Case WM_KEYDOWN, WM_SYSKEYDOWN
' Extracting keys data that similar to ProcessCmdKey's keyData parameter.
Dim keyData As Keys = DirectCast(CInt(m.WParam), Keys)
Select Case keyData
Case Keys.Back
...
Case Else
Dim ch As Char = GetChar(keyData)
...