Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  Activating Keyboard Input  (Read 1595 times)
0 Members and 1 Guest are viewing this topic.
Offline Phoenix412

Senior Newbie





« Posted 2009-10-13 01:21:02 »

I'm developing a game within a Java application (using a Canvas class to draw).

The problem is, my arrow input from the keyboard is ignored until I click within the drawing
area. Is there a workaround to this? Thanks.

-Phoenix412
Offline Karmington

Senior Member


Medals: 1
Projects: 1


Co-op Freak


« Reply #1 - Posted 2009-10-13 01:51:12 »

i had this problem too, i think the trick was:

addListener
validate()

with possibly a requestFocus for good measure if necessary...
I'm not quite sure on what the best practice is, but validate seemed to be the magic word.

Offline Phoenix412

Senior Newbie





« Reply #2 - Posted 2009-10-13 02:24:45 »

Playing around with both right now but it doesn't appear to work. I think I
may be using it in the wrong place? Here's my Canvas constructor code, showing my implementation:

validate() and requestFocus() calls are made at the end of the canvas constructor

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  
CvWorld(OverworldFrame master)
   {  
      masterFrame = master;

      //Mouse movement listener
     addKeyListener(new KeyAdapter()
      { public void keyPressed(KeyEvent e)
         {
            int key = e.getKeyCode();
           
            if(key == KeyEvent.VK_UP)
               up = true;
            else if(key == KeyEvent.VK_DOWN)
               down = true;
            else if(key == KeyEvent.VK_RIGHT)
               right = true;
            else if(key == KeyEvent.VK_LEFT)
               left = true;

            try{

               }catch (Exception ex){}// why!??
        }
         public void keyReleased(KeyEvent e)
         {
            int key = e.getKeyCode();
           
            if(key == KeyEvent.VK_UP)
               up = false;
            else if(key == KeyEvent.VK_DOWN)
               down = false;
            else if(key == KeyEvent.VK_RIGHT)
               right = false;
            else if(key == KeyEvent.VK_LEFT)
               left = false;
         }
      });
     
      up = down = left = right = false;
      run_switch = false;
     
      logicalX = logicalY = 0;

                ...
     

      //initializes and starts Thread
     thr = new Thread(this);
       thr.start();
     
      validate();
      requestFocus();

   } // end constructor CvWorld


At first I used just validate, then I added requestFocus along with it.
Neither one appears to work.

This looks like what I need (according to the API), but I think
my usage isn't right.

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

Senior Member


Medals: 4



« Reply #3 - Posted 2009-10-14 04:07:56 »

I had a similar problem fixed by adding addFocusable(true), hope that helps.
Offline Phoenix412

Senior Newbie





« Reply #4 - Posted 2009-10-16 04:50:24 »

I had a similar problem fixed by adding addFocusable(true), hope that helps.

What class is that in? I couldn't find it for canvas and googling "addFocusable" only
gives a few results not related to Java...
Offline adon_y_coya

Senior Newbie





« Reply #5 - Posted 2009-10-16 07:38:18 »

zoto probably meant setFocusable(true). However, I think components are focusable by default.

I'd be surprised if the validate() would do the trick for a canvas, they're primarily intended for containers.

Also the requestFocus() probably won't work, as it's mostly intended for actionlistener components (text input, buttons, etc...).

Did you try to add your listeners to the owner frame (OverworldFrame master) instead of the canvas (CvWorld)?
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #6 - Posted 2009-10-16 16:27:24 »

requestFocus() really should do the trick. Just call it every single timestep for good measure.

See my work:
OTC Software
Offline adon_y_coya

Senior Newbie





« Reply #7 - Posted 2009-10-16 21:50:32 »

Well, he said that it didn't work, so...
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #8 - Posted 2009-10-16 22:13:18 »

Well, he said that it didn't work, so...
No, he did it once at the end of his constructor. Something he does later can easily take the focus away again. That's what I said all it every single timestep for good measure. That way, no matter what the user clicks on or what happens, you immediately pull focus back to the correct Component.

See my work:
OTC Software
Offline Abuse

JGO Coder


Medals: 10


falling into the abyss of reality


« Reply #9 - Posted 2009-10-16 22:55:22 »

RTFM  Grin

http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html

and specifically :-

http://java.sun.com/javase/7/docs/api/java/awt/Component.html#requestFocusInWindow()

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline adon_y_coya

Senior Newbie





« Reply #10 - Posted 2009-10-19 16:50:14 »

(For those that don't read the manuals)

Use requestFocusInWindow() instead of requestFocus().
"The focus behavior of this method can be implemented uniformly across platforms..." while the other's "...focus behavior is platform-dependent".

If it won't work in the constructor,
Quote from: Demonpants
...call it every single timestep for good measure.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #11 - Posted 2009-10-19 19:09:53 »

Also requestFocusInWindow() is meant to be called in the constructor, whereas requestFocus() isn't.

See my work:
OTC Software
Offline Phoenix412

Senior Newbie





« Reply #12 - Posted 2009-10-21 01:32:01 »

The combination of:

      setFocusable(true);
      validate();
      requestFocusInWindow();

called at the end of the constructor does the trick.
Thanks for the help!
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.

xsi3rr4x (75 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!