1 / 10

Keyboard and Mouse Events

Keyboard and Mouse Events. Handling Keyboard Events. Aside from keyboard shortcuts, there are other ways to handle keyboard activity in your programs. This can be especially useful in games or other programs which use the keyboard for something besides typing text.

edan
Download Presentation

Keyboard and Mouse Events

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Keyboard and Mouse Events

  2. Handling Keyboard Events • Aside from keyboard shortcuts, there are other ways to handle keyboard activity in your programs. • This can be especially useful in games or other programs which use the keyboard for something besides typing text. • VB Forms, and most other controls, respond to three different keyboard events: • KeyPress • KeyDown • KeyUp

  3. KeyPress • KeyPress happens when the user presses a key or key combination (like Shift+A). • The KeyPress event’s “e” parameter returns a “processed” value from the keyboard. • For instance, if the user types an “A” with the shift key down, e.KeyChar will return “A”, indicating that the user types a capital “A”. If an “A” is typed without the shift key down, e.KeyChar will return “a”. • Similarly the “4” key will have e.KeyChar equal to “4”, but it will be “$” if the shift key is down.

  4. KeyDown, KeyUp • In contrast, KeyDown and KeyUp return more “raw” information—they tell you exactly what key combinations have been typed. • As the name implies, KeyDown occurs when a key is pushed down, and KeyUp occurs when a key is released. This allows you to respond to each event separately, something you can’t do with KeyPress. • If you had a driving game, you could say “Hold the ‘B’ key to brake, and release it to stop braking.” • The “e” parameter for KeyDown and KeyUp contains more information: • e.Shift, e.Alt, e.Control are Booleans which indicate which of these keys was pressed when the other key was pushed down or released. • e.KeyCode is an enumeration with a value matching each key on the keyboard, including the function keys, the direction keys, and the numeric keypad keys.

  5. The “Using the Keyboard and Mouse” program demonstrates the use of these events.

  6. The Code

  7. KeyPreview • An important point: All of these events being handled are Form events. This is the simplest way to handle keyboard input (instead of responding to events from individual controls). • To make this work, you need to set the form’s KeyPreview property to True. • This allows the form’s events to respond before any other control events (such as typing in a TextBox). • Play with the “Using the Keyboard and Mouse” program to get a feel for how to use the KeyPress, KeyDown, and KeyUp events.

  8. Mouse Events • The previous slide explains how to handle various mouse events in your program. • That form is a part of the “Using the Keyboard and Mouse” program; simply double-click on the “Using the Keyboard” form to bring up the Mouse Events form. • The next slide shows the code for the Mouse Events form. • Again, play with the Mouse Events form to get a feel for how these events work.

  9. Mouse Code

More Related