Anda di halaman 1dari 3

HANDLING KEYBOARD INPUT AND MOUSE ACTIONS

6
Handling Keyboard Input

Capturing the keyboard stroke or keystroke in Visual Basic is done using the KeyCode or KeyAscii value. A
quick way to find out the KeyCode or KeyAscii value of a certain key is to put a test message box in the
appropriate event. Consider the following example:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)


MsgBox KeyCode
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)


MsgBox KeyAscii
End Sub

Once you know the KeyCode or KeyAscii value, use an If/Then/Else or Select Case statement to execute
code pertaining to the key typed. This sample code moves an object with the arrow keys.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)


Select Case KeyCode
Case 37 ‘Left arrow key
imgMain.Left = imgMain.Left – 40
Case 38 ‘Up arrow key
imgMain.Top = imgMain.Top – 40
Case 39 ‘Right arrow key
imgMain.Left = imgMain.Left + 40
Case 40 ‘Down arrow key
imgMain.Top = imgMain.Top + 40
End Select
End Sub

There are actually a whole pile of built-in constants for KeyCode values which can be used instead.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)


Select Case KeyCode
Case vbKeyLeft
imgMain.Left = imgMain.Left – 40
Case vbKeyUp
imgMain.Top = imgMain.Top – 40
Case vbKeyRight
imgMain.Left = imgMain.Left + 40
Case vbKeyDown
imgMain.Top = imgMain.Top + 40
End Select
End Sub

Information and Communication Technology Department 50


Palompon Institute of Technology
HANDLING KEYBOARD INPUT AND MOUSE ACTIONS
6
This example allows only numbers to be used as an Input to a TextBox.

Private Sub txtInput_KeyPress(KeyAscii As Integer)


Select Case KeyAscii
Case 48 To 57
‘Ok to type these keys
Case Else
KeyAscii = 0
End Select
End Sub

Setting the KeyAscii to 0 (the character code for null) cancels the keystroke.

The Shift argument holds a value which will tell you the state of the Shift, Ctrl, and Alt keys. The value will
be between 0 and 7.

• If Shift has a value of 0 . . . None of these keys are being held down.
• If Shift has a value of 1 . . . Shift key on the keyboard is being held down.
• If Shift has a value of 2 . . . Ctrl key on the keyboard is being held down.
• If Shift has a value of 3 . . . Shift key and Ctrl key on the keyboard are being held down.
• If Shift has a value of 4 . . . Alt key on the keyboard is being held down.
• If Shift has a value of 5 . . . Shift key and Alt key on the keyboard are being held down.
• If Shift has a value of 6 . . . Ctrl key and Alt key on the keyboard are being held down.
• If Shift has a value of 7 . . . All three keys (Shift, Ctrl, and Alt) are being held down.

Here’s an example which tests to see if the user has press Ctrl + X on the keyboard. If it is being press, the
program will end executing:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)


Dim reply

If KeyCode = Asc(“x”) Or KeyCode = Asc(“X”) Then


If Shift = 2 Then
reply = MsgBox (“Are you sure you want to Quit?”, _
vbQuestion + vbYesNo, “End Application”)
If reply = vbYes Then
End
End If
End If
End If
End Sub

SendKeys STATEMENT

The SendKeys statement sends a keystroke to your application as if it were actually typed. Here are few
examples:

SendKeys “{F1}” ‘F1


SendKeys “{BS}” ‘Backspace
SendKeys “{DEL}” ‘Delete
SendKeys “{ENTER}” ‘Enter
SendKeys “{CAPSLOCK}” ‘Caps Lock
Information and Communication Technology Department 51
Palompon Institute of Technology
HANDLING KEYBOARD INPUT AND MOUSE ACTIONS
6
SendKeys “+F2” ‘Shift + F2
SendKeys “^X” ‘Ctrl + C
SendKeys “%F4” ‘Alt + F4

Consider the following example which highlights whatever text in a textbox (named txtInput) when it
receives focus:

Private Sub txtInput_GotFocus()


SendKeys “{HOME} + {END}”
End Sub

Handling Mouse Actions

The main purpose of the MouseDown and MouseUp events is to distinguish between a left click and a right
click.

The built-in Button argument holds a value corresponding to which button was clicked. The Shift argument
holds a value corresponding to the state of the Ctrl, Alt, and Shift keys. The X and Y arguments will tell you
the exact coordinates of where the user clicked. Consider the following example:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _


X As Single, Y As Single)

If Button = vbLeftButton Then


‘Left button was clicked
MsgBox “You clicked the left button at coordinates: “ & X! _
& “, “ & Y!
ElseIf Button = vbRightButton Then
‘Right button was clicked
MsgBox “You clicked the right button at coordinates: “ & X! _
& “, “ & Y!
End If
End Sub

The MouseMove event is triggered constantly as the user moves the mouse. This event also contains
Button and Shift arguments.

You may have seen that aggravating program which wants you to click a button, but moves the button away
once you put your mouse over it. This is very easy to do with the MouseMove event. Add a command
button named cmdClickMe to a Form named frmMain and add this code:

Private Sub cmdClickMe_MouseMove(Button As Integer, Shift As Integer, _


X As Single, Y As Single)
cmdClickMe.Left = Rnd * (frmMain.Width – cmdClickMe.Width)
cmdClickMe.Top = Rnd * (frmMain.Height – cmdClickMe.Height)
End Sub

Rnd is a Visual Basic function which can help you generate random numbers.

Information and Communication Technology Department 52


Palompon Institute of Technology