Keyboard and Mouse Events Handling

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Handling Keyboard Events

A user interacts with the application by pressing either keys on the keyboard or by using
mouse. A programmer should know which key the user has pressed on the keyboard or
whether the mouse is moved, pressed, or released. These are also called ‘events’. Knowing
these events will enable the programmer to write his code according to the key pressed or
mouse event.

KeyListener interface of java.awt.event package helps to know which key is pressed or


released by the user. It has 3 methods
1. Public void keyPressed(KeyEvent ke): This method is called when a key is pressed
on the keyboard. This include any key on the keyboard along with special keys like
function keys, shift, alter, caps lock, home, end etc.

2. Public void keyTyped(keyEvent ke) : This method is called when a key is typed on
the keyboard. This is same as keyPressed() method but this method is called when
general keys like A to Z or 1 to 9 etc are typed. It cannot work with special keys.

3. Public void keyReleased(KeyEvent ke): this method is called when a key is release.

KeyEvent class has the following methods to know which key is typed by the user.

1. Char getKeyChar(): this method returns the key name (or character) related to the key
pressed or released.
2. Int getKeyCode(): this method returns an integer number which is the value of the
key presed by the user.

The follwing are the key codes for the keys on the keyboard. They are defined as
constants in KeyEvent class. Remember VK represents Virtual Key.
 To represent keys from a to z : VK_A to VK_Z.
 To represent keys from 1 to 9: VK_0 to VK_9.
 To represent keys from F1 to F12: VK_F1 to VK_F12.
 To represent home, end : VK_HOME, VK_END.
 To represent PageUp, PageDown: VK_PAGE_UP, VK_PAGE_DOWN
 To represent Insert, Delete: VK_INSERT, VK_DELETE
 To represent caps lock: VK_CAPS_LOCK
 To represent alter key: VK_ALT
 To represent Control Key: VK_CONTROL
 To represent shift: VK_SHIFT
 To represent tab key: VK_TAB
 To represent arrow keys: VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN
 To represent Escape key: VK_ESCAPE
 Static String getKeyText(int keyCode); this method returns a string describing the
keyCode such as HOME, F1 or A.
Program: to trap a key which is pressed on the keyboard and display its name in the
text area.

package com.myPack;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class KeyBoardEvents extends JFrame implements KeyListener
{
private static final Font Font = null;
Container c;
JTextArea a;

String str = " ";


KeyBoardEvents()
{
c=getContentPane();
a = new JTextArea("Press a Key");

a.setFont(new Font ("Times New Roman", Font.BOLD,30));


c.add(a);
a.addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
int keycode = ke.getKeyCode();
if(keycode == KeyEvent.VK_F1) str += "F1 Key";
if(keycode == KeyEvent.VK_F2) str += "F2 Key";
if(keycode == KeyEvent.VK_F3) str += "F3 Key";
if(keycode == KeyEvent.VK_PAGE_UP) str += "Page UP";
if(keycode == KeyEvent.VK_PAGE_DOWN) str += "Page Down";
a.setText(str);
str =" " ;
}
public void keyRelease(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
}
public static void main(String args[])
{
KeyBoardEvents kbe = new KeyBoardEvents();
kbe.setSize(400,400);
kbe.setVisible(true);
kbe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub

Output:

Handling Mouse Events


The user may click, release, drag, or move a mouse while interacting with athe
application. If the programmer knows what the user has done, he can write the code
according to the mouse events. To trap the mouse events, MouseListener and
MouseMotionListener interfaces of java.awt.event package are use.

MouseListener interface has the following methods.

1. void mouseClicked(MouseEvent e); void MouseClicked this method is invoked


when the mouse button has been clicked(pressed and released) on a component.
2. void mouseEntered(MouseEvent e): this method is invoked when the mouse enters
a component.
3. void mouseExited(MouseEvent e): this method is invoked when the mouse exits a
component
4. void mousePressed(MouseEvent e): this method is invoked when a mouse button
has been pressed on a component.
5. void mouseRelease(MouseEvent e): this method is invoked when a mouse button
has been released ona component.

MouseMotionListener interface has the following methods.


1. void mouseDragged(MouseEvent e): this method is invoked when a mouse button is
pressed on a component and then dragged.
2. void mouseMoved(MouseEvent e): this method is invoked when a mouse cursor has
been moved onto a component and then dragged.
The MouseEVent class has the following methods
1. int getButton(): this method returns a value representing a mouse button, when it
is clicked it retursn 1 if left button is clicked, 2 if middle button, and 3 if right
button is clicked.
2. int getX(); this method returns the horizontal x position of the event relative to the
source component.
3. int getY(); this method returns the vertical y postion of the event relative to the
source component.

Program to create a text area and display the mouse event when the button on the
mouse is clicke, when mouse is moved, etc. is done by user.

package com.myPack;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MouseEvents extends JFrame implements MouseListener, MouseMotionListener
{
String str =" ";
JTextArea ta;
Container c;
int x, y;

MouseEvents()
{
c=getContentPane();
c.setLayout(new FlowLayout());

ta = new JTextArea("Click the mouse or move it", 5, 20);


ta.setFont(new Font("Arial", Font.BOLD, 30));
c.add(ta);

ta.addMouseListener(this);
ta.addMouseMotionListener(this);
}

public void mouseClicked(MouseEvent me)


{
int i = me.getButton();
if(i==1)
str+= "Clicked Button : Left";
else if(i==2)
str+= "Clicked Button : Middle";
else if(i==3)
str+= "Clicked Button : Right";
this.display();
}
public void mouseEntered(MouseEvent me)
{
str += "Mouse Entered";
this.display();
}
public void mouseExited(MouseEvent me)
{
str += "Mouse Exited";
this.display();
}
public void mousePressed(MouseEvent me)
{
x = me.getX();
y= me.getY();
str += "Mouse Pressed at :" +x + "\t" + y;
this.display();
}
public void mouseReleased(MouseEvent me)
{
x = me.getX();
y= me.getY();
str += "Mouse Released at :" +x + "\t" + y;
this.display();
}
public void mouseDragged(MouseEvent me)
{
x = me.getX();
y= me.getY();
str += "Mouse Dragged at :" +x + "\t" + y;
this.display();
}
public void mouseMoved(MouseEvent me)
{
x = me.getX();
y= me.getY();
str += "Mouse Moved at :" +x + "\t" + y;
this.display();
}

public void display()


{
ta.setText(str);
str=" ";
}

public static void main(String args[])


{
MouseEvents mes = new MouseEvents();
mes.setSize(400,400);
mes.setVisible(true);
mes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy