What I am trying to do is handle an event in which an end user pounds the keyboard with his palm, fist, face, etc. Sounds ridiculous but it will be used as a supplemental safety feature in a high-power laser lab. I was thinking of watching for 4 or more keys. Examples of valid combinations:
- u{f5}7hby6
- 6g3.io{alt}mn'
- p9hyi
- {tab}2{f2}56{shift}z
- {alt}{windows}\{ctrl}{f8}
- basically anything else that can be typed with a face
I have tried this method, which doesn’t work because not all KeyUp or KeyDown events are handled especially when multiple keys are pressed.
Private keez As New List(Of Integer)
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
keez.Add(e.KeyValue)
If keez.Count >= 4 Then MessageBox.Show("I handled it!")
End Sub
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Try
keez.Remove(AscW(e.KeyChar))
Catch
End Try
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
keez.Remove(e.KeyValue)
End Sub
With this method, keys are missed constantly. I’m wondering if anyone knows of a way to handle multiple keys pressed consecutively. Most Google searches are related to standard key combos which almost always include a modifier (ctrl, alt, shift) + one additional key.
I don’t think you can handle more than 4 keys at the same time (for example, 5 letter characters at the same time will not work) – it is a hardware limitation of most today’s keyboards, i.e. how they are wired internally. Keyboards that support all keys pressed at the same time can be very expensive, and I haven’t seen one live. You can check this thread for more information. Also discussed on tomshardware.