keyboard and mouse events

10
Keyboard and Mouse Events

Upload: edan

Post on 22-Jan-2016

87 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Keyboard and Mouse Events

Keyboard and Mouse Events

Page 2: 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.

• VB Forms, and most other controls, respond to three different keyboard events:

• KeyPress• KeyDown• KeyUp

Page 3: Keyboard and Mouse Events

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.

Page 4: Keyboard and Mouse Events

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.

Page 5: Keyboard and Mouse Events

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

Page 6: Keyboard and Mouse Events

The Code

Page 7: Keyboard and Mouse Events

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.

Page 8: Keyboard and Mouse Events
Page 9: Keyboard and Mouse Events

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.

Page 10: Keyboard and Mouse Events

Mouse Code