Keyboard Input: InputActionEvent.getTriggerCharacter() - Returning Blank

Hey Guys… Another problem at the moment:



public class WeaponryInput extends KeyInputAction {
   ...

   public void performAction(InputActionEvent iActionEvent) {
      
      Character c = new Character (iActionEvent.getTriggerCharacter());
      
      System.out.println("Weapon input handler received request from '" + c + "' key");
   }

   ...
}



Prints out:

Weapon input handler received request from '

Well, where did you register the action? Not all events have a character associated - it can be 0.

Ah, apologies… the forum seems to have truncated my message. It should have included the following code from a class that extends a ThirdPersonHandler:



        WeaponryInput weaponInputHandler = new WeaponryInput();
       
        //Add action for button "1", to select weapon 1
        this.addAction( weaponInputHandler, "Select Weapon: 1", KeyInput.KEY_1, false );
        //Add action for button "1", to select weapon 1
        this.addAction( weaponInputHandler, "Select Weapon: 2", KeyInput.KEY_2, false );



As you can see, I am assigning the 1 and 2 keys to the same instantiation of WeaponryInput. When these buttons are pressed, I get the empty print statement:

"Weapon input handler received request from '

The upper message seems to have been truncated too! The print statement should have a closing inverted comma too!



Strange bug in the forum software with two inverted commas next to each-other!

disso said:

Strange bug in the forum software with two inverted commas next to each-other!

My guess would be that it is caused by the 0 character :wink:

You should wrap your print statement with an

if ( iActionEvent.getTriggerPressed() && iActionEvent.getTriggerCharacter() != 0 )

I recently fought with the same code. I found out that every ON_PRESS Event gives you the proper character but every ON_RELEASE Event comes up with something useless (I guess its this 0 character).

Is there a special reason for that? It would be easy enough to distinguish between the press and release event by getTriggerPressed(), wouldn't it? And it would be useful to know which key has been released?!

Monky said:
I found out that every ON_PRESS Event gives you the proper character but every ON_RELEASE Event comes up with something useless (I guess its this 0 character).

Exactly. Pressing a key results in a character. Releasing does not. It's like this for most keyboard input events (awt, jinput, etc.) - it makes sense. If you need to distinguish the key you should use the key code - it is the same for pressed and released.

OK, not really understanding what is going on here… I've changed my code to:



   public void performAction(InputActionEvent iActionEvent) {
      if ( iActionEvent.getTriggerPressed() && iActionEvent.getTriggerCharacter() != 0 ) {
         Character c = new Character (iActionEvent.getTriggerCharacter());
         
         System.out.println("Weapon input handler received request from " + c + " key");
      } else {
         System.out.println("NOTHING");
      }
   }



And the console is giving me


NOTHING
NOTHING
NOTHING
NOTHING
NOTHING



When I repeatedly press the key I've assigned (numeric 1). Therefore, going by what is being said, you are implying that the ON_RELEASE event is being fired without the ON_PRESS event? Surely thats impossible?!?

I'm sure I'm being stupid here...

"Numeric 1" results in a character only if numlock is turned on…

Tried both with and without numlock on, still "NOTHING", even tried a different key for clarity:



        this.addAction( weaponInputHandler, "Select Weapon: 1", KeyInput.KEY_B, false );



Am I correct to be using a ThirdPersonHandler?

Thanks.

Have a look into TestInputHandler, please, to check what you are doing differently - I think it's working there what you are trying to achieve…

OK, found the problem… but don't understand why it is happening:


this.addAction( weaponInputHandler, InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_B, InputHandler.AXIS_NONE, false );



WORKS! But the following does not:

this.addAction( weaponInputHandler, InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_B, false );



So my question is, what does the "InputHandler.AXIS_NONE" parameter do?

Cheers!

Ah, I didn't see that in the first place. You are using an entirely different method by omitting that parameter!

The method you are using with

this.addAction( weaponInputHandler, InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_B, false );


looks like this:
    public void addAction( InputActionInterface inputAction, String triggerCommand, int keyCode, boolean allowRepeats ) {
while the 'working' method looks like this:
    public void addAction( InputActionInterface action, String deviceName, int button, int axis, boolean allowRepeats ) {
Have a look at the JavaDoc of those methods, as well. Only the second one triggers the action with device data.

The first method is using the old command system which does not pass characters at all. Maybe we should remove that old method or at least emphasize that there's a the different mechanism behind it...

Makes a lot more sense now… Thanks very much for your help! Your a STAR!!