Share via

Visual Basic Concepts

Responding to Mouse and Keyboard Events

Your Visual Basic applications can respond to a variety of mouse events and keyboard events. For example, forms, picture boxes, and image controls can detect the position of the mouse pointer, can determine whether a left or right mouse button is being pressed, and can respond to different combinations of mouse buttons and SHIFT, CTRL, or ALT keys. Using the key events, you can program controls and forms to respond to various key actions or interpret and process ASCII characters.

In addition, Visual Basic applications can support both event-driven drag-and-drop and OLE drag-and-drop features. You can use the Drag method with certain properties and events to enable operations such as dragging and dropping controls. OLE drag and drop gives your applications all the power you need to exchange data throughout the Windows environment — and much of this technology is available to your application without writing code.

You can also use the mouse or keyboard to manage the processing of long background tasks, which allows your users to switch to other applications or interrupt background processing.

Other actions and events that involve the mouse or keyboard (the Click and DblClick events, the Focus events, and the Scroll event) are not covered in this chapter. For more information on the Click and DblClick events, see the topics "Clicking Buttons to Perform Actions" and "Understanding Focus" in "Forms, Controls, and Menus," and see "Click Event" and “DblClick Event" in the Language Reference. Also see "Scroll Event."


  • Responding to Mouse Events

  • Detecting Mouse Buttons

  • Detecting SHIFT, CTRL, and ALT States

  • Dragging and Dropping

  • OLE Drag and Drop

  • Customizing the Mouse Pointer

  • Responding to Keyboard Events

  • Interrupting Background Processing


  Responding to Mouse Events

How to use the MouseDown, MouseUp, and MouseMove events to control or enhance mouse action.

Detecting Mouse Buttons

How to detect various mouse button states using the button argument.

Detecting SHIFT, CTRL, and ALT States

How to detect various mouse button and SHIFT, CTRL, and ALT key states using the shift argument.

Dragging and Dropping

Enabling event-driven drag-and-drop within your Visual Basic application.

OLE Drag and Drop

How to use this powerful Windows technology to move or copy data from control to control within Visual Basic or from Visual Basic to other Windows applications, and vice versa.

Customizing the Mouse Pointer

Changing the mouse pointer to inform the user about the state and functionality of your application.

Responding to Keyboard Events

How to use the KeyDown, KeyUp, and KeyPress events to respond to various key actions or interpret and process ASCII characters.

Interrupting Background Processing

Managing intermittent and long-running background tasks.