我在表单上处理CTRL-C,CTRL-V,CTRL-X时遇到问题。
Private Function HandleKeyDown(sender As Object,
e As KeyEventArgs,
ByVal vShow As String) As Boolean
HandleKeyDown = False
If e.KeyCode = Keys.F1 Then
Help.ShowPopup(Me, vShow, Cursor.Position)
End If
If e.KeyCode = Keys.C AndAlso e.Modifiers = Keys.Control Then
sender.Copy()
ElseIf e.KeyCode = Keys.V AndAlso e.Modifiers = Keys.Control Then
sender.Paste()
ElseIf e.KeyCode = Keys.X AndAlso e.Modifiers = Keys.Control Then
sender.Cut()
Else
Console.WriteLine(String.Format("Modifiers:{0} KeyCode:{1} KeyData:{2} KeyValue:{3} ", e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
End If
HandleKeyDown = True
End Function
KeyDown事件永远不会获取第二个密钥。 KeyCode似乎唯一拥有的值是Keys.Control。这是Console.WriteLine为CTRL-C
输出的内容Modifiers:Control KeyCode:ControlKey KeyData:ControlKey, Control KeyValue:17
我哪里错了?
答案 0 :(得分:1)
你是从实际的事件处理程序中调用它吗?我不得不添加"处理MyBase.KeyDown"并删除你的额外参数。
Private Sub HandleKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
'HandleKeyDown = False
If e.KeyCode = Keys.C AndAlso e.Modifiers = Keys.Control Then
MessageBox.Show(String.Format("Modifiers:{0} --- KeyCode:{1} --- KeyData:{2} --- KeyValue:{3} ",
e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
'sender.Copy()
ElseIf e.KeyCode = Keys.V AndAlso e.Modifiers = Keys.Control Then
MessageBox.Show(String.Format("Modifiers:{0} --- KeyCode:{1} --- KeyData:{2} --- KeyValue:{3} ",
e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
'sender.Paste()
ElseIf e.KeyCode = Keys.X AndAlso e.Modifiers = Keys.Control Then
MessageBox.Show(String.Format("Modifiers:{0} --- KeyCode:{1} --- KeyData:{2} --- KeyValue:{3} ",
e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
'sender.Cut()
End If
'HandleKeyDown = True
End Sub
它为我工作。我得到的值分别为88,67和86.