CS396N - Web Programming |
Spring 2002 |
Chapter 11 - Mouse and Keyboard Events |
What are Events?
Interfaces
}
Event | Generated by | Listener Interface | Methods in them |
Action event | Pressing a button | ActionListener | actionPerformed() |
Adjustment event | Scroll bar is manipulated | AdjustmentListener | adjustmentValueChanged() |
Component event | A component is hidden, moved, resized, or shown | ComponentListener | componentHidden()
componentMoved() componentResized() componentShown() |
Container event | A component is added or removed from a container | ContainerListener | componentAdded()
componentRemoved() |
Focus event | A component gains or loses focus | FocusListener | focusGained()
focusLost() |
Item event | An item is selected or deselected | ItemListener | itemStateChanged() |
Key event | A key is pressed, released or typed | KeyListener | keyPressed()
keyReleased() keyTyped() |
Mouse event | Mouse button is clicked, pressed or released. Mouse pointer enters leaves a component | MouseListener | mouseClicked()
mouseEntered() mouseExited() mousePressed() mouseReleased() |
Mouse event | Mouse pointer is dragged or moved | MouseMotionListener | mouseDragged()
mouseMoved() |
Text event | Text value is changed | TextListener | textValueChanged() |
Window event | A window is activated, closed, deactivated, deiconfied, opened, or quit | WindowListener | windowActivated()
windowClosed() windowClosing() windowDeactivated() windowDeiconified() windowIconified() windowOpened() |
} //Several listener interfaces can be listed,
separated by commas.
Step 4: Define all the methods of the implemented
listener interfaces. I
Event Class | MouseEvent |
Listener Interface | MouseListener |
Listener Methods | public void mouseEntered(MouseEvent event)
public void mouseExited(MouseEvent event) public void mouseClicked(MouseEvent event) public void mousePressed(MouseEvent event) public void mouseReleased(MouseEvent event) By looking at the name of the methods, it is clear which activity invokes which method. event.getX() returns (int) the x coordinate of mouse position.
|
Four steps:
Step 1: Include the following import statement:
Step 2: Include Implements MouseListener
:
}
Step 4: Define the five MouseListener methods:
}
public void mouseEntered(MouseEvent event){
}
public void mouseexited(MouseEvent event){
}
public void mousePressed(MouseEvent event){
}
public void mouseReleased(MouseEvent event){
}
public class MouseEvent1 extends Applet implements MouseListener{
public void init() {
public void mouseClicked(MouseEvent e){
public void mouseEntered(MouseEvent e){
public void mouseExited(MouseEvent e){
public void mousePressed(MouseEvent e){
public void mouseReleased(MouseEvent e){
}
Event Class | MouseEvent |
Listener Interface | MouseMotionListener |
Listener Methods | public void mouseMoved(MouseEvent
event)
Invoked when the mouse button has been moved on a component (with no buttons down). public void mouseDragged(MouseEvent event) Invoked when a mouse button is pressed on a component and then dragged. event.getX() returns (int) the x coordinate of mouse position. event.getY() returns (int) the y coordinate of mouse position. event.getClickCount() returns (int) the number of mouse clicks. |
public class MouseEvent2 extends Applet
implements
MouseMotionListener,MouseListener{
public void init() {
public void mouseMoved(MouseEvent event){
public void mouseDragged(MouseEvent event){
public void mouseClicked(MouseEvent e){
public void mouseEntered(MouseEvent e){
public void mouseExited(MouseEvent e){
public void mousePressed(MouseEvent e){
public void mouseReleased(MouseEvent e){
}
public class MouseEvent3 extends Applet
implements
MouseMotionListener,MouseListener{
public void init() {
public void mouseMoved(MouseEvent event){
public void mouseDragged(MouseEvent event){
public void mouseClicked(MouseEvent event){
public void mouseEntered(MouseEvent event){
public void mouseExited(MouseEvent event){
public void mousePressed(MouseEvent event){
public void mouseReleased(MouseEvent event){
public void print(int x, int y){
Whenever a key is pressed, an object of the KeyEvent class is generated.
Event Class | KeyEvent |
Listener Interface | KeyListener |
Listener Methods | public void keyPressed(KeyEvent event)
Invoked when a key is pressed. public void keyReleased(KeyEvent event) Invoked when a key is released. public void keyTyped(KeyEvent event) Invoked when a key is typed (pressed and released). |
KeyEvent class | For a list of Class variables (virtual key codes),
which are defined as static and final, see Virtual Key Codes
table below.
event.getKeyChar() returns (char) the character pressed. event.getKeyCode() returns (int) the "virtual key code", see table below. event.getModifiers(). event.isShiftDown() returns (boolean) true if shift key is down. event.isControlDown() returns (boolean) true if control key is down. event.isAltDown() returns (boolean) true if alt key is down KeyEvent.getKeyModifiersText(int modifiers) returns (String). KeyEvent.getKeyText(int keycode) returns (String). |
Virtual Key Codes
Alpha keys | VK_A through VK_Z |
Numeric keys | VK_0 through VK_9 |
Function keys | VK_F1 through VK_F12 |
Action keys | VK_BACK_SPACE
VK_ESCAPE, VK_PRINTSCREEN, VK_SCROLL_LOCK, VK_PAUSE |
Num pad number keys | VK_NUMPAD0 through VK_NUMPAD9 |
Other Num pad keys | VK_NUM_LOCK, VK_SLASH, VK_MULTIPLY, VK_SUBTRACT, VK_ADD, VK_HOME, VK_UP, VK_PAGE_UP, VK_LEFT, VK_RIGHT, VK_END, VK_DOWN, VK_PAGE_DOWN, VK_INSERT, VK_DELETE, VK_ENTER |
Modifier keys | VK_ALT, VK_CAPS_LOCK, VK_CONTROL, VK_META, VK_SHIFT |
Punctuation keys | VK_SPACE, VK_ENTER, VK_TAB, VK_BACK_QUOTE, VK_SLASH, VK_BACK_SLASH, VK_OPEN_BRACKET, VK_CLOSE_BRACKET, VK_PERIOD, VK_QUOTE, VK_SEMICOLON, VK_SEPARATOR, VK_COMMA, VK_DECIMAL, VK_EQUALS, |
Other keys | VK_ACCEPT, VK_CANCEL, VK_CLEAR, VK_CONVERT, VK_FINAL, VK_HELP, VK_KANA, VK_KANJI, VK_MODECHANGE, VK_NONCONVERT, VK_UNDEFINED, |
Note
Example
The following program displays several text boxes. Type any key in top box. The other boxes in the applet show information about the key pressed.
public void init(){
input = new TextField(30);
input.addKeyListener(this);
output1 = new TextField(30);
output2 = new TextField(30);
output3 = new TextField(30);
output4 = new TextField(30);
output5 = new TextField(30);
output6 = new TextField(30);
output7 = new TextField(30);
output8 = new TextField(30);
add(input);
add(output1);
add(output2);
add(output3);
add(output4);
add(output5);
add(output6);
add(output7);
add(output8);
}
}
public void keyPressed(KeyEvent event){
output2.setText("Virtual key code value
= "+n);
output3.setText("Name of key pressed =
"+KeyEvent.getKeyText(n));
output4.setText("Shift was pressed = "+event.isShiftDown());
output5.setText("Control key down = "+event.isControlDown());
output6.setText("Alt key was pressed =
"+event.isAltDown());
output7.setText("Modifiers value = "+event.getModifiers());
output8.setText("Modifier text
="+KeyEvent.getKeyModifiersText(modifiers));
public void keyReleased(KeyEvent event) {
}
}
Example
The following program displays two boxes. Type each keyboard key in the first box and see result in the second box.
public void keyTyped(KeyEvent event){
}
public void keyPressed(KeyEvent event){
String string = " ";
switch(event.getKeyCode()){
}
public void keyReleased(KeyEvent event) {
}
}