Handles user input such as keyboard, mouse and joystick.
This is a global object that can always be accessed.
By “binding” input to functions, it causes the function to be run when the key/button is pressed and again when it is released.
If the same key is bound more than one, more than one function will be called, in the opposite order that they were bound in.
//move the mouse to the center of the screen
GetInputManager:SetMousePos(Vector2(C_SCREEN_X/2, C_SCREEN_Y/2));
InputManager | Handles user input such as keyboard, mouse and joystick. |
Binding Related | |
AddBinding | |
RemoveBinding | |
RemoveBindingByEntity | |
Direct Device Access | |
GetMousePos | |
SetMousePos | |
GetJoystickCount | |
GetJoystick |
nil AddBinding(string inputName, string callbackName, number entityID)
Adding a binding causes the engine to call the specified function every time the key is pressed or released.
This can be a single key, joystick button/dir, or mouse button.
Valid input name examples: a, b, c, 3, 4, f1, control, mouse_middle, mouse_left, space, mouse_wheel_up, tab, escape, subtract, equals, numpad_subtract, numpad_add, left, right, up, down and so forth.
return | The Enter key, don’t ask me why it’s called return in Clanlib, did they write it on a Commodore 64? |
control | The control key must be held down also. “control,r” means Ctrl-R must be pressed. |
shift | The shift key must be held down also. “shift,r” means Shift-R must be pressed. |
alt | The alt/option key must be held down also. “shift,r” means Shift-R must be pressed. |
always | This means the key will always trigger, even if shift/alt/control is also down. Without “left,always”, you couldn’t fire with control and press left at the same time. |
editor_only | Means these keybindings will only trigger when the editor is open. |
game_and_editor | Means these keybindings will always trigger. Default is only while the editor is not open. |
//binding with a global callback
GetInputManager:AddBinding("f1,always", "OnHitF1Key", C_ENTITY_NONE);
//Let's also bind something to a specific entity, by providing a valid entityID
GetInputManager:AddBinding("mouse_left", "OnMouseLeft", someEntityID);
function OnHitF1Key(bKeyDown)
if (bKeyDown) then
LogMsg("You pressed F1 down.");
else
LogMsg("You released F1.");
end
return true; //keep processing additional callbacks for F1 if applicable
end
function OnMouseLeft(bKeyDown)
if (bKeyDown) then
LogMsg("You pressed the left mouse button down.");
else
LogMsg("You released the left mouse button.");
end
return true; //keep processing additional callbacks for the left mouse button if applicable
end
Valid joystick name examples: joy_0_left, joy_1_up, joy_0_button_0, joy_any_down
Instead of a # for which joystick, you can use “any” to map to all of them.
inputName | A string defining some forum of input. Use comma to separate words. The order doesn’t matter. |
callbackName | A string containing the name of a properly setup function that should be called. (it must accept one parm and return true or false) |
entityID | If not <C_ENTITY_NONE> this means the function is located in this Entity’s namespace. |
When an Entity is deleted, any associated bindings to it are also automatically deleted for you.
boolean RemoveBinding(string inputName, string callbackName, int entityID)
This allows you to remove any previously setup binding. Because there may be many bindings to the same key, you must enter all the information so it knows which binding to remove.
inputName | A string defining the input, the same one used to make the binding. |
string callBackName | The name of the callback function that was used to create the binding we’re after. |
entityID | If the original binding was to an Entity function, you’ll need to enter its ID here. Otherwise, <C_ENTITY_NONE>. |
True if a binding was actually removed.
Vector2 GetMousePos()
A Vector2 object containing the location of the mouse in screen coordinates. Use ScreenToWorld to convert to a position on the active Map.
nil SetMousePos(Vector2 vPos)
vPos | A Vector2 object containing the new mouse position in screen coordinates. |
Joystick GetJoystick(number joystickID)
This let’s you get access to additional information about the connected joysticks. So you can detect say, a 360 controller and properly map to its extra buttons.
You use the normal AddBinding to map to joysticks.
A Joystick object or nil if the joystick doesn’t exist.