Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  KeyEvent.KEY_TYPED  (Read 1790 times)
0 Members and 1 Guest are viewing this topic.
Offline Archibald

Junior Devvie


Projects: 1



« Posted 2012-02-29 18:11:52 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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).

Offline counterp

Senior Devvie


Medals: 11



« Reply #1 - Posted 2012-02-29 19:17:40 »

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

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2012-02-29 19:18:42 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Archibald

Junior Devvie


Projects: 1



« Reply #3 - Posted 2012-02-29 20:49:11 »

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 Cheesy

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.
Offline Regenuluz
« Reply #4 - Posted 2012-02-29 20:54:36 »

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.
Offline Archibald

Junior Devvie


Projects: 1



« Reply #5 - Posted 2012-02-29 21:07:08 »

A simple google search would provide you with
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).
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-02-29 21:20:06 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
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:
1  
2  
3  
public class MySuperAwesomeListener implements MouseListener, MouseMotionListener, KeyListener {
    ....
}


And you can also use an Adapter to only use certain methods you need:
1  
2  
3  
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:
1  
2  
3  
4  
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:
1  
2  
3  
4  
5  
6  
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:
1  
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.

Offline Archibald

Junior Devvie


Projects: 1



« Reply #7 - Posted 2012-03-01 11:13:28 »

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...
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-03-02 02:43:16 »

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

Offline BoBear2681

JGO Coder


Medals: 19



« Reply #9 - Posted 2012-03-02 03:55:57 »

enableEvents(long) and processEvents(AWTEvent) is deprecated API

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

Quote from: Archibald
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)...

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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (37 views)
2014-12-09 22:41:13

BurntPizza (72 views)
2014-12-08 04:46:31

JscottyBieshaar (34 views)
2014-12-05 12:39:02

SHC (46 views)
2014-12-03 16:27:13

CopyableCougar4 (42 views)
2014-11-29 21:32:03

toopeicgaming1999 (110 views)
2014-11-26 15:22:04

toopeicgaming1999 (96 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!