Write Your First Visual Basic Program
Command Button's KeyPress, KeyDown and KeyUp Events
The events that will be mentioned in the
are commonly used, and without learning about them
really can't go anywhere in Visual Basic programming.
To try these examples, start a new project (as
taught in Lesson 1).
Add 1 Command Button to your form. The Command
is called by default Command1.
following code to the code window (you
can copy and paste it using Ctrl + C
and Ctrl + V for
Private Sub Command1_KeyDown(KeyCode As Integer, Shift As
Private Sub Command1_KeyPress(KeyAscii As Integer)
Private Sub Command1_KeyUp(KeyCode As Integer, Shift As
When the Command Button's KeyDown event will be
"KeyDown" will be printed on the form,
the Command Button's KeyPress event will be executed,
"KeyPress" will be
printed on the form, and when
the Command Button's KeyUp event will be
"KeyUp" will be printed on the form.
Run the program,
and click the button with the mouse.
Nothing has happened.
the KeyDown, Key_Press, and KeyUp events are
being executed Only when you
press a key on the keyboard.
Now press any key
on the keyboard, hold it down for few seconds,
and then release it.
form will look like this:
The first event that been executed is the
because "KeyDown" was the first text that been printed on
The second event was KeyPress, and then again
After every KeyDown event that been executed, a KeyPress
had been executed.
We learnt that when a key is being holded down,
KeyDown and the KeyPress events are being executed in
this order over and
over again, until the
key is up
When you release the key, the KeyUp event is being executed