Home » Blog » Handling Keyboard Input and Mouse Events

Share This Post

Applets / Webstart / Java / Main Slider / Web Applications

Handling Keyboard Input and Mouse Events

Handling Keyboard Input and Mouse Events

Screen Shot Drag Stick Man

Keyboard Input and Mouse Events

In this example, I show you some simple input examples using Applets. Later I might add Web Start or even Swing Desktop examples. Here we get keyboard input (typing) as keyDown event. And we get mouse positions and clicks. We demonstrated mouse input as move, clicks and mouse drag and drop with a more complex example using a vector sprite stickman.

See also Sound Effects and Music

Keyboard Input

Here I give you sample HTML needed. Put the source and HTML in the same folder and compile the source with ‘javac KeyTest.java’ Save this as ‘keyboard.html’ then run it at the console with ‘appletviewer keyboard.html’ . Note that you will have to give the window focus by clicking it, then when you press a key, output in the console will show which key was pressed. It will also show they key you pressed in the status bar of the applet viewer window.

KeyEvent class

The HTML.

<html>
 <title>Keyboard Input</title>
 <body>
  <applet code="KeyTest.class" codebase="." width=200 height=200>
  </applet>
 </body>
</html>

And here is the source…

import java.applet.*;
import java.awt.*;

public class KeyTest extends Applet{
 public boolean keyDown(Event e, int key){
  if(key==Event.UP){
   System.out.println("UP");
  }else if(key==Event.F1){
   System.out.println("F1");
  }else{
   if(e.controlDown())
    System.out.print("CTRL+");
   System.out.println("key:"+(char)key);
  }
  showStatus("Hello you pressed "+(char)key);
  return true;
 }

}

 

Mouse Move and Click Input

The HTML in a file called say ‘mouse.html’. Run it with ‘appletviewer mouse.html’.

MouseEvent class

<html>
 <title>Mouse Input</title>
 <body>
  <applet code="MouseTest.class" codebase="." width=200 height=200>
  </applet>
 </body>
</html>

 

import java.applet.*;
import java.awt.*;

public class MouseTest extends Applet{
public boolean mouseDown(Event e, int x, int y){
System.out.println("mouseDown at (" + x +", "+ y + ")");
return true;
}
public boolean mouseUp(Event e, int x, int y){
System.out.println("mouseUp at (" + x +", "+ y + ")");
return true;
}
public boolean mouseMove(Event e, int x, int y){
System.out.println("mouseMove at (" + x +", "+ y + ")");
return true;
}
public boolean mouseDrag(Event e, int x, int y){
System.out.println("mouseDrag at (" + x +", "+ y + ")");
return true;
}
public boolean mouseEnter(Event e, int x, int y){
System.out.println("mouseEnter at (" + x +", "+ y + ")");
return true;
}
public boolean mouseExit(Event e, int x, int y){
System.out.println("mouseExit at (" + x +", "+ y + ")");
return true;
}
}

Mouse Drag and Drop

Here you may click and hold, drag and drop by clicking in any of the rectangular area surrounding the stick man. I’m not sure how this might work if we had two stick men. I may need to try that next.

MouseDragEvent Class

<html>
 <title>Stick Man Drag and Drop</title>
 <body>
  <applet code="DragStickManApplet.class" codebase="." width=200 height=200>
  </applet>
 </body>
</html>

Sprites

First we need a few classes for the Sprites. Start with the abstract Sprite Class which is extended by Sprite2D. I could have put all of the first 3 classes together in one class but this is the way I chose to do it. Its a bit more extendable.

import java.awt.*;
public abstract class Sprite{
protected boolean visible;
protected boolean active;
abstract void paint(Graphics g);
abstract void update();
public boolean isVisible(){
return visible;
}
public void setVisible(boolean visible){
this.visible=visible;
}
public boolean isActive(){
return active;
}
public void setActive(boolean active){
this.active=active;
}
public void suspend(){
setVisible(false);
setActive(false);
}
public void restore(){
setVisible(true);
setActive(true);
}
}

Sprite2D

Next Sprite2D which extends and implements the above abstract Sprite class.

import java.awt.*;

public abstract class Sprite2D extends Sprite{
protected int locx;
protected int locy;
Color color;
boolean fill;
public boolean getFill(){
return fill;
}
public void setFill(Boolean fill){
this.fill=fill;
}
public Color getColor(){
return color;
}
public void setColor(Color color){
this.color=color;
}
}

Stick Man Sprite

Next we have StickManSprite class that extends Sprite2D.

import java.awt.*;

public class StickManSprite extends Sprite2D{
int x,y;
int h,w;
Color c;
boolean fill;

public StickManSprite (int x, int y, int w, int h, Color c){
this.x=x;
this.y=y;
this.w=w;
this.h=h;
this.c=c;
restore();
}
public void update(){
}
public void paint(Graphics g){
int neckx=(int)(w/2);
int necky=(int)(h/3);
int waistx=(int)(w/2);
int waisty=(int)(h/3*2);
if(visible){
g.setColor(c);
if(fill){
g.fillOval(neckx+x-2,necky+y-7,5,5);
g.drawLine(neckx+x,necky+y,x,y);
g.drawLine(neckx+x,necky+y,x+w,y);
g.drawLine(neckx+x,necky+y,waistx+x,waisty+y);
g.drawLine(waistx+x,waisty+y,x,y+h);
g.drawLine(waistx+x,waisty+y,x+w,y+h);
}else{
g.drawOval(neckx+x-2,necky+y-7,5,5);
g.drawLine(neckx+x,necky+y,x,y);
g.drawLine(neckx+x,necky+y,x+w,y);
g.drawLine(neckx+x,necky+y,waistx+x,waisty+y);
g.drawLine(waistx+x,waisty+y,x,y+h);
g.drawLine(waistx+x,waisty+y,x+w,y+h);
}
}
}
}

Drag Stick Man

Next we have DragStickMan class that extends StickManSprite.

import java.awt.*;

public class DragStickMan extends StickManSprite {
protected boolean draggable;
public void setDraggable(boolean draggable){
this.draggable=draggable;
}
public boolean isDraggable(){
return draggable;
}
public boolean inside(int x, int y){
return (this.x<=x&&this.y<=y&&(this.x+w>=x)&&(this.y+h>=y));
}
public void translate(int x, int y){
this.x+=x;
this.y+=y;
}
public void grow(){
w++;
h++;
}
public void shrink(){
if(w>0){
w--;
}
if(h>0){
h--;
}
}
public DragStickMan(int x, int y, int w, int h, Color c){
super(x,y,w,h,c);
fill=true;
draggable=false;
}
}

Drag Stick Man Applet

And finally, we have DragStickManApplet. Put all these source files in the same folder. Compile them with ‘javac *.java’. Put the HTML file in a file called ‘stickman.html’ in the same folder. Then use ‘appletviewer stickman.html’ to run it.

import java.awt.*;
import java.applet.*;

public class DragStickManApplet extends Applet{
Font courierFont;
String testString = "Drag the StickMan!";
DragStickMan dragStickMan = new DragStickMan(0,0,50,100,Color.red);
public void init(){
courierFont = new Font("Courier", Font.BOLD+Font.ITALIC,14);
}
public void paint(Graphics g){
g.setFont(courierFont);
FontMetrics fontMetrics = g.getFontMetrics();
int stringWidth = fontMetrics.stringWidth(testString);
int width = (bounds().width = stringWidth)/2;
int height= bounds().height/2;
g.setColor(Color.green);
g.drawString(testString,width,height);
dragStickMan.paint(g);
}
int oldx,oldy;
public boolean mouseDown(Event e, int x, int y){
if(dragStickMan.inside(x,y)){
oldx = x;
oldy = y;
dragStickMan.setDraggable(true);
}
return true;
}
public boolean mouseUp(Event e, int x, int y){
dragStickMan.setDraggable(false);
return true;
}
public boolean mouseDrag(Event e, int x, int y){
if(dragStickMan.isDraggable()){
dragStickMan.translate(x-oldx,y-oldy);
oldx=x;
oldy=y;
repaint();
}
return true;
}
public boolean keyDown(Event e, int key){
switch(key){
case Event.RIGHT:
dragStickMan.grow();
repaint();
break;
case Event.LEFT:
dragStickMan.shrink();
repaint();
break;
default:
break;
}
return true;
}
}

 

Share This Post

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>