Start new project.
Add a button to the form. Click on the button, go to the properties window, click on the events icon, find the keydown event and double click on it.
If you write inside the sub: e. -> you will get a menu displaying all the possible properties for "e" under the "Common" tab.
Insert this inside the sub:
MsgBox("KeyValue: " & vbNewLine & e.KeyValue & vbNewLine & e.KeyValue.ToString)
- start the program, click on button1 if it's not highlighted then press a key on your keyboard.
"KeyValue" will give you the decimal value of the character according to the ASCII code. You can check the ASCII table here: Link.
Turning KeyValue to a string doesn't change anything, you always get the ASCII decimal value of a character.
The main event for this sub is KeyDown, so if you keep holding down a key on your keyboard, the sub will be constantly executed.
Let's change the previous line of code into this:
MsgBox("KeyValue: " & vbNewLine & e.KeyValue & vbNewLine & e.KeyValue.ToString & vbNewLine & vbNewLine &
"KeyCode: " & vbNewLine & e.KeyCode & vbNewLine & e.KeyCode.ToString)
- start program, press keys on the keyboard.
"KeyCode" will also represent the ASCII value of a character until it's turned into a string. The string value of "KeyCode" is the uppercase version of the character itself. You can also try pressing the modifier keys like control, alt and shift. Pressing "Alt" will give you "Menu".
If you hold down 2 or more keys at the same time, the "KeyCode" property will hold the value of the last one you have pressed.
Let's change again the 2 lines of code that we have inside the button1 sub:
MsgBox("KeyValue: " & vbNewLine & e.KeyValue & vbNewLine & e.KeyValue.ToString & vbNewLine & vbNewLine &
"KeyCode: " & vbNewLine & e.KeyCode & vbNewLine & e.KeyCode.ToString & vbNewLine & vbNewLine &
"KeyData: " & vbNewLine & e.KeyData & vbNewLine & e.KeyData.ToString)
- try the program out.
"KeyData" can add together the values of the three modifier keys (alt, control and shift) and another character from the keyboard when they are held down.
"Shift" value should be 65552, "control" should be 131089 and "alt" should be 262162.
Although it adds together more than one modifier, it doesn't add more than one character, which means that "KeyData" will always give you a unique integer value.
If "KeyData" is turned to a string, it will display you the names of all the modifiers that are pressed down at the same time plus the last key that was pressed.
Now change the event of button1 sub from keydown:
Handles Button1.KeyDown
to keyup:
Handles Button1.KeyUp
- now the sub will be run only when you release a pressed key on the keyboard. "KeyUp" should have the same properties for the "e" variable since the same event handler is declared ("KeyEventArgs").
Private Sub Button1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyUp
"KeyCode: " & vbNewLine & e.KeyCode & vbNewLine & e.KeyCode.ToString & vbNewLine & vbNewLine &
"KeyData: " & vbNewLine & e.KeyData & vbNewLine & e.KeyData.ToString)
End Sub
End Class
Add another button to the form, go to properties, events icon, double click the keydown button again. Add this in the sub:
MsgBox(e.Shift & vbNewLine & e.Control & vbNewLine & e.Alt)
- click on the second button first if the first one is highlighted then press a key
Specific modifier properties will show either true or false when the specific key is pressed on the keyboard. Turning the values to string doesn't change anything.
Turn the previous code line into this:
- the modifier property holds a value only for the modifier keys otherwise is zero (None). Just like "KeyData" it can list out more modifiers pressed together.
Change the event of button2 sub to keyup:
Handles Button2.KeyUp
- although a modifier key can start a sub with a KeyUp event, the event handler properties used inside this sub will not register that those modifiers were pressed. You should get "false" when displaying the values of the modifier properties.
Add a textbox to the form and give it a KeyDown event. Inside the sub add:
e.SuppressKeyPress = True
- "SuppresKeyPress" property will disallow any user input to be placed into the textbox. By default it's always set to false.
Change the previous code line to this:
- this will disallow the insertion of the "c" character.
When you write "Keys." inside the sub, a menu will appear showing you all the possible keys that you could block this way.
This is how you could block all the numbers from being typed into the textbox:
Placing any operation like a messagebox or a dialog, which stop the sub execution for a moment, is going to conflict with the usage of the "SuppressKeyPress" property and therefore it will not do it's job.
Code:
Private Sub Button1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown
MsgBox("KeyValue: " & vbNewLine & e.KeyValue & vbNewLine & e.KeyValue.ToString & vbNewLine & vbNewLine &
"KeyCode: " & vbNewLine & e.KeyCode & vbNewLine & e.KeyCode.ToString & vbNewLine & vbNewLine &
"KeyData: " & vbNewLine & e.KeyData & vbNewLine & e.KeyData.ToString)
End Sub
Private Sub Button2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button2.KeyUp
MsgBox(e.Shift.ToString & vbNewLine & e.Control & vbNewLine & e.Alt & vbNewLine & vbNewLine &
"Modifier: " & vbNewLine & e.Modifiers & vbNewLine & e.Modifiers.ToString)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyValue >= Keys.D0 And e.KeyValue <= Keys.D9 Then
e.SuppressKeyPress = True
End If
End Sub
End Class
Add another textbox to the form, go to properties, give it the KeyPress event. Insert into the sub:
MsgBox(e.KeyChar)
The event handler for KeyDown and KeyUp was KeyEventArgs, while the one for KeyPress is KeyPressEventArgs.
The "KeyPress" event will not start the sub by only pressing modifiers and keys from F1 to F12, it will start mainly by pressing printable ASCII characters and also keys like escape and return. However, the event handler does take into consideration if a modifier was held down before a triggering keyboard key was pressed.
"KeyChar" holds the symbolic value of the characters sent by the "KeyPress" event, not the numeric ASCII value.
Erase the previous code line (textbox2 sub), insert this:
- the message box will start when the pressed keyboard key is the character "d".
Keys.D.Tostring - gives us uppercase characters by default, so we have to add ToLower for lowercase.
Change the previous code with this one:
Chr(100) - same as using: Keys.D.ToString.ToLower. The number inside the brackets is the decimal value of lowercase "d" character present in the ASCII table.
e.Handled = True - e.handled can be used just like e.SuppressKeyPress, it will remove the action that the keystroke is supposed to perform inside the control (for example, being printed inside the textbox).
You can even add a message box to this "if" statement, the code should still work.
The "Handled" property might not work well, on the other hand, when using the "KeyDown" event.
Final code:
Private Sub Button1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown
MsgBox("KeyValue: " & vbNewLine & e.KeyValue & vbNewLine & e.KeyValue.ToString & vbNewLine & vbNewLine &
"KeyCode: " & vbNewLine & e.KeyCode & vbNewLine & e.KeyCode.ToString & vbNewLine & vbNewLine &
"KeyData: " & vbNewLine & e.KeyData & vbNewLine & e.KeyData.ToString)
End Sub
Private Sub Button2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button2.KeyUp
MsgBox(e.Shift.ToString & vbNewLine & e.Control & vbNewLine & e.Alt & vbNewLine & vbNewLine &
"Modifier: " & vbNewLine & e.Modifiers & vbNewLine & e.Modifiers.ToString)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyValue >= Keys.D0 And e.KeyValue <= Keys.D9 Then
e.SuppressKeyPress = True
End If
End Sub
Private Sub TextBox2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If e.KeyChar = Chr(100) Then
e.Handled = True
End If
End Sub
End Class