The data type used for identifying key/butt press

Hey. I know you guys are working on a user’s manual but I’ve got to ask this question to help me with my development with my current project.

What data type do you use to identify what keyboard button, mouse button, or joystick button was pressed?

I saw in Axis.Identifier there is a method called getName() and that returns a String.

I am guessing that all keyboard, mouse, and joystick buttons inheret from this class. So would I be safe to use Strings for holding keyboard/mouse/joystick buttons to determine what action needs to associate with these buttons or can I get that information another way, such as the use of ints?

Also, what is the performance differnce from making a buch of string comparisons as apposed to integer comparisions?

Thanks

String comaprison is a lot more CPU. Whether it makes a difference in your app is a toss up really as you are talking “not much” comapred to “almost none.”

Still I’d avoid it in general.

An Axis object represents one value be it a button, one axis of a stick, or what have you. I’d base the mapping on Axis object to what it controls, myself.

So say when ‘a’ is pressed on the keyboard you want an action to be performed.

How do u check that ‘a’ was pressed?

In other libraries such as JavaJoystick, you get a unique ID for each button. I would just make an integer comparision to see if key ‘a’ (which has a unique number) is equal to the number that the button returned.

Can u do that in JInput?
Jeff: you stated that you check the refernce of the Axis object. Well, how do u know that the object in which you are checking the refernce from points to the button ‘a’?

Thanks

Hi
Does this mean your keyboard test program works now?, just checking, because if it does, then I’ll look at something else tonight.

Cheers

Endolf

nah, the keyboard test doesn’t work. I’m trying to code up my application so that when jinput does work for me, I will be able to plugin jinput stuff.

Some sourcecode from our game which will be use JInput. We use this class for general checking of Axis instances against a specific value. We call this the extend value. not pressed value because some axis can have two such values (eg. gamepad).


private class AxisInput implements PollingInput {
        private Axis axis;
        private boolean isTriggered;
        private float extendValue;
        private boolean override;
        private float deadZone;
        
        AxisInput(Axis newAxis, float newExtendValue) {
            axis = newAxis;
            deadZone = axis.getDeadZone() + 0.1f;
            
            extendValue = newExtendValue;
            
            // this adds this Input object to a List<PollingInput> were
           // it is polled at certain times
            pollingInputs.add(this);
        }
        
        public boolean isTriggered() {
            return isTriggered;
        }
        
        public void reset() {
            // this is a feature of our Input objects: you can reset them
            // to force the user to press the again to get it recognized
            override = true;
            
            isTriggered = false;
        }
        
        public void poll() {
            // this updates the triggered state
            boolean newTriggered = Math.abs(axis.getPollData()-extendValue) <= deadZone;
            
            // handles proper resetting
            if(override) {
                if(!newTriggered) {
                    override = false;
                }
            } else {
                isTriggered = newTriggered;
            }
        }
    }

Such an AxisInput is created like this: new AxisInput(xAxis, -1).
This delivers true on isTriggered() whenever one presses to the left on my gamepad.

The hard thing is how do I find my xAxis object? The code above is somewhat silly because it is hardcoded to my gamepad only. I see no problems when I the user has to interact and press something and the game simply polls every available axis object to find out which one has been used.
Now imagine I want to save the input configuration to disk and use it when the app is restarted.

What information should I write into a file?

Thats exactly what I was trying to get at.

Like how do u know ‘a’ was pressed or game controller button1 was pressed.

AH this is a real issue. Somethign I’ve doena bit of digging on but am stalled ebcause of other thinsg right now.

Initially it was thought that there shoulf be standard Axis names.

On windows the name of the ‘a’ button is “A”.

Unfrotunately this doesnt work right yet on mac which returns the truely helpful “buttonXXX” or something approaching that.

We’re still tryign tod ecide the rigth way to handle that.
I lean towards converting the names to standard in the plug-in myself but we’re trying to get a sensible answer otu of Apple as to what the “right” way is on their platform even in native code to recognize this.

Hi
One way of doing this is to get the button names using some kind of configurator dialog, ‘Please press the button you wish to use for ‘forwards’ (We recomend the ‘A’ key)’, or something like that, it’s not ideal, but it should get you going till we figure something better out in the plugins layer.

Cheers

Endolf

Isn’t this an even more scary issues since drivers on Windows seems to be able to be able to define their own button names (at least thats the way it appeared when I tried this stuff out).

So, although you could map some commonly expected keys to some standardised values in the plugin, you’d still have random names for the buttons on a controller…

Unless of course thats fine, expected keys can be mapped using some HardwareToJava(int Code) mapping function on the plugin and anything else just returns unknown, leaving the developer to deal with it as Endolf suggests above.

Kev

So the way JavaJoystick handles naming is that it detects the joystick buttons and assings them an id. The actual name isn’t important.

Then you can check if button 1 is held down via a mask.

For instance say you have detected a Joystick j

you call a method


int curButtonsPressed = j.getButtons();

curButtonsPressed is a button mask. To find out if say BUTTON6 was pressed do this:


if(j.Button6 & curButtonsPressed) > 0){
  //button 6 was pressed
}

I really like this because its very straight forward and easy to use.
I still have no idea how you detect when specific buttons are being pressed in jinput.

Maybe you guys should look at JavaJoystick. I know they don’t have all the featurs you have like forcefeed back and it only works for windows and linux, but I was able to use their stuff without much trouble at all.

The problem is that just means that the mappings have been hardcoded in the LWJGL joystick code.

As mentioned before we could do that with the names in our plug-ins but if the underlying system driver decided to do somethign different it would fail invisibly :frowning:

We are tryign to fidn otu what the "right way’ is from the gusy who make the OS in the cas eof OSX and i wouldn’t want to hack sucha hardcoded solution untilw e have an answer.

So basically, u are saying not to use JINPUT until u guys get this figured out?

[quote]So basically, u are saying not to use JINPUT until u guys get this figured out?
[/quote]
Nope.

I am saying either build the translation table yourself or let your user do it through key assignment at start up.

OR if you vcan’t wait fro someoen else to get to it, do the reasearch and propsoe a proper fix yourself. Thats the great thing abotu open source. If you don’t like the way it is, you can change it :slight_smile:

Roger that.
Maybe I’ll write a class that handles this functionality that way it won’t disturbe the core arch.

So I checked-out jinput and am unable to get any information back from my controllers, keyboards, and mice.

I ran readtest and only 1 window opened up. Nothing was added to that window either (it was a blank JPanel (Frame or whatever)).

Here is the output from the command line


readtest:
     [java] Scanning jar: linux.jar
     [java] Examining file : META-INF/
     [java] Examining file : META-INF/MANIFEST.MF
     [java] Examining file : net/
     [java] Examining file : net/java/
     [java] Examining file : net/java/games/
     [java] Examining file : net/java/games/input/
     [java] Examining file : net/java/games/input/LinuxAxis.class
     [java] Examining file : net/java/games/input/LinuxDevice$LinuxHat.class
     [java] Examining file : net/java/games/input/LinuxDevice$ButtonID.class
     [java] Examining file : net/java/games/input/LinuxDevice$1.class
     [java] Examining file : net/java/games/input/LinuxDevice.class
     [java] Examining file : net/java/games/input/LinuxEnvironmentPlugin.class
     [java] Found candidate class: net/java/games/input/LinuxEnvironmentPlugin.class
     [java] Adding class to plugins:net.java.games.input.LinuxEnvironmentPlugin
     [java] Examining file : net/java/games/input/LinuxKeyboard$KeyID.class
     [java] Examining file : net/java/games/input/LinuxKeyboard.class
     [java] Examining file : net/java/games/input/LinuxMouse$LinuxMouseBall.class
     [java] Examining file : net/java/games/input/LinuxMouse$LinuxMouseButtons.class
     [java] Examining file : net/java/games/input/LinuxMouse$LinuxMouseButton.class
     [java] Examining file : net/java/games/input/LinuxMouse.class
     [java] Examining file : net/java/games/input/LinuxNativeTypesMap.class
     [java] Examining file : net/java/games/input/NativeDefinitions.class

thanks

Hi
That sounds (and looks) very much like it found the linux plugin, but no devices. Does non CVS code find everything OK?

Cheers

Endolf

So if I’m following this correctly…

Axis name - is what you display to the user (it may be translated to a users locale), but the Axis ‘Identifier’ name is a constant for known axis types (e.g. uppercase letter for most keyboard keys, some definition to come for gamepad buttons, etc.)

We need a standard convention for naming Identifiers for the case of gamepad buttons and sticks, where there could be two sticks (left and right) or a single stick. E.g. do we call it ‘left stick’ if there is only one? How do we code a game that wants a stick control if that could be named ‘left stick’ or ‘stick 1’ or whatever…

This needs a little design time… so that the code that the game programmers are writing is as straight-forward as possible.

Even if we need a bunch of helper methods to go with JInput for things like ‘get directional controllers’, ‘get default directional controller’, maybe a prefs system for things like 'default fire/attack button, ‘default jump button’, ‘default use/action button’ (similar for ‘select’ button, ‘start’ button, etc…)

Hi
Did you ever wonder why most games default to the first two axis and first 4+ buttons on the first controller it finds. It’s because they can be called anything they like (on windows, not under linux, dunno about mac). It really is up to the driver writers and there is nothing we can do about that one.
Helper methods could be done, and i’ve been pondering a ‘detect which axis has just been moved and in what direction’ and ‘which button has just been pressed’ helper for game configs, the defaults just have to be ‘get the first 4 buttons from a game controller’ or ‘get the first 3 axis from a game controller’ (you can get the controller type, and this should be set). Of course this goes a little pear shapped under linux, when the axis names aren’t applied right in the kernel, for example, my analog 3 axis 4 button gameport style joystick has listed a Z axis instead of a throttle, nowt can be done about it in jinput as it’s a kernel level problem (don’t have any other gameports to try any other joysticks). I think for my own purposes I’ll be using the device name, and then just Axis 1, Axis 2, button 1, button 2 naming, overiding what jinput thinks as it’s just too much out of our hands.

HTH

Endolf

Nope, it does the same thing.

This may have been asked already but what version of Linux are you using? JInput only supports fairly recent kernels.