Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
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  
  Keyboard input wont work  (Read 532 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-02-27 17:58:23 »

hi guys,
i've been learning with the JavaHug beginner game development and i can't find why the keyListener doesn't work, here is the code,
thank you

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  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
package javagame;

import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class JavaGame extends JFrame{
   
   int x,y;
   
   public class AL extends KeyAdapter{
      public void KeyPressed(KeyEvent e){
         int keyCode = e.getKeyCode();
         if(keyCode == e.VK_LEFT){
            x--;
           
         }
         if(keyCode == e.VK_RIGHT){
            x++;
           
         }
         if(keyCode == e.VK_UP){
            y--;
           
         }
         if(keyCode == e.VK_DOWN){
            y++;
           
         }
      }
     
      public void KeyReleased(KeyEvent e){
         
      }
   }
   
   
   
   public JavaGame(){
      addKeyListener(new AL());
      setTitle("JavaGame");
      setSize(300,250);
      setResizable(false);
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
      x = 150;
      y = 150;
     
     
   }
   
   
   
   
   public void paint(Graphics g){
     
      g.fillOval(x, y, 15, 15);
     
      repaint();
   }
   
   
   public static void main (String[] arghs){
      new JavaGame();
   }
   
}

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline tyeeeee1
« Reply #1 - Posted 2013-02-27 18:50:15 »

I'm not exactly sure what KeyAdapter is or what you're doing with it but here is what a working KeyListener looks like (CnPd from my game):

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  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
package Functions;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyboardListener implements KeyListener
{
    int xpos = 0; //Player's X and Y positions on the current map.
    int ypos = 0;
   
    public KeyboardListener()
    {
    }

    public void keyPressed(KeyEvent event)
    {
        int keyCode = event.getKeyCode();

        if (keyCode == 87) //w key pressed
        {
            ypos++;
        }
        else if (keyCode == 65) //a key pressed
        {
            xpos--;
        }
        else if (keyCode == 83) //s key pressed
        {
            ypos--;
        }
        else if (keyCode == 68) //d key pressed
        {
            xpos++;
        }
        else if (keyCode == 38) //up arrow key pressed
        {
            ypos++;
        }
        else if (keyCode == 37) //left arrow key pressed
        {
            xpos--;
        }
        else if (keyCode == 40) //down arrow key pressed
        {
            ypos--;
        }
        else if (keyCode == 39) //right arrow key pressed
        {
            xpos++;
        }
    }

     public void keyReleased(KeyEvent event)
     {
        int keyCode = event.getKeyCode();

        if (keyCode == 87) //w key released
        {
        }
        else if (keyCode == 65) //a key released
        {
        }
        else if (keyCode == 83) //s key released
        {
        }
        else if (keyCode == 68) //d key released
        {
        }
        else if (keyCode == 38) //up arrow key released
        {
        }
        else if (keyCode == 37) //left arrow key released
        {
        }
        else if (keyCode == 40) //down arrow key released
        {
        }
        else if (keyCode == 39) //right arrow key released
        {
        }
    }

    public void keyTyped(KeyEvent event) //This needs to be here or else an abstract error will be shown when compiling, don't worry; Just keep this here.
    {
    }
   
    //Get methods
    public int getXpos()
    {
        return xpos;
    }
   
    public int getYpos()
    {
        return ypos;
    }
}


Disregard some of the comments, they're for me and not you.
Offline matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #2 - Posted 2013-02-27 19:27:18 »

 Shocked
...

Please, don't use those numbers and comments. Those awesome constants (/finals) were invented:
1  
KeyCode.VK_UP; // "Up" keycode

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-02-28 00:34:39 »

Are you using an IDE? KeyPressed and KeyReleased are not valid KeyAdapter methods, the k should be lowercase. Also, do you absolutely have to have a separate class for event handling? Seems easier to just have the main class implement KeyListener, seeing as you're not doing too much custom stuff in class AL.
That repaint() call in the paintComponent() method looks suspicious, I realize this isn't the full source but you should be using a proper game loop. And yeah, like matheus said, use the statics Smiley
Offline Dane

Senior Newbie





« Reply #4 - Posted 2013-02-28 08:58:55 »

Also if you're going to use more than 3 if statements, you should use a switch:

1  
2  
3  
4  
5  
6  
7  
8  
9  
switch( keyCode )
{
   case KeyCode.VK_UP:
      ypos++;
      break;
   case KeyCode.VK_DOWN:
      ypos--;
      break;
}


For multiple keys doing the same thing:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
switch( keyCode )
{
   case KeyCode.VK_W:
   case KeyCode.VK_UP:
      ypos++;
      break;
   case KeyCode.VK_S:
   case KeyCode.VK_DOWN:
      ypos--;
      break;
}


Good night.  Grin

Doing it wrong since 0x7CC.
Offline alaslipknot
« Reply #5 - Posted 2013-03-01 20:08:56 »

thanx everyone, problem solved, it was caused by the upper case "K" it should be "keyPressed" not "KeyPressed" 

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2013-03-01 20:11:24 »

thanx everyone, problem solved, it was caused by the upper case "K" it should be "keyPressed" not "KeyPressed" 

If you use eclipse:
write @Override in front of your methods like that:
1  
2  
@Override
public void keyPressed(...) { ... }

Then eclipse will tell you, if you misspelled the method name, or have put the wrong method arguments.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
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.

TehJavaDev (34 views)
2014-10-27 03:28:38

TehJavaDev (27 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (59 views)
2014-10-16 15:22:06

Norakomi (48 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
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!