Você está na página 1de 7

Depending on the type of application you are creating, you might need to notice what the user is doing

with the mouse. You need to know when and where the mouse was clicked, which button was clicked, and when the button was released. You also need to know what the user did while the mouse button was being held down. Another thing that you might need to do is read the keyboard events. As with the mouse, you might need to know when a key was pressed, how long it was held down, and when it was released. Today you are going to learn
y y y y

What mouse events are available for use and how to determine which one is appropriate for your application's needs. How you can listen to mouse events and how to react to them in your Visual C++ application. What keyboard events are available for use and what actions will trigger each of these events. How to capture keyboard events and take action based on what the user pressed.

Understanding Mouse Events As you learned yesterday, when you are working with most controls, you are limited to a select number of events that are available in the Class Wizard. When it comes to mouse events, you are limited for the most part to click and double-click events. Just looking at your mouse tells you that there must be more to capturing mouse events than recognizing these two. What about the right mouse button? How can you tell if it has been pressed? And what about drawing programs? How can they follow where you drag the mouse? If you open the Class Wizard in one of your projects, select the dialog in the list of object IDs, and then scroll through the list of messages that are available, you will find a number of mouse-related events, which are also listed in Table 3.1. These event messages enable you to perform any task that might be required by your application.

TABLE 3.1. MOUSE EVENT MESSAGES. Message WM_LBUTTONDOWN WM_LBUTTONUP WM_LBUTTONDBLCLK WM_RBUTTONDOWN WM_RBUTTONUP WM_RBUTTONDBLCLK WM_MOUSEMOVE WM_MOUSEWHEEL Description The left mouse button has been pressed. The left mouse button has been released. The left mouse button has been double-clicked. The right mouse button has been pressed. The right mouse button has been released. The right mouse button has been double-clicked. The mouse is being moved across the application window space. The mouse wheel is being moved.

Capturing Keyboard Events Reading keyboard events is similar to reading mouse events. As with the mouse, there are event messages for when a key is pressed and when it is released. These events are listed in Table 3.2. TABLE 3.2. KEYBOARD EVENT MESSAGES. Message Description WM_KEYDOWN A key has been pressed down. WM_KEYUP A key has been released. The keyboard obviously has fewer messages than the mouse does. Then again, there are only so many things that you can do with the keyboard. These event messages are available on the dialog window object and are triggered only if there are no enabled controls on the window. Any enabled controls on the window have input focus, so all keyboard events go to them. That's why you remove all controls from the main dialog for your drawing application.

Keyboard Messages Introduction A keyboard is a hardware object attached to the computer. By default, it is used to enter recognizable symbols, letters, and other characters on a control. Each key on the keyboard displays a symbol, a letter, or a combination of those, to give an indication of what the key could be used for. The user typically presses a key, which sends a signal to a program. The signal is analyzed to find its meaning. If the program or control that has focus is equipped to deal with the signal, it may produce the expected result. If the program or control cannot figure out what to do, it ignores the action. Each key has a code that the operating system can recognize. The Key Down Message When a user has pressed a key or a combination while using a control, the KeyDown event can be used to identify the key or the combination. A key is typically one of those you see on the keyboard. The keyboard also has special keys referred to as modifiers. These are Alt, Shift, and Ctrl. Only the Alt key can be used by itself to change something on the screen. For example, it is used to activate a menu. Otherwise, users press a modifier and an additional key to make something happen. The KeyDown event allows you to identify what key the user pressed. The KeyDown event is of KeyEventArgs type interpreted through the KeyEventHandler delegate. The constructor of the KeyEventArgs class has the following syntax: public: KeyEventArgs(Keys keyData); The keyData argument represents the key or the combination of keys that was pressed. This class provides the following properties through the second argument of the KeyDown event:
y

Handled: This Boolean property indicates whether the event was carried out. If its value is true, the event was carried. If its value is false, the event was not carried

KeyCode: This property specifies the key that was pressed. The recognized keys are members of theKeys enumerator Alt: This Boolean property indicates whether an Alt key was pressed. If the Alt key was pressed, this property has a true value. Otherwise, its value is false Control: This Boolean property indicates whether a Ctrl key was pressed. If a Ctrl key was pressed, this property has a true value. Otherwise, its value is false Shift: This Boolean property indicates whether a Shift key was pressed. If a Shift key was pressed, this property has a true value. Otherwise, its value is false Modifiers: This property allows you to identify the key modifier that was pressed. KeyData: This property represents the key combined with a key modifier that were pressed KeyValue: This property represents the resulting integral value of the key of the KeyData property that was pressed

The Key Up Message After a user has pressed a key, he or she can release it. This causes the control to fire a KeyUp event, which is the opposite of the KeyDown event. Like KeyDown, the KeyUp event is of typeKeyEventHandler. It is handled by a KeyEventArgs class with the same characteristics we reviewed for the KeyDown event. The Key Press Message When the user presses a key, the control that has focus fires the KeyPress event. Unlike the other two keyboard messages, the key pressed for this event should (must) be a character key. The KeyPress event is handled by the KeyPressEventHandler delegate. The event is of type KeyPressEventArgs. The constructor of the KeyPressEventArgs class is: public: KeyPressEventArgs(__wchar_t keyChar); The KeyPressEventArgs class has the following properties:
y

Handled: This property identifies whether this event was handled

KeyChar: The property identifies the key that was pressed. It must be a letter or a recognizable symbol. Lowercase alphabetic characters, digits, and the lower base characters such as ; , [ ] - = / are recognized as they are. For an uppercase letter or an upper base symbol, the user must press Shift + the key. The character would be identified as one entity. This means that the symbol % typed with Shift + 5 is considered as one character.

Mouse Events Introduction The mouse is another object that is attached to the computer and allows the user to interact with the machine. The mouse and the keyboard can each accomplish some tasks that are not normally available on the other or both can accomplish some tasks the same way. The mouse is equipped with two, three, or more buttons. When a mouse has two buttons, one is usually located on the left and the other is located on the right. When a mouse has three buttons, one usually is in the middle of the other two. A mouse can also have a round object referred to as a wheel. The mouse is used to select a point or position on the screen. Once the user has located an item, which could also be an empty space, a letter or a word, he or she would position the mouse pointer on it. To actually use the mouse, the user would press either the left, the middle (if any), or the right button. If the user presses the left button once, this action is called Click. If the user presses the right mouse button, the action is referred to as Right-Click. If the user presses the left button twice and very fast, the action is calledDouble-Click. If the mouse is equipped with a wheel, the user can position the mouse pointer somewhere on the screen and roll the wheel. This usually causes the document or page to scroll up or down, slow or fast, depending on how it was configured. Mouse Enter Before using a control with the mouse, the user must first position the mouse on it. When this happens, the control fires a MouseEnter event. This event is carried by an EventArgs argument. This means that the argument doesn't provide much information, only to let you know that the mouse was positioned on a control.

Mouse Move Whenever the mouse is being moved on top of a control, a MouseMove is fired. This event is of typeMouseEventArgs and handled by the MouseEventHandler delegate. The constructor of theMouseEventArgs class is: public: MouseEventArgs(MouseButtons Button, int Clicks, int X, int Y, int Delta); The properties of the MouseEventArgs class, accessible through the second argument of the MouseMoveevent, and that are the same as those of the constructor, are:
y y y

X: This is the left location of the mouse cursor on the control Y: This is the top location of the mouse cursor on the control Button: This property identifies the button that was pressed. The available buttons (Left, Middle, None,Right, XButton1, and XButton2) are stored in the MouseButtons enumerator. Clicks: This property identifies the number of times the Button was pressed and released Delta: This property identifies number of times the mouse wheel has been rotated

Mouse Hover If the user positions the mouse on a control and moves it over, a MouseHover event is fired. This event is carried by an EventArgs argument that doesn't provide further information than the mouse is hovering over the control. Mouse Down Imagine the user has located a position or an item on a document and presses one of the mouse buttons. While the button is pressed and is down, a button-down message is sent. This event is called MouseDownand is of

type MouseEventArgs. It uses the same characteristics as those we reviewed for the MouseMoveevent. Mouse Up After pressing a mouse button, the user usually releases it. While the button is being released, a button-up message is sent and it depends on the button, left or right, that was down. The event produced is MouseUp. Like the MouseDown message, the MouseUp event is of type MouseEventArgs which is passed to theMouseEventHandler delegate for processing. This event uses the same characteristics as those we reviewed for the MouseMove event. Mouse Leave When the user moves the mouse pointer away from a control, the control fires a MouseLeave event. TheMouseLeave event is of type EventArgs. This means that it is only meant to let you know that the mouse is leaving the control.

Você também pode gostar