Input: Keyboard (2) A partial list of virtual codes: Description

Input: Introduction • The following classes and interfaces are used for keyboard and mouse events – KeyListener – KeyAdapter – MouseListener – MouseMo...
Author: Elwin Charles
2 downloads 2 Views 107KB Size
Input: Introduction • The following classes and interfaces are used for keyboard and mouse events – KeyListener – KeyAdapter – MouseListener – MouseMotionListener – MouseAdapter – MouseInputListener – MouseWheelListener • These events work for anything derived from java.awt.Component • To enable a Component to be notified of a specific type of event, a listener for the given type of event must be added: – public void addKeyListener(java.awt.event.KeyListener l) – public void addMouseListener(java.awt.event.MouseListener l) – public void addMouseMotionListener(java.awt.event.MouseMotionListener l) – public void addMouseWheelListener(java.awt.event.MouseWheelListener l) • All Listeners are used similarly: – The add***Listener call is usually placed in initialization code (e.g., constructor) – The parameter is a reference to the object requesting notification of the event – Event notification will be sent to the registered Listener

1

Input: Keyboard • Interface KeyListener – Listens for keyboard events – Sends events to callback function that you implement for handling the event – Program must implement three methods: 1. public abstract void keyPressed(KeyEvent e) ∗ Called on a key press event 2. public abstract void keyReleased(KeyEvent e) ∗ Called on a key release event 3. public abstract void keyTyped(KeyEvent e) ∗ Called when a key is typed (press followed by release) – To access values from keyboard, use 1. public char getKeyChar() ∗ Use with keyTyped() ∗ Returns character typed ∗ If there is not a corresponding unicode character, CHAR UNDEFINED is returned 2. public char getKeyCode() ∗ Use with keyPressed() and keyReleased() ∗ Returns virtual key code typed · Virtual codes contained in KeyEvent class · The codes are independent of any platform or device

2

Input: Keyboard (2) · A partial list of virtual codes: Code VK LEFT VK RIGHT VK UP VK DOWN VK KP LEFT VK KP RIGHT VK KP UP VK KP DOWN VK 0 .. VK 9 VK A .. VK Z VK F1 .. VK F12 VK ENTER VK SHIFT VK TAB VK ALT VK META VK CONTROL

Description left arrow right arrow up arrow down arrow left keypad arrow right keypad arrow up keypad arrow down keypad arrow numeric keys alphabetic keys function keys enter key shift key tab key alt key meta key control key

– public int getID() ∗ Returns type of event that has occurred: · KEY PRESSED · KEY RELEASED · KEY TYPED – public boolean isActionKey() ∗ Returns true if one of following keys is typed: · · · ·

3

Input: Keyboard (3) • For example (modified from Deitel and Deitel): import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SampleListener extends JFrame implements KeyListener { //declarations public SampleListener() { //... addKeyListener(this); //... } public void keyPressed (KeyEvent e) { String s = "" + e.getKeyText(); //perform appropriate processing } //more KeyListener method definitions public static void main (String args[]) { //... SampleListener myListener = new SampleListener(); //... } }

4

Input: Keyboard (4) • Class KeyAdapter – Abstract class that implements KeyListener – Implements all three KeyListener methods with empty bodies – Useful substitute for KeyListener when only want to use one or two of the key methods ∗ Allows you to override only the method(s) you need – For example (modified from Deitel and Deitel): import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SampleAdapter extends JFrame { //declarations public SampleAdapter() { //... } //method definitions public static void main (String args[]) { //... SampleAdapter myAdapter = new SampleAdapter(); myAdapter.addKeyListener(new KeyAdapter () { public void keyPressed (KeyEvent e) { String s = "" + e.getKeyText(); //perform appropriate processing } ); //... } }

5

Input: Keyboard (5) OR import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SampleAdapter extends JFrame { //declarations public SampleAdapter() { //... addKeyListener(new myKeyAdapter () { //... } //method definitions public static void main (String args[]) { //... SampleAdapter myAdapter = new SampleAdapter(); //... } //inner class implements adapter private class myKeyAdapter extends KeyAdapter () { public void keyPressed (KeyEvent e) { String s = "" + e.getKeyText(); //perform appropriate processing } } }

6

Input: Mouse • There are two interfaces that can be used for a mouse: 1. MouseListener 2. MouseMotionListener • Both must be initialized with the appropriate method: 1. addMouseListener() 2. addMouseMotionListener() • Interface MouseListener – Five methods must be implemented: 1. public abstract void mouseClicked(MouseEvent e) ∗ Called when mouse clicked (press and release) 2. public abstract void mouseEntered(MouseEvent e) ∗ Called when mouse enters the window 3. public abstract void mouseExited(MouseEvent e) ∗ Called when mouse leaves the qwindow 4. public abstract void mousePressed(MouseEvent e) ∗ Called when mouse pressed 5. public abstract void mouseReleased(MouseEvent e) ∗ Called when mouse released • Interface MouseMotionListener – Two methods must be implemented: 1. public abstract void mouseDragged(MouseEvent e) ∗ Called when mouse moved with button held 2. public abstract void mouseMoved(MouseEvent e) ∗ Called when mouse moved

7

Input: Mouse (2) • Class MouseEvent 1. public int getButton() – Returns BUTTON1, BUTTON2, or BUTTON3 – Valid for mouseClicked(), mousePressed() 2. public int getX() – Returns x coordinate of mouse when event occurs – Valid for all mouse methods 3. public int getY() – Returns y coordinate of mouse when event occurs – Valid for all mouse methods 4. public Point getPoint() – Returns (x, y) Point object of mouse when event occurs 5. public Point getClickCount() – Returns how many clicks were executed – Valid for mouseClicked(), mousePressed(), mouseReleased() 6. public int getID() – Returns type of event that has occurred: ∗ MOUSE CLICKED ∗ MOUSE DRAGGED ∗ MOUSE ENTERED ∗ MOUSE EXITED ∗ MOUSE MOVED ∗ MOUSE PRESSED ∗ MOUSE RELEASED • Class MouseAdapter – Abstract class that implements MouseListener – Comparable to KeyAdapter

8

Input: Mouse (3) • Class MouseMotionAdapter – Similar to above • Interface MouseInputListener – Defined in javax.swing.event – Implements both MouseListener and MouseMotionListener – Has all seven mouse methods • Interface MouseWheelListener – Used with mice having wheels – One method must be implemented: 1. public abstract void mouseWheelMoved(MouseWheelEvent e) • Class MouseWheelEvent – public int getWheelRotation() ∗ Returns the number of clicks the wheel has turned ∗ Negative values indicate the wheel has rotated away from the user (up)

9

Input: InputEvent class • Class InputEvent class is super class of MouseEvent and KeyEvent classes – public int getModifiers() ∗ Returns a bit mask that can be used to determine if several keys/buttons were pressed ∗ Valid for keyTyped(), keyPressed(), mouseClicked(), mousePressed() ∗ Used in conjunction with following masks: · BUTTON1 MASK · BUTTON2 MASK · BUTTON3 MASK · ALT MASK · CTRL MASK · META MASK · SHIFT MASK ∗ Use bitwise AND of mask with value returned to determine if a specific set of actions occurred – Following methods can also be used: ∗ ∗ ∗ ∗

public public public public

boolean boolean boolean boolean

isAltDown() isControlDown() isMetaDown() isShiftDown()

– public long getWhen() ∗ Returns time when event occurs

10

Input: Window Events • Window Listeners are added to a Component as described above: – public void addFocusListener(java.awt.event.FocusListener l) – public void addWindowListener(java.awt.event.WindowListener l) • Interface WindowListener – Comparable to KeyListener and MouseListener – Follwing seven methods must be defined by implementor: 1. public abstract void windowActivated(WindowEvent e) ∗ Called when window gains focus 2. public abstract void windowClosed(WindowEvent e) ∗ Called when hide() or dispose() has been called 3. public abstract void windowClosing(WindowEvent e) ∗ Called when user has requested that the window be closed; e.g., via a close button, system menu, etc. 4. public abstract void windowDeactivated(WindowEvent e) 5. public abstract void windowDeiconified(WindowEvent e) 6. public abstract void windowIconified(WindowEvent e) 7. public abstract void windowOpened(WindowEvent e) • Class WindowAdapter – Like the mouse and key adapter classes • Class WindowEvent – public int getID() ∗ Returns type of event that has occurred: · WINDOW ACTIVATED · WINDOW CLOSED · WINDOW CLOSING · WINDOW DEACTIVATED · WINDOW DEICONIFIED · WINDOW ICINIFIED · WINDOW OPENED

11

Input: Window Events (2) • Interface FocusListener – Following two methods must be defined by implementor: 1. public abstract void focusGained(FocusEvent e) 2. public abstract void focusLost(FocusEvent e) • Class FocusAdapter – Like the mouse and key adapter classes • Class FocusEvent – public int getID() ∗ Returns type of event that has occurred: · FOCUS GAINED · FOCUS LOST – public boolean isTemporary() ∗ Returns true if focus loss is temporary; e.g., due to pop-up menu, scroll bar, etc.

12