Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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  
  How to use keyboard  (Read 4905 times)
0 Members and 1 Guest are viewing this topic.
Offline zeni

Senior Newbie





« Posted 2009-05-27 17:32:00 »

How to use keyboard + JInput ?

i did'nt found it on google =9

Examples are welcome. =p
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2009-05-27 19:02:01 »

Here's random parts of an InputManager class I made.

First you need to find the keyboard and store it for future use.
1  
2  
3  
4  
5  
6  
7  
private static Keyboard findKeyboard()
{
   for (int i = 0; i < controllers.length; i++)
      if (controllers[i].getType() == Controller.Type.KEYBOARD)
         return (Keyboard)controllers[i];
   return null;
}


In an initializer somewhere...
1  
keyboard = findKeyboard();


Then every timestep you want to poll the keyboard. This simply goes through all the buttons to find what has been pressed, putting it all in a giant boolean list.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public static boolean[] pollKeyboard()
{
   keyboard.poll();
   Component[] c = keyboard.getComponents();
   boolean[] b = new boolean[c.length];
   for (int i = 0; i < c.length; i++)
   {
      if (c[i].getPollData() != 0)
         b[i] = true;
      else
         b[i] = false;
   }
   
   return b;
}


To use the above, you'd do this:
1  
2  
3  
4  
if (pollKeyboard()[net.java.games.input.Component.Identifier.Key.A])
{
   doA();
}


Or if you just want to avoid a boolean list (better depending on what you want to do) you can do this:
1  
2  
3  
4  
if ((keyboard.isKeyDown(net.java.games.input.Component.Identifier.Key.A))
{
   doA();
}

See my work:
OTC Software
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #2 - Posted 2009-05-28 06:09:56 »

Hi

Checking every key is a bit over kill, for simple WASD type keyboard control it's fine, but if you want to capture typing in a dialog box for example, it's alot more effort than would be nice. There is the event model also. Instead of asking 120 times if a particular key has been pressed, you just ask the device for the events since the last poll.

Check the getting started thread, and for events see this post

HTH

Endolf

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

Senior Newbie





« Reply #3 - Posted 2009-05-29 03:21:37 »

I tryed do it. But isn't work =(

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  
package net.java.games.input.test;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Keyboard;

public class ControllerManager {
   private static Keyboard keyboard = findKeyboard();
   
   private static Keyboard findKeyboard()
   {
      ControllerEnvironment controllerEnvironment =ControllerEnvironment.getDefaultEnvironment();
      Controller[] aController = controllerEnvironment.getControllers();
      for (int i = 0; i < aController.length; i++)
         if (aController[i].getType() == Controller.Type.KEYBOARD)
            return (Keyboard)aController[i];
      return null;
   }
   
   
public static void main(String[] args) {
   

   while(true){
     
      if (keyboard.isKeyDown(net.java.games.input.Component.Identifier.Key.A)){
               System.out.println("I'm pressed");
            }
   
   }
   
   
}

}
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #4 - Posted 2009-05-29 06:37:56 »

Hi

On a linux box here when I run that code I get a null pointer because it found no keyboard. What os are you using?, does the applet demo from here work?

Cheers

Endolf

Offline zeni

Senior Newbie





« Reply #5 - Posted 2009-05-29 17:13:00 »

I using Linux too, but i set this "-Djinput.plugins=net.java.games.input.AWTEnvironmentPlugin" in my VM.
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #6 - Posted 2009-05-29 21:44:29 »

How are you launching this?, from an IDE?, or from the command line?

Endolf

Offline zeni

Senior Newbie





« Reply #7 - Posted 2009-06-01 16:19:07 »

From eclipse IDE. "VM arguments"
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #8 - Posted 2009-06-01 16:44:08 »

This post suggests you got something working correctly. It's not finding anything that admits to being a keyboard. do the controller text/read tests work?

Endolf

Offline zeni

Senior Newbie





« Reply #9 - Posted 2009-06-01 17:37:51 »

no, i only detected the keyboard. And I tryed do this

 
I tryed do it. But isn't work =(

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  
package net.java.games.input.test;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Keyboard;

public class ControllerManager {
   private static Keyboard keyboard = findKeyboard();
   
   private static Keyboard findKeyboard()
   {
      ControllerEnvironment controllerEnvironment =ControllerEnvironment.getDefaultEnvironment();
      Controller[] aController = controllerEnvironment.getControllers();
      for (int i = 0; i < aController.length; i++)
         if (aController[i].getType() == Controller.Type.KEYBOARD)
            return (Keyboard)aController[i];
      return null;
   }
   
   
public static void main(String[] args) {
   

   while(true){
     
      if (keyboard.isKeyDown(net.java.games.input.Component.Identifier.Key.A)){
               System.out.println("I'm pressed");
            }
   
   }
   
   
}

}


but did'nt work.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trigger

Innocent Bystander





« Reply #10 - Posted 2009-09-02 19:42:30 »

hi zeni,

first of all you forgot to poll the keyboard with keyboard.poll() at the beginning of the while loop. in addition this wont work on my windows vista as well. i tryed to start it from inside eclipse as well as from the command line but it didnt work. as soon as i use the same code in an application that uses a JFrame it starts to work tho. any ideas why it acts like that? (just a wild guess: probably DInput needs a window that implements a callback for the keyboard handling?)

best regards
johannes diemke
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #11 - Posted 2009-09-02 21:40:22 »

Hi

Instead of calling findKeyboard in a static initialiser, try calling it from the constructor of ControllerManager.

JInput needs to do some static initialisation of it's own, my guess is that by calling yours early on, it's getting confused.

HTH

Endolf

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.

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

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

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

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

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

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

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

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
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!