In this tutorial I'll show you to use the Keyboard key events to get info about when a key is pressed and which key it is. There's 4 different key events KeyDown, KeyPressed, KeyUp and PreviewKeyDown. The first and the third one is working the same while the last one is a very little bit different but the mayor thing with the last one is that it's called first, then the Normal KeyDown and then the KeyPressed Event, if the key isn't released these three events will continue to be called in this order. Then when the key is released the KeyUp event is called. The KeyPressed event has a lot fewer functions then the other three, it have almost nothing but a good thing you can do with it which you can't with the others is to get what text the press should have generated, more to that later.
Get the Key's Value
To get the key which was pressed you can use e.KeyCode, e.KeyValue and e.KeyData. KeyCode and KeyData are enums so they are probably easier to use since they key's numbers doesn't follow any easy order. e.KeyCode, e.KeyValue and e.KeyData works for PreviewKeyDown, KeyDown and KeyUp but not for KeyPressed. An example on how you can use it:
[highlight=VB.NET] Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.W
MessageBox.Show("Move forward")
Case Keys.A
MessageBox.Show("Move left")
Case Keys.S
MessageBox.Show("Move back")
Case Keys.D
MessageBox.Show("Move right")
End Select
End Sub[/highlight]
Shift, Control and Alt
When using the events PreviewKeyDown, KeyDown and KeyUp you can use e.Shift, e.Control and e.Alt to get a boolean value indicating if Shift is down, Control is down and if Alt is down. You can also get the same thing by using My.Computer.Keyboard.ShiftKeyDown, My.Computer.Keyboard.ControlKeyDown and My.Computer.Keyboard.AltKeyDown and this could be used from everywhere not only from inside these three events. An example using the first way:
[highlight=VB.NET] Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
MessageBox.Show("Alt: " & e.Alt & " Ctr: " & e.Control & " Shift: " & e.Shift)
End Sub[/highlight]
Caps Lock, Scroll Lock and Num Lock
You can also use My.Computer.Keyboard.CapsLock, My.Computer.Keyboard.ScrollLock and My.Computer.Keyboard.NumLock to receive boolean values indicating if the "locks" is enabled or not.
e.KeyChar
e.KeyChar can only be used by the KeyPressed Event. It will return the same thing as when you're typing with your keyboard. For example, if you're pressing the a button it will return "a" but if you at the same time has CapsLock enabled or holding shift down it will automatically return "A" instead. So this could be used if you want to allow a user to write somewhere else then a textbox or something similar. The simple example below will use e.KeyChar to allow the user to type in a label:
[highlight=VB.NET] Private Sub frmMain_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
lblTest.Text &= e.KeyChar
End Sub[/highlight]
SendKeys
You can also simulate keystrokes. If you use My.Computer.Keyboard.SendKeys("a") for example it would behave as you would have typed a on the keyboard. It's a simulation of the key press rather then that the application think the key was pressed since you can simulate keystrokes couch by other programs, so therefor you can make the program write texts for you :P An example for this is:
[highlight=VB.NET] My.Computer.Keyboard.SendKeys("H")
My.Computer.Keyboard.SendKeys("e")
My.Computer.Keyboard.SendKeys("l")
My.Computer.Keyboard.SendKeys("l")
My.Computer.Keyboard.SendKeys("o")[/highlight]
You can also do it all in one row:
[highlight=VB.NET] My.Computer.Keyboard.SendKeys("Hello")[/highlight]
That was everything for this tutorial, have a fun time coding. :)
Keyboard keys in VB.NET
Started by Vswe, Nov 08 2009 10:09 AM
2 replies to this topic


Sign In
Create Account


Back to top









