.NET 按键按下事件 [按键组合不起作用]
.NET Key Down Event [Combination of Keys not working]
我是编程新手。目前在按键组合方面存在问题。
[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
在这里卡了半天。希望高手指点解决方法
谢谢!
由于 Keys.LWin
在技术上不是修饰键,您必须使用变通方法来解决此问题。
这是一个相当简单的例子:
每次按下一个键时,将其添加到 table。
检查我们的 table 中是否存在所需的组合键,如果存在,则在按下时执行您想执行的任何操作。
释放一个键后,将其从table中删除。
像这样的东西应该可以工作:
'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 之外的其他组合键,因为 Windows 似乎没有发送它到您的应用程序,因为它也是锁定计算机的快捷方式。
我是编程新手。目前在按键组合方面存在问题。
[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
在这里卡了半天。希望高手指点解决方法
谢谢!
由于 Keys.LWin
在技术上不是修饰键,您必须使用变通方法来解决此问题。
这是一个相当简单的例子:
每次按下一个键时,将其添加到 table。
检查我们的 table 中是否存在所需的组合键,如果存在,则在按下时执行您想执行的任何操作。
释放一个键后,将其从table中删除。
像这样的东西应该可以工作:
'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 之外的其他组合键,因为 Windows 似乎没有发送它到您的应用程序,因为它也是锁定计算机的快捷方式。