public class SquidInput
extends com.badlogic.gdx.InputAdapter
SquidInput.KeyHandler.handle(char, boolean, boolean, boolean)
as chars like DOWN_ARROW or its value, '↓'. Shift
modifies the input as it would on a QWERTY keyboard, and the exact mapping is documented in
fromCode(int, boolean)
as well. This class handles mouse input immediately, but stores keypresses in a
queue, storing all key events and allowing them to be processed one at a time using next()
or all at once
using drain()
. To have an effect, it needs to be registered by calling
Input.setInputProcessor(InputProcessor)
. Note that calling hasNext()
does more than just check if
there are events that can be processed; because hasNext() is expected to be called frequently, it is also the point
where this class checks if a key is being held and so the next event should occur. Holding a key only causes the
keyDown() method of InputListener to be called once, so this uses hasNext() to see if there should be a next event
coming from a held key.
combineModifiers(char, boolean, boolean, boolean)
, while the unmap() methods allow removal of
any no-longer-wanted remappings.
next()
. Mouse inputs do not affect hasNext(), and next() will process only key pressed events. Also, see
above about the extra behavior of hasNext regarding held keys.Modifier and Type | Class and Description |
---|---|
static interface |
SquidInput.KeyHandler
A single-method interface used to process "typed" characters, special characters produced by unusual keys, and
modifiers that can affect them.
|
Modifier and Type | Field and Description |
---|---|
static char |
BACKSPACE
Backspace key on most PC keyboards; Delete key on Mac keyboards.
|
static char |
CENTER_ARROW
Not typically a dedicated key, but if numpadDirections is enabled, this will be sent by Numpad 5.
|
static char |
DOWN_ARROW
Down arrow key.
|
static char |
DOWN_LEFT_ARROW
Not typically a dedicated key, but if numpadDirections is enabled, this will be sent by Numpad 1.
|
static char |
DOWN_RIGHT_ARROW
Not typically a dedicated key, but if numpadDirections is enabled, this will be sent by Numpad 3.
|
static char |
END
End key (commonly used for moving a cursor to end of line).
|
static char |
ENTER
Enter key, also called Return key.
|
static char |
ESCAPE
Esc or Escape key
|
static char |
F1
Function key F1
|
static char |
F10
Function key F10
|
static char |
F11
Function key F11
|
static char |
F12
Function key F12
|
static char |
F2
Function key F2
|
static char |
F3
Function key F3
|
static char |
F4
Function key F4
|
static char |
F5
Function key F5
|
static char |
F6
Function key F6
|
static char |
F7
Function key F7
|
static char |
F8
Function key F8
|
static char |
F9
Function key F9
|
static char |
FORWARD_DELETE
Delete key on most PC keyboards; no equivalent on some (all?) Mac keyboards.
|
static char |
GAMEPAD_A
Gamepad A button.
|
static char |
GAMEPAD_B
Gamepad B button.
|
static char |
GAMEPAD_C
Gamepad C button.
|
static char |
GAMEPAD_L1
Gamepad L1 button.
|
static char |
GAMEPAD_L2
Gamepad L2 button.
|
static char |
GAMEPAD_LEFT_THUMB
Gamepad Left Thumb button.
|
static char |
GAMEPAD_R1
Gamepad R1 button.
|
static char |
GAMEPAD_R2
Gamepad R2 button.
|
static char |
GAMEPAD_RIGHT_THUMB
Gamepad Right Thumb button.
|
static char |
GAMEPAD_SELECT
Gamepad Select button.
|
static char |
GAMEPAD_START
Gamepad Start button.
|
static char |
GAMEPAD_X
Gamepad X button.
|
static char |
GAMEPAD_Y
Gamepad Y button.
|
static char |
GAMEPAD_Z
Gamepad Z button.
|
protected squidpony.squidmath.IntIntOrderedMap |
heldCodes |
static char |
HOME
Home key (commonly used for moving a cursor to start of line).
|
protected boolean |
ignoreInput |
static char |
INSERT
Insert key.
|
protected SquidInput.KeyHandler |
keyAction |
protected long |
lastKeyTime |
static char |
LEFT_ARROW
Left arrow key.
|
com.badlogic.gdx.utils.IntIntMap |
mapping |
protected SquidMouse |
mouse |
protected boolean |
numpadDirections |
static char |
PAGE_DOWN
Page Down key.
|
static char |
PAGE_UP
Page Up key.
|
protected squidpony.squidmath.IntVLA |
queue |
protected long |
repeatGapMillis |
static char |
RIGHT_ARROW
Down arrow key.
|
static char |
TAB
Tab key.
|
static char |
UP_ARROW
Up arrow key.
|
static char |
UP_LEFT_ARROW
Not typically a dedicated key, but if numpadDirections is enabled, this will be sent by Numpad 7.
|
static char |
UP_RIGHT_ARROW
Not typically a dedicated key, but if numpadDirections is enabled, this will be sent by Numpad 9.
|
static char |
VERTICAL_ARROW
Not typically a dedicated key, but if numpadDirections is enabled, this will be sent by Numpad 0.
|
Constructor and Description |
---|
SquidInput()
Constructs a new SquidInput that does not respond to keyboard or mouse input.
|
SquidInput(SquidInput.KeyHandler keyHandler)
Constructs a new SquidInput that does not respond to mouse input, but does take keyboard input and sends keyboard
events through some processing before calling keyHandler.handle() on keypresses that can sensibly be processed.
|
SquidInput(SquidInput.KeyHandler keyHandler,
boolean ignoreInput)
Constructs a new SquidInput that does not respond to mouse input, but does take keyboard input and sends keyboard
events through some processing before calling keyHandler.handle() on keypresses that can sensibly be processed.
|
SquidInput(SquidInput.KeyHandler keyHandler,
SquidMouse mouse)
Constructs a new SquidInput that responds to mouse and keyboard input when given a SquidMouse and a
SquidInput.KeyHandler implementation.
|
SquidInput(SquidInput.KeyHandler keyHandler,
SquidMouse mouse,
boolean ignoreInput)
Constructs a new SquidInput that responds to mouse and keyboard input when given a SquidMouse and a
SquidInput.KeyHandler implementation, and can be put in an initial state where it ignores input until told
otherwise via setIgnoreInput(boolean).
|
SquidInput(SquidMouse mouse)
Constructs a new SquidInput that does not respond to keyboard input, but does take mouse input and passes mouse
events along to the given SquidMouse.
|
Modifier and Type | Method and Description |
---|---|
SquidInput |
clearMapping()
Removes any remappings to key bindings that were in use in this SquidInput.
|
static int |
combineModifiers(char key,
boolean alt,
boolean ctrl,
boolean shift)
Combines the key (as it would be given to
SquidInput.KeyHandler.handle(char, boolean, boolean, boolean) ) with the
three booleans for the alt, ctrl, and shift modifier keys, returning an int that can be used with the internal
queue of ints or the public mapping of received inputs to actual inputs the program can process. |
void |
drain()
Processes all events queued up, passing them through this object's key processing and then to keyHandler.
|
void |
flush()
Empties the backing queue of data.
|
char |
fromCode(int keycode,
boolean shift)
Maps keycodes to unicode chars, sometimes depending on whether the Shift key is held.
|
boolean |
getIgnoreInput()
Get the status for whether this should ignore input right now or not.
|
SquidInput.KeyHandler |
getKeyHandler() |
SquidMouse |
getMouse() |
long |
getRepeatGap()
Gets the amount of milliseconds of holding a key this requires to count as a key repeat.
|
boolean |
hasNext()
Returns true if at least one event is queued, but also will call
keyDown(int) if a key is being held but
there is a failure to process the repeated event. |
boolean |
isUsingNumpadDirections() |
boolean |
keyDown(int keycode) |
SquidInput |
keyMappingFromString(java.lang.String keymap)
Reads in a String (almost certainly produced by
keyMappingToString() ) to set the current key remapping. |
java.lang.String |
keyMappingToString()
Gets the current key remapping as a String, which can be saved in a file and read back with
keyMappingFromString(String) . |
boolean |
keyTyped(char character) |
boolean |
keyUp(int keycode) |
boolean |
mouseMoved(int screenX,
int screenY) |
void |
next()
Processes the first key event queued up, passing it to this object's InputProcessor.
|
SquidInput |
remap(char pressedChar,
boolean pressedAlt,
boolean pressedCtrl,
boolean pressedShift,
char targetChar,
boolean targetAlt,
boolean targetCtrl,
boolean targetShift)
Remaps a char that could be input and processed by
SquidInput.KeyHandler.handle(char, boolean, boolean, boolean)
(possibly with some pressed modifiers) to another char with possible modifiers. |
SquidInput |
remap(int[] pairs)
Remaps many keypress combinations, each of which is a char and several potential modifiers, to other keypress
combinations.
|
SquidInput |
remap(int pressed,
int target)
Remaps a keypress combination, which is a char and several potential modifiers, to another keypress combination.
|
boolean |
scrolled(float amountX,
float amountY) |
void |
setIgnoreInput(boolean ignoreInput)
Set the status for whether this should ignore input right now or not.
|
void |
setKeyHandler(SquidInput.KeyHandler keyHandler) |
void |
setMouse(SquidMouse mouse) |
void |
setRepeatGap(long time)
Sets the amount of milliseconds of holding a key this requires to count as a key repeat.
|
void |
setUsingNumpadDirections(boolean using) |
boolean |
touchDown(int screenX,
int screenY,
int pointer,
int button) |
boolean |
touchDragged(int screenX,
int screenY,
int pointer) |
boolean |
touchUp(int screenX,
int screenY,
int pointer,
int button) |
SquidInput |
unmap(char pressedChar,
boolean pressedAlt,
boolean pressedCtrl,
boolean pressedShift)
Removes a keypress combination from the mapping by specifying the char and any modifiers that are part of the
keypress combination.
|
SquidInput |
unmap(int pressed)
Removes a keypress combination from the mapping by specifying the keypress combination as an int.
|
protected SquidInput.KeyHandler keyAction
protected boolean numpadDirections
protected boolean ignoreInput
protected SquidMouse mouse
protected final squidpony.squidmath.IntVLA queue
protected long lastKeyTime
protected squidpony.squidmath.IntIntOrderedMap heldCodes
protected long repeatGapMillis
public final com.badlogic.gdx.utils.IntIntMap mapping
public static final char LEFT_ARROW
public static final char UP_ARROW
public static final char RIGHT_ARROW
public static final char DOWN_ARROW
public static final char DOWN_LEFT_ARROW
public static final char DOWN_RIGHT_ARROW
public static final char UP_RIGHT_ARROW
public static final char UP_LEFT_ARROW
public static final char CENTER_ARROW
public static final char VERTICAL_ARROW
public static final char ENTER
public static final char TAB
public static final char BACKSPACE
public static final char FORWARD_DELETE
public static final char INSERT
public static final char PAGE_DOWN
public static final char PAGE_UP
public static final char HOME
public static final char END
public static final char ESCAPE
public static final char F1
public static final char F2
public static final char F3
public static final char F4
public static final char F5
public static final char F6
public static final char F7
public static final char F8
public static final char F9
public static final char F10
public static final char F11
public static final char F12
public static final char GAMEPAD_A
public static final char GAMEPAD_B
public static final char GAMEPAD_C
public static final char GAMEPAD_X
public static final char GAMEPAD_Y
public static final char GAMEPAD_Z
public static final char GAMEPAD_L1
public static final char GAMEPAD_L2
public static final char GAMEPAD_R1
public static final char GAMEPAD_R2
public static final char GAMEPAD_LEFT_THUMB
public static final char GAMEPAD_RIGHT_THUMB
public static final char GAMEPAD_START
public static final char GAMEPAD_SELECT
public SquidInput()
ApplicationListener.create()
method has started, and
cannot be called in an ApplicationListener's constructor or in initialization at the class level. This is because
all constructors attempt to load a file, if it exists, with Gdx.files.local("keymap.preferences")
, and
Gdx.files
is only available starting in create(). This file, keymap.preferences, is meant to contain any
key remappings specified by the user, and its contents should be produced by keyMappingToString()
when
any key mappings change and are saved. You can use your own filename, but keymap.preferences is the standard one
and these can be exchanged between games (so common remappings for users of DVORAK or AZERTY keyboards can be
sent between users even if they play different games). If you really want a custom filename, or to load the
keymap from a larger user profile, you can give the contents to keyMappingFromString(String)
on a
SquidInput after construction.public SquidInput(SquidMouse mouse)
ApplicationListener.create()
method has started, and
cannot be called in an ApplicationListener's constructor or in initialization at the class level. This is because
all constructors attempt to load a file, if it exists, with Gdx.files.local("keymap.preferences")
, and
Gdx.files
is only available starting in create(). This file, keymap.preferences, is meant to contain any
key remappings specified by the user, and its contents should be produced by keyMappingToString()
when
any key mappings change and are saved. You can use your own filename, but keymap.preferences is the standard one
and these can be exchanged between games (so common remappings for users of DVORAK or AZERTY keyboards can be
sent between users even if they play different games). If you really want a custom filename, or to load the
keymap from a larger user profile, you can give the contents to keyMappingFromString(String)
on a
SquidInput after construction.mouse
- a SquidMouse instance that will be used for handling mouse input.public SquidInput(SquidInput.KeyHandler keyHandler)
ApplicationListener.create()
method has started, and
cannot be called in an ApplicationListener's constructor or in initialization at the class level. This is because
all constructors attempt to load a file, if it exists, with Gdx.files.local("keymap.preferences")
, and
Gdx.files
is only available starting in create(). This file, keymap.preferences, is meant to contain any
key remappings specified by the user, and its contents should be produced by keyMappingToString()
when
any key mappings change and are saved. You can use your own filename, but keymap.preferences is the standard one
and these can be exchanged between games (so common remappings for users of DVORAK or AZERTY keyboards can be
sent between users even if they play different games). If you really want a custom filename, or to load the
keymap from a larger user profile, you can give the contents to keyMappingFromString(String)
on a
SquidInput after construction.keyHandler
- must implement the SquidInput.KeyHandler interface so it can handle() key input.public SquidInput(SquidInput.KeyHandler keyHandler, boolean ignoreInput)
ApplicationListener.create()
method has started, and
cannot be called in an ApplicationListener's constructor or in initialization at the class level. This is because
all constructors attempt to load a file, if it exists, with Gdx.files.local("keymap.preferences")
, and
Gdx.files
is only available starting in create(). This file, keymap.preferences, is meant to contain any
key remappings specified by the user, and its contents should be produced by keyMappingToString()
when
any key mappings change and are saved. You can use your own filename, but keymap.preferences is the standard one
and these can be exchanged between games (so common remappings for users of DVORAK or AZERTY keyboards can be
sent between users even if they play different games). If you really want a custom filename, or to load the
keymap from a larger user profile, you can give the contents to keyMappingFromString(String)
on a
SquidInput after construction.keyHandler
- must implement the SquidInput.KeyHandler interface so it can handle() key input.ignoreInput
- true if this should ignore input initially, false if it should process input normally.public SquidInput(SquidInput.KeyHandler keyHandler, SquidMouse mouse)
ApplicationListener.create()
method has started, and
cannot be called in an ApplicationListener's constructor or in initialization at the class level. This is because
all constructors attempt to load a file, if it exists, with Gdx.files.local("keymap.preferences")
, and
Gdx.files
is only available starting in create(). This file, keymap.preferences, is meant to contain any
key remappings specified by the user, and its contents should be produced by keyMappingToString()
when
any key mappings change and are saved. You can use your own filename, but keymap.preferences is the standard one
and these can be exchanged between games (so common remappings for users of DVORAK or AZERTY keyboards can be
sent between users even if they play different games). If you really want a custom filename, or to load the
keymap from a larger user profile, you can give the contents to keyMappingFromString(String)
on a
SquidInput after construction.keyHandler
- must implement the SquidInput.KeyHandler interface so it can handle() key input.mouse
- a SquidMouse instance that will be used for handling mouse input.public SquidInput(SquidInput.KeyHandler keyHandler, SquidMouse mouse, boolean ignoreInput)
ApplicationListener.create()
method has started, and
cannot be called in an ApplicationListener's constructor or in initialization at the class level. This is because
all constructors attempt to load a file, if it exists, with Gdx.files.local("keymap.preferences")
, and
Gdx.files
is only available starting in create(). This file, keymap.preferences, is meant to contain any
key remappings specified by the user, and its contents should be produced by keyMappingToString()
when
any key mappings change and are saved. You can use your own filename, but keymap.preferences is the standard one
and these can be exchanged between games (so common remappings for users of DVORAK or AZERTY keyboards can be
sent between users even if they play different games). If you really want a custom filename, or to load the
keymap from a larger user profile, you can give the contents to keyMappingFromString(String)
on a
SquidInput after construction.keyHandler
- must implement the SquidInput.KeyHandler interface so it can handle() key input.mouse
- a SquidMouse instance that will be used for handling mouse input.ignoreInput
- true if this should ignore input initially, false if it should process input normally.public void setKeyHandler(SquidInput.KeyHandler keyHandler)
public void setMouse(SquidMouse mouse)
public boolean isUsingNumpadDirections()
public void setUsingNumpadDirections(boolean using)
public SquidInput.KeyHandler getKeyHandler()
public SquidMouse getMouse()
public boolean getIgnoreInput()
public void setIgnoreInput(boolean ignoreInput)
ignoreInput
- true if this should object should ignore and not queue input, false otherwise.public SquidInput remap(char pressedChar, boolean pressedAlt, boolean pressedCtrl, boolean pressedShift, char targetChar, boolean targetAlt, boolean targetCtrl, boolean targetShift)
SquidInput.KeyHandler.handle(char, boolean, boolean, boolean)
(possibly with some pressed modifiers) to another char with possible modifiers. When the first key/modifier mix
is received, it will be translated to the second group in this method.pressedChar
- a source char that might be used in handling, like 'q', 'A', '7', '(', or UP_ARROW
.pressedAlt
- true if alt is part of the source combined keypress, false otherwisepressedCtrl
- true if ctrl is part of the source combined keypress, false otherwisepressedShift
- true if shift is part of the source combined keypress, false otherwisetargetChar
- a target char that might be used in handling, like 'q', 'A', '7', '(', or UP_ARROW
.targetAlt
- true if alt is part of the target combined keypress, false otherwisetargetCtrl
- true if ctrl is part of the target combined keypress, false otherwisetargetShift
- true if shift is part of the target combined keypress, false otherwisepublic SquidInput remap(int pressed, int target)
pressed
combination is received, it will be translated to target
in this method.pressed
- an int for the source keypress, probably produced by combineModifiers(char, boolean, boolean, boolean)
target
- an int for the target keypress, probably produced by combineModifiers(char, boolean, boolean, boolean)
combineModifiers is usually used to make pressed and target
public SquidInput remap(int[] pairs)
pairs
- an int array alternating source and target keypresses, each probably produced by combineModifiers(char, boolean, boolean, boolean)
combineModifiers is usually used to make the contents of pairs
public SquidInput unmap(char pressedChar, boolean pressedAlt, boolean pressedCtrl, boolean pressedShift)
pressedChar
- a char that might be used in handling, like 'q', 'A', '7', '(', or UP_ARROW
.pressedAlt
- true if alt is part of the combined keypress, false otherwisepressedCtrl
- true if ctrl is part of the combined keypress, false otherwisepressedShift
- true if shift is part of the combined keypress, false otherwisepublic SquidInput unmap(int pressed)
pressed
- an int for the source keypress, probably produced by combineModifiers(char, boolean, boolean, boolean)
combineModifiers is usually used to make pressed
public SquidInput clearMapping()
public static int combineModifiers(char key, boolean alt, boolean ctrl, boolean shift)
SquidInput.KeyHandler.handle(char, boolean, boolean, boolean)
) with the
three booleans for the alt, ctrl, and shift modifier keys, returning an int that can be used with the internal
queue of ints or the public mapping
of received inputs to actual inputs the program can process.key
- a char that might be used in handling, like 'q', 'A', '7', '(', or UP_ARROW
.alt
- true if alt is part of this combined keypress, false otherwisectrl
- true if ctrl is part of this combined keypress, false otherwiseshift
- true if shift is part of this combined keypress, false otherwisepublic java.lang.String keyMappingToString()
keyMappingFromString(String)
. Because that method is automatically called by SquidInput's constructor
and tries to load a file named "keymap.preferences" in the local folder, it is recommended that you save
user-remapped keys to that file, but only if the user has actively changed them (that file could exist from an
earlier run and you don't want to affect it unless the user made some change). This will allow input with any of
Shift, Alt, and Ctl modifiers in any script supported by the Unicode Basic Multilingual Plane (the first 65536
chars of Unicode).public SquidInput keyMappingFromString(java.lang.String keymap)
keyMappingToString()
) to set the current key remapping.
This is automatically called if a file named "keymap.preferences" is present in the local folder, receiving the
contents of that file as a parameter, so games that want to save a user's preferences should try to save that
file if possible (this also allows one file to be reused across multiple computers or installations by copying
it). This will allow input with any of Shift, Alt, and Ctl modifiers in any script supported by the Unicode Basic
Multilingual Plane (the first 65536 chars of Unicode). You may want to call clearMapping()
before
calling this if you have already set the mapping and want the String's contents to be used as the only mapping.keymap
- a String that was probably produced by keyMappingToString()
public void drain()
public long getRepeatGap()
public void setRepeatGap(long time)
time
- how long a key needs to be held before this will count it as a key repeat, as a positive long in millisecondspublic boolean hasNext()
keyDown(int)
if a key is being held but
there is a failure to process the repeated event. The conditions this checks:
keyDown(int)
already been called at least once?getRepeatGap()
milliseconds between the last key being received and this call?setRepeatGap(long)
, but the default of 220 ms is usually suitable. Too low of a value can cause normal
key presses to be counted twice or more, and too high of a value may delay an expected key repeat.public void next()
public void flush()
public boolean keyDown(int keycode)
keyDown
in interface com.badlogic.gdx.InputProcessor
keyDown
in class com.badlogic.gdx.InputAdapter
public boolean keyUp(int keycode)
keyUp
in interface com.badlogic.gdx.InputProcessor
keyUp
in class com.badlogic.gdx.InputAdapter
public boolean keyTyped(char character)
keyTyped
in interface com.badlogic.gdx.InputProcessor
keyTyped
in class com.badlogic.gdx.InputAdapter
public boolean touchDown(int screenX, int screenY, int pointer, int button)
touchDown
in interface com.badlogic.gdx.InputProcessor
touchDown
in class com.badlogic.gdx.InputAdapter
public boolean touchUp(int screenX, int screenY, int pointer, int button)
touchUp
in interface com.badlogic.gdx.InputProcessor
touchUp
in class com.badlogic.gdx.InputAdapter
public boolean touchDragged(int screenX, int screenY, int pointer)
touchDragged
in interface com.badlogic.gdx.InputProcessor
touchDragged
in class com.badlogic.gdx.InputAdapter
public boolean mouseMoved(int screenX, int screenY)
mouseMoved
in interface com.badlogic.gdx.InputProcessor
mouseMoved
in class com.badlogic.gdx.InputAdapter
public boolean scrolled(float amountX, float amountY)
scrolled
in interface com.badlogic.gdx.InputProcessor
scrolled
in class com.badlogic.gdx.InputAdapter
public char fromCode(int keycode, boolean shift)
','
to '<'
'.'
to '>'
'/'
to '?'
';'
to ':'
'\''
to '"'
'['
to '{'
']'
to '}'
'|'
to '\\'
'-'
to '_'
'+'
to '='
'`'
to '~'
(note, this key produces no event on the GWT backend)keycode
- a keycode as passed by libGDXshift
- true if Shift key is being held.Copyright © Eben Howard 2012–2022. All rights reserved.