1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| Imports System.Windows.Threading
Class MainWindow
Private keyRepeat As Boolean
Private keyRepeatHandler As EventHandler
Private keyRepeatTimer As New DispatcherTimer With {.Interval = TimeSpan.FromSeconds(3)}
Private ReadOnly Actions As IReadOnlyDictionary(Of Key, Action) =
New Dictionary(Of Key, Action) From {
{Key.A, AddressOf PerformAKey},
{Key.E, AddressOf PerformEKey},
{Key.S, Sub() Output.Text &= "S pressed" & Environment.NewLine},
{Key.Z, AddressOf PerformZKey}
}
Private Sub MainWindow_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.Key
Case Key.A, Key.B
Dim performKey As Action = Nothing
If Actions.TryGetValue(e.Key, performKey) Then
performKey()
End If
Case Key.E, Key.S, Key.Z, Key.Q
If Not keyRepeat AndAlso e.IsRepeat Then
keyRepeat = True
keyRepeatHandler = Sub()
Dim performKey As Action = Nothing
If Actions.TryGetValue(e.Key, performKey) Then
performKey()
MainWindow_KeyUp()
End If
End Sub
AddHandler keyRepeatTimer.Tick, keyRepeatHandler
keyRepeatTimer.Start()
End If
End Select
End Sub
Private Sub MainWindow_KeyUp() Handles Me.KeyUp
keyRepeat = False
keyRepeatTimer.Stop()
RemoveHandler keyRepeatTimer.Tick, keyRepeatHandler
End Sub
Private Sub PerformAKey()
Output.Text &= "A pressed" & Environment.NewLine
End Sub
Private Sub PerformEKey()
Output.Text &= "E pressed" & Environment.NewLine
End Sub
Private Sub PerformZKey()
Output.Text &= "Z pressed" & Environment.NewLine
End Sub
End Class |
Partager