multiple keypresses
-
hello everyone in my app i need to detect more than one keypresses like while key "1" is pressed i need to know if key "2" is also pressed ,ie, both keys are pressed at the same time or not? can any one help me??
TheMrProgrammer
-
hello everyone in my app i need to detect more than one keypresses like while key "1" is pressed i need to know if key "2" is also pressed ,ie, both keys are pressed at the same time or not? can any one help me??
TheMrProgrammer
The only way I know to do this is to use the API call
GetKeyboardState()
, unless someone knows of a simpler way.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
hello everyone in my app i need to detect more than one keypresses like while key "1" is pressed i need to know if key "2" is also pressed ,ie, both keys are pressed at the same time or not? can any one help me??
TheMrProgrammer
write your own code using the key_down/key_up events - the below code writes out to the debug window each key has been pressed(down). once you lift up on a key it shows it has been released(up).
Private _KeyDown As New List(Of Int16)
Private Sub TextBox1\_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If Not \_KeyDown.Contains(e.KeyCode) Then \_KeyDown.Add(e.KeyCode) Debug.Print("d:" & Chr(e.KeyValue)) End If End Sub Private Sub TextBox1\_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp If \_KeyDown.Contains(e.KeyCode) Then \_KeyDown.Remove(e.KeyCode) Debug.Print("u:" & Chr(e.KeyValue)) End If End Sub
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
-
write your own code using the key_down/key_up events - the below code writes out to the debug window each key has been pressed(down). once you lift up on a key it shows it has been released(up).
Private _KeyDown As New List(Of Int16)
Private Sub TextBox1\_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If Not \_KeyDown.Contains(e.KeyCode) Then \_KeyDown.Add(e.KeyCode) Debug.Print("d:" & Chr(e.KeyValue)) End If End Sub Private Sub TextBox1\_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp If \_KeyDown.Contains(e.KeyCode) Then \_KeyDown.Remove(e.KeyCode) Debug.Print("u:" & Chr(e.KeyValue)) End If End Sub
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
-
Nice code. I can use this to, but a question: It seem to me that the maximum numbre of simultanious keys is 3. Is this normal?
Historical. Ctrl Alt Delete Ctrl Shift (a Key) Shift AltGr (a Key) Any more than three is deemed to be elbows on the keyboard. I believe this can be altered, but alas not by me. Dave Kreskowiak may know this. Ask him,
------------------------------------ "Possessions make you poor, wealth is measurable only in experience." Sun Tzu 621BC
-
Historical. Ctrl Alt Delete Ctrl Shift (a Key) Shift AltGr (a Key) Any more than three is deemed to be elbows on the keyboard. I believe this can be altered, but alas not by me. Dave Kreskowiak may know this. Ask him,
------------------------------------ "Possessions make you poor, wealth is measurable only in experience." Sun Tzu 621BC
I adapted Larson's code just slightly (you need to disable TextBox1 by the way):
Private WithEvents \_KeyDown As New List(Of Int16) Private Sub TextBox1\_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If Not \_KeyDown.Contains(e.KeyCode) Then \_KeyDown.Add(e.KeyCode) TextBox1.Text &= Chr(e.KeyCode) End If End Sub Private Sub TextBox1\_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp If \_KeyDown.Contains(e.KeyCode) Then \_KeyDown.Remove(e.KeyCode) TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.IndexOf(Chr(e.KeyCode)), 1) End If End Sub
I managed to press more than 2 keys, but not all combos work, and some combos act like alt+ctrl etc. and other combos somehow work without a hitch.
My advice is free, and you may get what you paid for.
-
Nice code. I can use this to, but a question: It seem to me that the maximum numbre of simultanious keys is 3. Is this normal?
The is the normal max that will work reliably. If you want more than that, you're going to have to go to DirectX and it's DirectInput library, or use the XNA Framework (C#) to simplify using all the DirectX stuff.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
The is the normal max that will work reliably. If you want more than that, you're going to have to go to DirectX and it's DirectInput library, or use the XNA Framework (C#) to simplify using all the DirectX stuff.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008