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 (601)
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  
  If You Can't Read Your Keyboard, Read This Topic  (Read 1766 times)
0 Members and 1 Guest are viewing this topic.
Offline fletchergames

Senior Devvie





« Posted 2009-02-11 18:36:06 »

I'm new at JInput and just got it figured out for keyboard input.  The doesn't seem to be alot of documentation about this.  There's tidbits here and there, but there doesn't seem to be one tutorial that spells everything out.

For this reason, I'm posting this topic about some of the pitfalls.  I haven't figure out mouse and gamepad input yet, but some of these pitfalls should apply to them too.

When you download jinput_combined_dist_latest.zip from https://jinput.dev.java.net/ , you must extract all the files into your classpath somehow.  You always need jinput.jar, and you'll also need one or more binary files depending upon your OS.  It's .dll for Windows, .so for Linux, and .jnilib for Mac OS X.  This part isn't too hard to figure out.  If you can get the test programs to work, you should be able to get your own code to work too.

The first thing your program must do is read all the controllers.  That only requires the following two lines of code:

1  
2  
3  
ControllerEnvironment controllerEnvironment =
   ControllerEnvironment.getDefaultEnvironment();
Controller[] aController = controllerEnvironment.getControllers();


That isn't hard, but now you need to do something with those controllers.  I propose that you split them up by type.  You need to store all the controllers of each type so that you can poll them all, not just the first one.  Some people store just one controller, and it might be the wrong one.  This is probably most often the case when people have all kinds of weird pointer devices that they don't actually use.

Here's my code for separating the controllers:

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  
public class ControllerManager {
/**Constructor.
 */

public ControllerManager() {
   //find all the controllers
   ControllerEnvironment controllerEnvironment =
      ControllerEnvironment.getDefaultEnvironment();
   Controller[] aController = controllerEnvironment.getControllers();
   
   //separate the controllers into groups
   int numControllers = aController.length;
   for(int iController = 0; iController < numControllers; iController++) {
      Controller controller = aController[iController];
     
      //figure out which group to put the controller into
      Controller.Type controllerType = controller.getType();
      if(controllerType == Controller.Type.KEYBOARD)
         aKeyboard.add((Keyboard)controller);
      else if(controllerType == Controller.Type.MOUSE)
         aMouse.add((Mouse)controller);
      else if(controllerType == Controller.Type.TRACKBALL ||
         controllerType == Controller.Type.TRACKPAD)
      {
         if(controller instanceof Mouse)
            aMouse.add((Mouse)controller);
      } //end if the controller is a trackball or trackpad
      else if(controllerType == Controller.Type.GAMEPAD)
         aGamepad.add(controller);
   } //end for each controller
   
   //list the types of controllers found
   int numKeyboards = aKeyboard.size();
   int numMice = aMouse.size();
   int numGamepads = aGamepad.size();
   System.err.println("Controllers Found" +
      "\nGamepads: " + numGamepads +
      "\nKeyboards:" + numKeyboards +
      "\nMice/trackballs/trackpads: " + numMice +
      "\nUnknown: " + (numControllers - (numGamepads + numKeyboards + numMice)));
} //end constructor

//PRIVATE VARIABLES//////////////////////////////////////////////////////////////////////
//controllers
private final MutableArray<Controller> aGamepad = new MutableArray<Controller>();
private final MutableArray<Keyboard> aKeyboard = new MutableArray<Keyboard>();
private final MutableArray<Mouse> aMouse = new MutableArray<Mouse>();
} //end class ControllerManager


MutableArray is just my version of ArrayList.  The only other weirdness is that I just dumped trackballs and trackpads in with mice.  I'm not sure how this works out, but I imagine that's what should be done with them.

Now, you need some way to read the controllers.  Since I've only dealt with keyboards so far, that's all we're going to look at.  The straightforward way to check all your keyboards is the following:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
/*WARNING: THIS CODE WILL NOT WORK*/
Component.Identifier.Key key = /*whatever key you want to poll*/;
int numKeyboards = aKeyboard.size();
for(int iKeyboard = 0; iKeyboard < numKeyboards; iKeyboard++) {
   Keyboard keyboard = aKeyboard.get(iKeyboard);
   if(keyboard.isKeyDown(key)) {
      isPressedNow = true;
      break;
   }
} //end for each keyboard


This will not work.  It will never detect key presses.  This is because you have to call the poll method to make the keyboard check which keys are pressed.  The correct code is listed below:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
Component.Identifier.Key key = /*whatever key you want to poll*/;
int numKeyboards = aKeyboard.size();
for(int iKeyboard = 0; iKeyboard < numKeyboards; iKeyboard++) {
   Keyboard keyboard = aKeyboard.get(iKeyboard);
   keyboard.poll();
   if(keyboard.isKeyDown(key)) {
      isPressedNow = true;
      break;
   }
} //end for each keyboard


There is still one minor issue.  Since the controllers are only detected during the constructor, any devices plugged in after the constructor is called won't be detected.  The inverse is not a problem; if a controller is unplugged after the constructor is called, it won't hurt anything.  (I tested this by unplugging a USB keyboard from my laptop while my program was running.)  You could just detect all the controllers every time you poll for input, but detecting controllers takes close to 2 seconds on my computer.  Clearly, that's too long.

I'm not going to say anything about what you do with key presses when they happen.  That's covered pretty well in Developing Games in Java, even though that book doesn't use JInput.  I just posted this topic so that anyone who had a problem with JInput would be able to easily find some kind of solution.

If anyone else wants to post any solutions to beginner problems with reading keyboard input using JInput, this would probably be a good thread to post it in.
Offline endolf

JGO Coder


Medals: 7
Exp: 15 years


Current project release date: sometime in 3003


« Reply #1 - Posted 2009-02-12 07:23:20 »

Hi

The more information user have of jinput the better. Everything you mentioned so far is in the getting started thread, so it must not be clear there. Do you have any suggestions on what needs to be rewritten in a clearer way?.

Using isKeyDown for every key someone is using (especially if they are detecting typing for usernames/text) might get a bit tedious. As of version 2 of JInput you can get events from the device instead of checking each key in turn. Hopefully this post explains it Smiley

HTH

Endolf

Offline fletchergames

Senior Devvie





« Reply #2 - Posted 2009-02-16 18:52:59 »

My problem was that I skimmed the two intro/introduction threads and then stopped reading before the getting started thread.  The information I needed was there, but I read the information around it instead of what I needed.  I would almost say it would better to have less threads stickied, but the real issue is that I didn't read enough of them.

None of them jumped out at me as being what I needed, despite the fact that the relevant thread was called "Getting started with JInput".  I parsed that title as being another introduction thread and assumed it was about setting up the .dlls and so forth, which I had no problem with.

So I don't think anything needs to be changed unless there's some way the getting started thread can be made to seem more important.  Maybe the Getting Started link on https://jinput.dev.java.net/ should link directly to that thread instead of just to this forum.  It looks like it's supposed to, but there seems to be some kind of error.
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 (29 views)
2014-12-15 09:26:44

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

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

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

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

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

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

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

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

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!