Search code examples
vb.netvb.net-2010keypress

Is there a limit to how many key presses VB.NET can register at once?


So I'm currently designing my A-level computing project, and I need to know if VB.NET can register multiple keypresses at the same time, e.g F&J, and be able to treat them as separate keypresses. I may need anywhere up to 4 keypresses at once so if VB.NET can't do it, my program will be limited (though only slightly).

If this is possible, do i just treat it as if they weren't pressed at the same time and check for both keys individually, or is there a special way of detecting this?

Thanks in advance.


Solution

  • You can keep track of the keys you have pressed, and released.

    Create a new winforms project and add a Label. This should give you a good starting point.

    Public Class Form1
    
        Private pressedKeys As New List(Of System.Windows.Forms.Keys)()
    
        Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
            If Not pressedKeys.Contains(e.KeyCode) Then pressedKeys.Add(e.KeyCode)
            printCurrentKeys()
        End Sub
    
        Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
            If pressedKeys.Contains(e.KeyCode) Then pressedKeys.Remove(e.KeyCode)
            printCurrentKeys()
        End Sub
    
        Private Sub printCurrentKeys()
            If pressedKeys.Count > 0 Then
                Me.Label1.Text = pressedKeys.
                    Select(Of String)(Function(k) Chr(k)).
                    Aggregate(Function(s1, s2) s1 & ", " & s2)
            Else
                Me.Label1.Text = ""
            End If
        End Sub
    
    End Class
    

    enter image description here

    (The above 8 keys made possible by my anti-ghosting keyboard, the Sidewinder X4.)