KeyEvent.KEY_TYPED

I want to receive standard desktop keyboard behaviour (one key press = one event and after a while repeated ones, as configured by OS).

public class Game extends Applet implements Runnable {

	int mouseX,mouseY,mouseButton,lastMouseButton,isMouseButtonClicked;
	boolean KEY[]=new boolean[32767];
	int KEYTYPED;
	
	public void start() {
		enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
		new Thread(this).start();
	}

	public void processEvent(AWTEvent e){
        switch (e.getID())
        {
	        case KeyEvent.KEY_TYPED:
	        	KEYTYPED=((KeyEvent) e).getKeyCode();
	        	System.out.println("keyTyped "+((KeyEvent) e).getKeyCode());
	        break;
            case KeyEvent.KEY_PRESSED:
            	KEY[((KeyEvent) e).getKeyCode()]=true;
            	System.out.println("keyRaw "+((KeyEvent) e).getKeyCode());
            break;
            case KeyEvent.KEY_RELEASED:
            	KEY[((KeyEvent) e).getKeyCode()]=false;
            break;
            case MouseEvent.MOUSE_PRESSED:
            	mouseButton=((MouseEvent) e).getButton();
            break;
            case MouseEvent.MOUSE_RELEASED:
            	mouseButton=0;
            break;
            case MouseEvent.MOUSE_MOVED:
            case MouseEvent.MOUSE_DRAGGED:
            	mouseX=((MouseEvent) e).getX();
            	mouseY=((MouseEvent) e).getY();
            break;
        }
	}

KeyEvent.KEY_TYPED always shows 0. Also, it is not reacting to cursor keys at all (when other keys are pressed at least it reports 0).

Is there any particular reason why you’re not using KeyListener (it behaves as what you stated), MouseListener, and MouseMotionListener?

enableEvents(long) and processEvents(AWTEvent) is deprecated API, which is why you should use the new listeners like counterp mentioned.

LOL, I just rewrote the key handler because “Event was outdated and one should use AWTEvent instead”, now you tell me this one is outdated too :smiley:

OK, maybe like this, please drop me the links to examples THAT ARE NOT OUTDATED how to:

  • get a key
  • get mouse position and button state
  • load image and display it on screen

All for pure Java2D (something that would work with Java 1.6) without any frameworks/libraries/etc.

A simple google search would provide you with this page: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

The term that was used for the search is “java keylistener”, as seen with this link: http://www.google.dk/search?sourceid=chrome&ie=UTF-8&q=java+keylistener

It took all of 3 seconds. Way shorter than it took for you to set up that list of yours. For the mouse you’ll google “java mouselistener” and pick the first link.

For the load image and display on screen googling “BufferedImage” is the way to go.

That’s exacly what I did and I ended up thinking that AWTEvent is the proper one. Googling for Java tutorials is useless, you simply don’t know which one is outdated and which one is not (you end up with 3 or more versions and then you has to toss a coin to guess which one to use)…

Are there other non Oracle tutorial for this? The Oracle official site is hard to understand to me (few examples, not even one screenshot, etc).


public class MyMouseListener implements MouseListener {
    public void mouseClicked(MouseEvent e) {
        //almost useless, called when mouse button is released
    }
    
    public void mouseEntered(MouseEvent e) {
        //mouse entered the component
    }
    
    public void mouseExited(MouseEvent e) {
        //mouse exited the component
    }
    
    public void mousePressed(MouseEvent e) {
        //when mouse is pressed
    }
    
    public void mouseReleased(MouseEvent e) {
        //when mouse is released
    }
}

public class MyMouseMotionListener implements MouseMotionListener {
    public void mouseDragged(MouseEvent me) {
        //mouse button is held down and dragged
    }
    
    public void mouseMoved(MouseEvent me) {
        //mouse is moved
    }
}

public class MyKeyListener implements KeyListener {
    public void keyPressed(KeyEvent key) {
        //key is pressed
    }
    
    public void keyReleased(KeyEvent key) {
        //key is released
    }
    
    public void keyTyped(KeyEvent key) {
        //displayable char is released
    }
}

You can of course combine them:


public class MySuperAwesomeListener implements MouseListener, MouseMotionListener, KeyListener {
    ....
}

And you can also use an Adapter to only use certain methods you need:


public class MyMouseListener extends MouseAdapter {
    //not forced to implement all of them
}

MouseListener -> MouseAdapter
MouseMotionListener -> MouseMotionAdapter
KeyListener -> KeyAdapter

Do note that Java is a single-inheritance language, which means a class can only extend 1 class but can implement as many interfaces as it wants.

To add a listener:


JComponent component = new MyCustomJComponent();
component.addMouseListener(new MyMouseListener());
component.addMouseMotionListener(new MyMouseMotionListener());
component.addKeyListener(new MyKeyListener());

You can also use the anonymous inner class syntax to create quick listeners:


component.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent key) {
        if(key.getKeyCode() == KeyEvent.VK_ENTER)
            System.out.println("YOU PRESSED ENTER!!");
    }
});

To load an image, you use the javax.imageio.ImageIO class:


BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource("res/images/MyImage.png"));

getResource(String) takes a path that is relative to the working directory, or the root, of the project. This means in Eclipse, the root is the “bin” folder. In a JAR file, the root is the root of the JAR file or the folder it’s in.

I managed to make the image part, thanks, but no luck with the listener (too many errors, maybe I need some imports or maybe it require JFrame since the function starts with “J” or maybe something esle). Isn’t there really any example of a simple “display an image and move it left right using keys” on the forum?
Again, whenever I try to google it I get tons of unrelated and completely different methods of doing this, so I have no clue which one is the proper one…

Are you importing the right classes? All the event classes are in java.awt.event :stuck_out_tongue:

Of course I agree that listeners are the better approach than using those methods, but I don’t believe they are actually deprecated.

The best place for a simple overview of the API and how to use it, along with links to sample programs, is the official Java Tutorial. Go through it, and you will come out knowing what you need to know.

The Swing Tutorial
Subsection on writing event listeners