.NET Key Down事件[键组合不起作用]

时间:2017-09-27 13:55:58

标签: .net vb.net events key keydown

我是编程新手。目前在按键组合方面存在问题。

[LWin + L]

Private Sub unit_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If (e.KeyCode = Keys.L AndAlso e.KeyCode = Keys.LWin) Then
        MsgBox("Pressed!")
    End If
End Sub

不显示消息框。尝试了KeyUp,PreviewKeyDown但无济于事。

然而,它适用于其他组合,如 [Ctrl + L]

If (e.KeyCode = Keys.L AndAlso e.Modifiers = Keys.Control) Then

在这里呆了半天。希望一些专业人士可以提供解决方案。

谢谢!

1 个答案:

答案 0 :(得分:0)

由于Keys.LWin在技术上不是修饰键,因此您必须使用解决方法来解决此问题。

这是一个相当简单的问题:

  • 每按一次键,将其添加到表格中。

  • 检查我们的表中是否存在所需的组合键,如果确实存在,则按下它时要执行的任何操作。

  • 释放密钥后,将其从表格中删除。

这样的事情应该有效:

'This is our table containing all currently pressed keys.
Private KeysPressed As New HashSet(Of Keys)

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    'Add the currently pressed key to the table.
    KeysPressed.Add(e.KeyCode)

    'Determine if the combination LWin + L exists in our table (thus, if it is currently pressed)
    If KeysPressed.Contains(Keys.LWin) AndAlso KeysPressed.Contains(Keys.L) Then
        'Clear all pressed keys to avoid possible issues.
        KeysPressed.Clear()

        'Do your stuff here...
    End If
End Sub

Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
    'Remove the released key from the table.
    KeysPressed.Remove(e.KeyCode)
End Sub

你应该使用另一个组合键而不是 LWin + L ,因为它似乎没有将它发送到你的应用程序,因为它也是一个锁定的快捷方式你的电脑。