Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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  
  Key input ignored after clicking on components.  (Read 176 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-10-08 14:48:17 »

Hey,

I've just started to notice that whenever I click on anything other than my JFrame after starting my program for testing the key input is completely ignored, even if I click on the JFrame again. The KeyListener, which is below, is added to the JFrame at the start of the program. I don't see any reason for the key listener to just stop working when I click on my canvas or JTextArea.

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  
package valkryst.core;

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

/**
 * This class handles all keyboard input from the user.
 *
 * @author Valkryst
 * --- Last Edit 23-Sep-2013
 */

public class InputHandler implements KeyListener {
   private boolean[] isPressed = new boolean[128];
   
   /**
    * Checks if the specified key is currently pressed.
    * @param x The keyEvent.getKeyCode() number of the key.
    * @return Return whether the specified key is pressed or not.
    */

   public boolean isKeyPressed(final int x) {
      return isPressed[x];
   }
   
   /**
    * Forces the specified key to be set as released.
    * @param x The keyEvent.getKeyCode() number of the key.
    */

   public void forceRelease(final int x) {
      if (x < isPressed.length)
         isPressed[x] = false;
   }

   /**
    * When a key is pressed then set that key as pressed.
    */

   public void keyPressed(final KeyEvent e) {
      final int KEY_CODE = e.getKeyCode();
      if (KEY_CODE < isPressed.length)
         isPressed[KEY_CODE] = true;
   }
   
   /**
    * When a key is released then set that key as released.
    */

   public void keyReleased(final KeyEvent e) {
      final int KEY_CODE = e.getKeyCode();
      if (KEY_CODE < isPressed.length)
         isPressed[KEY_CODE] = false;
   }

   public void keyTyped(final KeyEvent e) {
   }
}


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  
package valkryst.core.graphics;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import valkryst.core.Audio;
import valkryst.core.InputHandler;
import valkryst.systems.Message;

/**
 * @author Valkryst
 * --- Last Edit 07-Oct-2013
 */

public class Frame extends JFrame{
   private static final long serialVersionUID = -4612661261769049289L;
   
   public final static int WIDTH = 300;
   public final static int HEIGHT = 168; //
  public final static int SCALE = 3;
   public final static InputHandler INPUT = new InputHandler();
   
   public Frame(final Screen screen) {
      setResizable(true); //You can set this to true, the graphics scale up.
     setTitle("Temp");
      add(screen);
      pack();
      setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

      addWindowListener(new WindowAdapter() { // Runs before the game exits.
        public void windowClosing(WindowEvent e) {
            Audio.shutdown();
            System.exit(0);
         }
      });
     
      addKeyListener(INPUT);
      setLayout(new BorderLayout());
      add(screen, BorderLayout.CENTER);
      setLocationRelativeTo(null);
      setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
      setMinimumSize(new Dimension(WIDTH, HEIGHT));
      requestFocus();
      setFocusable(true);
     
      TextArea textArea = new TextArea();
      textArea.setEditable(false);
      textArea.setFocusable(false);
      textArea.setCursor(getCursor());
      textArea.setPreferredSize(new Dimension(WIDTH, HEIGHT + (HEIGHT/2)));
      textArea.setMinimumSize(new Dimension(50, 100));
      textArea.setMaximumSize(new Dimension(300, 600));
      textArea.setBackground(Color.WHITE);
      Message.setTextArea(textArea);
      add(textArea, BorderLayout.SOUTH);
     
      pack();
     
      setVisible(true);
     
      screen.start();
   }
}



Sorry for the terrible description of what's going on, I'll improve it when I get back from class. Thanks for any replies.
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.

Nickropheliac (14 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (48 views)
2014-08-09 21:09:32
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!