com.jme.input
Class KeyBindingManager

java.lang.Object
  extended by com.jme.input.KeyBindingManager

public class KeyBindingManager
extends java.lang.Object

KeyBindingManager maintains a list of command and key pairs. A key is denoted as an int and corresponds to KeyInput's constants. A call to isValidCommand with an associated command will return true if they key associated with the command is pressed. This allows for pairings with InputAction how is also associated with a command, to be paired allowing for actions to be performed within the game based on user input.

Version:
$Id: KeyBindingManager.java 4406 2009-06-18 09:50:57Z noel.lynch $
Author:
Mark Powell
See Also:
KeyInput

Nested Class Summary
 class KeyBindingManager.KeyCodes
          KeyCodes defines a list of one or more keys for a given key command.
 
Method Summary
 void add(java.lang.String command, int keyCode)
          add adds a keycode to a command that already exists.
 void add(java.lang.String command, int[] keyCode)
          add adds a list of keycodes corresponding to a command.
 void applyQueuedKeyRestrictions()
           
 int[] get(java.lang.String command, int index)
          get retrieves the key(s) for a given command.
 java.util.Set<java.lang.Integer> getAlwaysRepeatableKeyCodes()
           
 java.util.Collection<java.lang.String> getCommands()
          Gets all commands currently registered to the key binding manager.
static KeyBindingManager getKeyBindingManager()
          getInstance gets the static singleton instance of the manager.
 java.util.ArrayList<KeyBindingManager.KeyCodes> getKeyCodesForCommand(java.lang.String command)
          Finds the associated key codes for this command.
 boolean isValidCommand(java.lang.String command)
          isValidCommand determines if a command is executable in the current state of the keyboard.
 boolean isValidCommand(java.lang.String command, boolean allowRepeats)
           
 boolean isValidCommand(java.lang.String command, boolean allowRepeats, boolean applyKeyRestricts)
          isValidCommand determines if a command is executable in the current state of the keyboard.
 void remove(java.lang.String command)
          remove deletes a key map from the list.
 void removeAll()
          removeAll deletes all key mappings from the list.
 void set(java.lang.String command, int keyCode)
          set sets the command to the given keycode overriding any previous keycodes previously set for the same command.
 void set(java.lang.String command, int[] keyCode)
          set sets the command to the given list of keycodes overriding any previous keycodes previously set for the same command.
 void setAlwaysRepeatableKeyCodes(java.util.Set<java.lang.Integer> alwaysRepeatableKeyCodes)
          The state of these keys is always determined by whether the key is now pressed, regardless of "repeatable" settings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

applyQueuedKeyRestrictions

public void applyQueuedKeyRestrictions()

setAlwaysRepeatableKeyCodes

public void setAlwaysRepeatableKeyCodes(java.util.Set<java.lang.Integer> alwaysRepeatableKeyCodes)
The state of these keys is always determined by whether the key is now pressed, regardless of "repeatable" settings.

To change the list, you must supply a new list here, since the default list is immutable. (You could copy the default list and then add or subtract, of course).


getAlwaysRepeatableKeyCodes

public java.util.Set<java.lang.Integer> getAlwaysRepeatableKeyCodes()
See Also:
#setAlwaysRepeatableKeyCodes(Set)

getCommands

public java.util.Collection<java.lang.String> getCommands()
Gets all commands currently registered to the key binding manager.

Returns:
the commands currently registered

getKeyCodesForCommand

public java.util.ArrayList<KeyBindingManager.KeyCodes> getKeyCodesForCommand(java.lang.String command)
Finds the associated key codes for this command.

Parameters:
command - the command to look up
Returns:
an array list of KeyCodes associated with this command

set

public void set(java.lang.String command,
                int keyCode)
set sets the command to the given keycode overriding any previous keycodes previously set for the same command.

Parameters:
command - the command to set.
keyCode - the key to set to the command.

set

public void set(java.lang.String command,
                int[] keyCode)
set sets the command to the given list of keycodes overriding any previous keycodes previously set for the same command.

Parameters:
command - the command to set.
keyCode - the list of keys to set to the command.

add

public void add(java.lang.String command,
                int keyCode)
add adds a keycode to a command that already exists. This will not override the previous keycode, but add to it. Allowing the two keys to perform the same command.

Parameters:
command - the command to add to.
keyCode - the key to add to the command.

add

public void add(java.lang.String command,
                int[] keyCode)
add adds a list of keycodes corresponding to a command. All the keys defined in the array must be pressed for the command to be valid.

Parameters:
command - the command to assign to the keys.
keyCode - the array of keys that must be pressed.

get

public int[] get(java.lang.String command,
                 int index)
get retrieves the key(s) for a given command. An array of ints are returned, where all ints would be required for the command to be executed. For example: int[] = {KEY_1, KEY_2} would require both 1 and 2 pressed at the same time.

Parameters:
command - the requested key map
Returns:
the key map for the command.

isValidCommand

public boolean isValidCommand(java.lang.String command)
isValidCommand determines if a command is executable in the current state of the keyboard. That is, is a valid key pressed to execute the requested command.

Parameters:
command - the command to check.
Returns:
true if the command should be executed, false otherwise.

isValidCommand

public boolean isValidCommand(java.lang.String command,
                              boolean allowRepeats)

isValidCommand

public boolean isValidCommand(java.lang.String command,
                              boolean allowRepeats,
                              boolean applyKeyRestricts)
isValidCommand determines if a command is executable in the current state of the keyboard. That is, is a valid key pressed to execute the requested command.

If both allowRepeats and applyKeyRestricts are false, then the caller must call .applyQueuedKeyRestrictions after an atomic set of inputs has been checked. This normally means that you need to call applyKeyRestricts() in your update function after all of your .isValidCommand() calls.

Parameters:
command - the command to check.
allowRepeats - allow repetitious key presses.
applyKeyRestricts - Only has effect if allowRepeats is false. The purpose of applyKeyRestricts of false is to allow you to check for the same key in multiple key bindings.
Returns:
true if the command should be executed, false otherwise.

remove

public void remove(java.lang.String command)
remove deletes a key map from the list.

Parameters:
command - the key map to delete.

removeAll

public void removeAll()
removeAll deletes all key mappings from the list.


getKeyBindingManager

public static KeyBindingManager getKeyBindingManager()
getInstance gets the static singleton instance of the manager.

Returns:
the instance of the key binding manager.