CHAPTER 12 MOUSE AND KEYBOARD 1 Chapter 12 Mouse and Keyboard (Main Page) 12.1 MousePointer constants. 12.2 Demonstrating the MousePointer property. 12.3 Demonstrating mouse events. 12.4 Mouse button constants. 12.5 Determining which mouse button was pressed. 12.6 Constants associated with variable Shift. 12.7 Determining which combination of Shift, Ctrl or Alt was pressed. 12.8 Button and Shift bit fields. 12.9 Constants used with method Drag. 12.10 Demonstrating manual drag-and-drop. 12.11 Demonstrating automatic drag-and-drop. 12.12 Common key event constants. 12.13 Demonstrating key events. 12.14 Demonstrating property KeyPreview.
CHAPTER 12 MOUSE AND KEYBOARD 2 Constant Value Cursor Description vbdefault 0 Default cursor. Describes the default cursor for a form or control. vbarrow 1 Arrow cursor. Typically used to indicate that selections can be made. vbcrosshair 2 Crosshair cursor. Typically used to indicate precision. vbibeam 3 I-beam cursor. Typically used to indicate that text can be input. vbsizepointer 5 Sizing cursor. Typically used to indicate that resizing is allowed in all directions. vbsizenesw 6 North-East-South-West cursor. Typically used to indicate that resizing is allowed in this direction. vbsizens 7 North-South cursor. Typically used to indicate that resizing is allowed in this direction. vbsizenwse 8 North-West-South-East cursor. Typically used to indicate that resizing is allowed in this direction. vbsizewe 9 West-East cursor. Typically used to indicate that resizing is allowed in this direction. vbuparrow 10 Up arrow. vbhourglass 11 Hourglass cursor. Typically used to indicate that the program is busy performing some task. vbnodrop 12 No-drop cursor. Typically used to indicate that a drop operation is not permitted. vbarrowhourglass 13 Arrow hourglass cursor. Typically used to indicate that the program is busy performing some task and that the user can still make selections with the mouse pointer. vbarrowquestion 14 Arrow question mark cursor. Typically used to indicate help is available for a feature. vbsizeall 15 Size all directions cursor. vbcustom 99 Custom cursor. Typically used to display a non-visual Basic cursor). Fig. 12.1 MousePointer constants.
CHAPTER 12 MOUSE AND KEYBOARD 3 1 ' Fig. 12.2 2 ' Changing the mouse pointer 3 Option Explicit ' General declaration 4 5 Private Sub optcursor_click(index As Integer) 6 MousePointer = Index 7 End Sub Initial GUI at execution. GUI after user selects arrow question. Fig. 12.2 Demonstrating the MousePointer property. 1 ' Fig. 12.3 2 ' Demonstrating mouse events 3 Option Explicit ' General declaration 4 5 Private Sub Form_Load() 6 Call Randomize ' Randomize 7 End Sub 8 9 Private Sub cmdclear_click() 10 Call Cls ' Clear Form 11 End Sub 12 13 Private Sub Form_Click() 14 15 ' Randomly set Form ForeColor 16 Select Case (1 + Int(Rnd() * 4)) 17 Case 1 18 ForeColor = vbblack
CHAPTER 12 MOUSE AND KEYBOARD 4 19 Case 2 20 ForeColor = vbmagenta 21 Case 3 22 ForeColor = vbred 23 Case 4 24 ForeColor = vbblue 25 End Select 26 27 End Sub 28 29 Private Sub Form_DblClick() 30 31 ' Randomly set Form BackColor 32 Select Case (1 + Int(Rnd() * 4)) 33 Case 1 34 BackColor = vbwhite 35 Case 2 36 BackColor = vbyellow Fig. 12.3 Demonstrating mouse events (part 1 of 3). 37 Case 3 38 BackColor = vbgreen 39 Case 4 40 BackColor = vbcyan 41 End Select 42 43 ' Change chkmove BackColor to Form's BackColor 44 chkmove.backcolor = BackColor 45 End Sub 46 47 Private Sub Form_MouseDown(Button As Integer, _ 48 Shift As Integer, X As Single, _ 49 Y As Single) 50 51 CurrentX = X ' Set x coordinate 52 CurrentY = Y ' Set y coordinate 53 Print "MouseDown" 54 End Sub 55 56 Private Sub Form_MouseUp(Button As Integer, _ 57 Shift As Integer, X As Single, _ 58 Y As Single) 59 60 ' Reverse coordinates 61 CurrentX = Y 62 CurrentY = X 63 Print "MouseUp" 64 End Sub 65 66 Private Sub Form_MouseMove(Button As Integer, _ 67 Shift As Integer, X As Single, _ 68 Y As Single) 69 70 ' If checked enable printing operations 71 If chkmove.value = 1 Then 72 CurrentX = X 73 CurrentY = Y 74 Print "MouseMove" 75 End If 76
CHAPTER 12 MOUSE AND KEYBOARD 5 77 End Sub Fig. 12.3 Demonstrating mouse events (part 2 of 3). Initial GUI at execution. GUI after user has clicked mouse in several different locations. GUI after user has pressed Clear and then clicked Enable MouseMove and moved the mouse. Fig. 12.3 Demonstrating mouse events (part 3 of 3).
CHAPTER 12 MOUSE AND KEYBOARD 6. Constant Value Description vbrightbutton 1 The right mouse button. vbleftbutton 2 The left mouse button. vbmiddlebutton 4 The center mouse button. Fig. 12.4 Mouse button constants. 1 ' Fig. 12.5 2 ' Mouse buttons 3 Option Explicit ' General declaration 4 5 Private Sub Form_Load() 6 imgimage.picture = LoadPicture("d:\images\ch12\mouse0.gif") 7 End Sub 8 9 Private Sub Form_MouseDown(Button As Integer, _ 10 Shift As Integer, X As Single, _ 11 Y As Single) 12 Call SetPressedImage(Button) 13 End Sub Fig. 12.5 Determining which mouse button was pressed (part 1 of 3). 14 15 Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _ 16 X As Single, Y As Single) 17 18 Call SetReleasedImage 19 End Sub 20 21 Private Sub imgimage_mousedown(button As Integer, _ 22 Shift As Integer, _ 23 X As Single, Y As Single) 24 Call SetPressedImage(Button) 25 End Sub 26 27 Private Sub imgimage_mouseup(button As Integer, _ 28 Shift As Integer, _ 29 X As Single, Y As Single) 30 Call SetReleasedImage 31 End Sub 32 33 Private Sub SetPressedImage(b As Integer) 34 imgimage.picture = LoadPicture("d:\images\ch12\mouse" & _ 35 b & ".gif") 36 End Sub 37 38 Private Sub SetReleasedImage() 39 imgimage.picture = LoadPicture("d:\images\ch12\mouse0.gif") 40 End Sub
CHAPTER 12 MOUSE AND KEYBOARD 7 Initial GUI at execution. GUI after user presses the left mouse button. Fig. 12.5 Determining which mouse button was pressed (part 2 of 3). GUI after the user presses the right mouse button. Fig. 12.5 Determining which mouse button was pressed (part 3 of 3). Constant Value Description vbshiftmask 1 Status of the Shift key. vbctrlmask 2 Status of the Ctrl key. vbaltmask 4 Status of the Alt key. Fig. 12.6 Constants associated with variable Shift.
CHAPTER 12 MOUSE AND KEYBOARD 8 1 ' Fig. 12.7 2 ' Mouse buttons 3 Option Explicit ' General declaration 4 5 Private Sub Form_Load() 6 imgimage.picture = LoadPicture("d:\images\ch12\mouse0.gif") 7 End Sub 8 9 Private Sub Form_MouseDown(Button As Integer, _ 10 Shift As Integer, x As Single, _ 11 Y As Single) 12 13 Dim a As Integer, c As Integer, s As Integer 14 15 a = Shift And vbaltmask ' Mask Alt bit 16 c = Shift And vbctrlmask ' Mask Ctrl bit 17 s = Shift And vbshiftmask ' Mask Shift bit 18 19 If (a <> 0) Then ' Alt 20 lblkeys(a).backcolor = vbgreen 21 End If 22 23 If (s <> 0) Then ' Shift 24 lblkeys(s).backcolor = vbgreen 25 End If 26 27 If (c <> 0) Then ' Ctrl 28 lblkeys(c).backcolor = vbgreen 29 End If 30 31 imgimage.picture = LoadPicture("d:\images\ch12\mouse" & _ 32 Button & ".gif") 33 End Sub 34 35 Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _ 36 x As Single, Y As Single) 37 38 ' Change Label BackColors to Form's BackColor 39 lblkeys(1).backcolor = BackColor 40 lblkeys(2).backcolor = BackColor 41 lblkeys(4).backcolor = BackColor 42 43 ' Load mouse image of unpressed buttons 44 imgimage.picture = LoadPicture("d:\images\ch12\mouse0.gif") 45 End Sub Fig. 12.7 Determining which combination of Shift, Ctrl or Alt was pressed (part 1 of 2).
CHAPTER 12 MOUSE AND KEYBOARD 9 Initial GUI at execution. GUI after user presses the left mouse button while Shift is held down. GUI after user presses the right mouse button while Ctrl is held down. Fig. 12.7 Determining which combination of Shift, Ctrl or Alt was pressed (part 2 of 2).
CHAPTER 12 MOUSE AND KEYBOARD 10 Button bit field... M R L Most significant bits Least significant bits Shift bit field... A C S Most significant bits Least significant bits Key M R L A C S Bit representing middle mouse button Bit representing right mouse button Bit representing left mouse button Bit representing Alt key Bit representing Ctrl key Bit representing Shift key Fig. 12.8 Button and Shift bit fields. Constant Value Description vbcanceldrag 0 Cancels drag-and-drop operation. Event procedure DragDrop is not called. vbbegindrag 1 Begins drag-and-drop operation. Event procedure DragDrop is called. vbenddrag 2 Terminates drag-and-drop operation. Event procedure DragDrop is called. Fig. 12.9 Constants used with method Drag. 1 ' Fig. 12.10 2 ' Demonstrating manual drag-and-drop 3 Option Explicit ' General declaration 4 Dim mcurrentcell As Integer ' General declaration 5 6 Private Sub Form_Load() 7 Dim x As Integer 8 9 mcurrentcell = 2 ' Lower left corner 10 11 For x = 1 To 64 12 13 If x Mod 2 Then 14 picsquare(x).picture = LoadPicture("d:\images\ch12\" & _ 15 "w_marble.jpg") 16 Else 17 picsquare(x).picture = LoadPicture("d:\images\ch12\" & _ 18 "b_marble.jpg") 19 End If 20 21 Next x 22 23 picsquare(2).picture = LoadPicture("d:\images\ch12\" & _
CHAPTER 12 MOUSE AND KEYBOARD 11 24 "b_knight.jpg") 25 End Sub 26 27 Private Sub picsquare_mousedown(index As Integer, _ 28 Button As Integer, _ 29 Shift As Integer, _ 30 x As Single, Y As Single) 31 32 ' If on the PictureBox displaying the image 33 ' then enable dragging. 34 If Index = mcurrentcell Then 35 picsquare(mcurrentcell).drag vbbegindrag 36 End If 37 38 End Sub Fig. 12.10 Demonstrating manual drag-and-drop (part 1 of 4). 39 40 Private Sub picsquare_dragover(index As Integer, _ 41 Source As Control, _ 42 x As Single, Y As Single, _ 43 State As Integer) 44 45 ' Display icon while dragging over a PictureBox 46 picsquare(index).dragicon = LoadPicture("d:\images" & _ 47 "\ch12\knight.cur") 48 End Sub 49 50 Private Sub picsquare_dragdrop(index As Integer, _ 51 Source As Control, _ 52 x As Single, Y As Single) 53 54 ' Draw image at new position 55 If Index Mod 2 Then 56 picsquare(index).picture = LoadPicture("d:\images\ch" & _ 57 "12\w_knight.jpg") 58 Else 59 picsquare(index).picture = LoadPicture("d:\images\ch" & _ 60 "12\b_knight.jpg") 61 End If 62 63 ' Remove last image only if the drop is at 64 ' a different location. 65 If mcurrentcell <> Index Then 66 If Source.Index Mod 2 Then 67 Source.Picture = LoadPicture("d:\images\ch12" & _ 68 "\w_marble.jpg") 69 Else 70 Source.Picture = LoadPicture("d:\images\ch12" & _ 71 "\b_marble.jpg") 72 End If 73 74 End If 75 76 ' Update current image position 77 mcurrentcell = Index 78 End Sub Fig. 12.10 Demonstrating manual drag-and-drop (part 2 of 4).
CHAPTER 12 MOUSE AND KEYBOARD 12 Initial GUI at execution. GUI when the user is performing a drag operation. Note the change in the mouse pointer. Since a drop has not occurred, the original image is still visible. Fig. 12.10 Demonstrating manual drag-and-drop (part 3 of 4). GUI after user drops the knight. Fig. 12.10 Demonstrating manual drag-and-drop (part 4 of 4).
CHAPTER 12 MOUSE AND KEYBOARD 13 1 ' Fig. 12.11 2 ' Demonstrating Automatic drag-and-drop 3 Option Explicit ' General declaration 4 5 Private Sub Form_Load() 6 Dim a As Integer 7 8 ' Set all DragMode properties to Automatic 9 For a = cmdbutton.lbound To cmdbutton.ubound 10 cmdbutton(a).dragmode = 1 ' Automatic 11 Next a 12 13 End Sub 14 15 Private Sub Form_DragDrop(Source As Control, X As Single, _ 16 Y As Single) 17 18 Dim w As Integer, h As Integer 19 20 ' Center control on mouse pointer 21 w = X - Source.Width / 2 22 h = Y - Source.Height / 2 23 24 ' Move button to location where drop occurs 25 Call Source.Move(w, h) 26 End Sub Initial GUI at execution. Fig. 12.11 Demonstrating automatic drag-and-drop (part 1 of 2).
CHAPTER 12 MOUSE AND KEYBOARD 14 GUI after user has dragged and dropped three buttons. A dragand-drop operation is occurring on a button. GUI after user has dragged and dropped all buttons. Fig. 12.11 Demonstrating automatic drag-and-drop (part 2 of 2). Constant ASCII value(s) Description vbkeya - vbkeyz 65-90 A key through Z key. vbkeynumpad0 - vbkeynumpad9 96-105 Keypad numeric keys 0 through 9. vbkey0 - vbkey9 48-57 Numeric keys 0 through 9. vbkeyf1 - vbkeyf16 112-127 Function keys F1 through F16. vbkeydecimal 110 Decimal point key (Period key). vbkeyback 8 Backspace key. vbkeytab 9 Tab key. vbkeyreturn 13 Return key (or Enter key). vbkeyshift 16 Shift key. vbkeycontrol 17 Ctrl key. vbkeycapital 20 Caps Lock key. vbkeyescape 27 Escape key. vbkeyspace 32 Space bar. vbkeyinsert 45 Insert key. Fig. 12.12 Common key event constants.
CHAPTER 12 MOUSE AND KEYBOARD 15 Constant ASCII value(s) Description vbkeydelete 46 Delete key. Fig. 12.12 Common key event constants. 1 ' Fig. 12.13 2 ' Demonstrating KeyDown, KeyUp, and KeyPress 3 Option Explicit ' General declaration 4 Dim mtitlestring As String ' General declaration 5 6 Private Sub Form_Load() 7 ' Store Caption value for use in KeyPress 8 mtitlestring = Caption & Space$(5) 9 End Sub 10 11 Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) 12 13 ' Determine which, if any, of the Shift, Ctrl, 14 ' or Alt keys is pressed 15 Select Case Shift 16 Case vbshiftmask ' Shift 17 ForeColor = vbyellow 18 Case vbaltmask ' Alt 19 ForeColor = vbred 20 Case vbctrlmask ' Ctrl 21 ForeColor = vbgreen 22 Case vbshiftmask + vbaltmask ' Shift + Alt 23 ForeColor = vbblue Fig. 12.13 Demonstrating key events (part 1 of 3). 24 Case vbshiftmask + vbctrlmask ' Shift + Ctrl 25 ForeColor = vbmagenta 26 Case vbaltmask + vbctrlmask ' Alt + Ctrl 27 ForeColor = vbcyan 28 Case vbaltmask + vbctrlmask + vbshiftmask ' All three 29 Call Cls 30 End Select 31 32 ' Test for letter key 33 If KeyCode >= vbkeya And KeyCode <= vbkeyz Then 34 Print Chr$(KeyCode); ' Print the character 35 ElseIf KeyCode = vbkeyreturn Then ' Return key 36 Print ' Print on next line 37 End If 38 39 End Sub 40 41 Private Sub Form_KeyPress(KeyAscii As Integer) 42 ' Update title bar to display the key pressed 43 Caption = mtitlestring & "(" & Chr$(KeyAscii) & ")" 44 End Sub 45 46 Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) 47 ' When key is released, change ForeColor to black 48 ForeColor = vbblack 49 End Sub
CHAPTER 12 MOUSE AND KEYBOARD 16 Initial GUI at execution. Fig. 12.13 Demonstrating key events (part 2 of 3). GUI after user has typed text. GUI after user has held Shift, Ctrl and Alt simultaneously to clear the form. Fig. 12.13 Demonstrating key events (part 2 of 2).
CHAPTER 12 MOUSE AND KEYBOARD 17 1 ' Fig. 12.14 2 ' Demonstrating the KeyPreview property. 3 Option Explicit ' General declaration 4 5 Private Sub Form_Load() 6 Call Randomize 7 8 ' Allow Form to get key events first 9 KeyPreview = True 10 End Sub 11 12 Private Sub Form_KeyPress(KeyAscii As Integer) 13 14 ' Only allow numeric keys 15 If KeyAscii >= vbkey0 And KeyAscii <= vbkey9 Then 16 txtinput.text = txtinput.text & Chr$(KeyAscii) 17 End If 18 19 End Sub 20 21 Private Sub txtinput_keypress(keyascii As Integer) 22 KeyAscii = 0 ' Disable event handling 23 End Sub 24 25 Private Sub txtinput_keyup(keycode As Integer, Shift As Integer) 26 27 Select Case Int(Rnd() * 3) 28 Case 0 29 txtinput.backcolor = vbyellow 30 Case 1 31 txtinput.backcolor = vbcyan 32 Case 2 33 txtinput.backcolor = vbred 34 End Select 35 36 End Sub Initial GUI at execution. GUI after user enters a series of numbers. Fig. 12.14 Demonstrating property KeyPreview.