180 likes | 407 Views
Event Handling. In this class we will cover:. Keyboard Events Mouse Events Focus Events Action Interface Multicasting. Keyboard Events. Table 8-1 on page 298 shows all AWT listener interfaces, events, and event sources. Keyboard Events
E N D
In this class we will cover: • Keyboard Events • Mouse Events • Focus Events • Action Interface • Multicasting
Keyboard Events • Table 8-1 on page 298 shows all AWT listener interfaces, events, and event sources. • Keyboard Events • When the user pushes a key, a key_Pressed KeyEvent is generated. • When the user releases a key, a key_Released KeyEvent is generated. • You trap these events in the keyPressed and the keyReleaesd methods of any class that implements the KeyListener interface. • A third method, keyTyped, combines the two and it reports on the characters that were generated by the user’s keystrokes.
Keyboard Events • The difference between characters and virtual key codes: • Virtual key codes correspond to keys on the keyboard. • e.g. VK_A denotes the key marked A • There is no separate lowercase virtual key code - the keyboard does not have separate lowercase keys. • Virtual key codes are indicated with a prefix of VK_ such as VK_A or VK_SHIFT • Here are the five events that are generated when a user tyes an uppercase A • Pressed the shift key (keyPressed called for VK_SHIFT) • Pressed the A key (keyPressed called for VK_A) • Typed “A” (keyTyped called for an “A”) • Released the A key (keyReleased called for VK_A) • Released the shift key (keyReleased called for VK_SHIFT)
Keyboard Events • So, how can you see the virtual key codes:public void keyPressed (KeyEvent event) { int keyCode = event.getKeyCode();} • You can use the isShiftDown, isControlDown, isAltDown, and isMetaDown instead of looking for the virtual key codespublic void keyPressed (KeyEvent event) { int keyCode = event.getKeyCode(); if (keyCode == KeyEvent.VK_RIGHT && event.isShiftDown()) {…}}
Keyboard Events • To obtain the actual character that was typed use the keyTyped method and call the getKeyChar method.public void keyTyped (KeyEvent event) { char keyChar = event.getKeyChar();} • Note: not all keystrokes result in a call to keyTyped. Only those keystrokes that generate a Unicode character can be captured in the keyTyped method. You need the keyPressed method to check for cursor keys and other command keys.
Mouse Events • When a user clicks a mouse button, 3 listener methods are called: • mousePressed, mouseReleased and mouseClicked • You can use the getX and getY methods of the MouseEvent argument to get the x and y- coordinates of the mouse pointer. • If you want to distinguish between single, double and triple clicks, use the getClickCount method. • You can also change the cursor moved by the mouse with the getPredefinedCursor method of the Cursor class. See pg. 307 for sample cursor shapes. • If the user presses a button while the mouse is in motion, mouseDragged calls are generated instead of mouseClicked calls.
Mouse Events • How do we listen to mouse events? • Mouse clicks are reported through the mouseClicked procedure, which is part of the MouseListener interface. • Mouse move and drag events are defined in a separate interface called MouseMotionListener. • because mouse move events occur much more frequently • See www2.bc.edu/~bernier/MC697/LectureNotes/Sketch.java
Focus Events • When you type a keystroke, you keystrokes must go to a specific screen object. • The window manager (e.g. Windows) directs all keystrokes to the active window. • When the active window is controlled by a Java program, the Java window receives the keystrokes and directs them towards a particular component. • This component is then said to have focus. • When a component has focus, it usually gives a visual cue • text fields have a blinking cursor • a button has a rectangle around it • When a text field has focus, you can type in it. • When a button has focus, you can click on it.
Focus Events • Only one component at a time can have focus. • When a user selects a component, that component is said to gain focus. • Conversely, when a user selects a different component, the original component is said to lose focus. • Some components, such as labels and panels, do not get focus by default because it is assumed that they are just there for decorations or grouping. • To override this (for example, to implement a drawing program with panels that paint something in reaction to keystrokes), you need to call:panel.setFocusable(true);
Actions • It is common to have multiple ways to activate the same command. • You can do this by linking all he events to the same listener. • For example, you can have a button that changes the background color and you can have an alternative way of changing the background color by using a set of keystrokes (e.g. alt-b). • The Swing package provides a useful mechanism to encapsulate commands and attach them to multiple event sources - the Action interface
Action Interface • The Action object encapsulates the following: • a description of the command (as a text string and optional icon) • parameters that are necessary to carry out the command (e.g. color to set). • You can store and retrieve arbitrary name/value pairs in the aciton object using the putValue and getValue methods • action.putValue(Action.NAME, “Blue”);action.putValue(Action.SMALLICON, new ImageIcon(“blue-ball.gif”));
Action Interface • The Action interface and the AbstractAction adapter class are very useful when using menus. We will discuss this more when we talk about Swing User Interfaces. • Read more about the Action interface on page 316.
Putting it all together • See www2.bc.edu/~bernier/MC697/LectureNotes/Sketch.java
Final Notes on Event Handling • Multicasting • All AWT event sources support can send the same event to multiple listeners. • There is no guarantee about the order in which the events are delivered to the set of registered listeners. • Customizing Events • You can manipulate the event queue directly to add or remove events. This can be useful sometimes. • You can also create a new custom event type and insert it into the event queue and then have it dispatched to a listener - just like regular AWT events. • Read more about customizing events on page 32.