er2.bindKey 41
Calls a callback when a keyboard key is pressed. The callback is one registered with er2.setCallback, so you bind a key to an id, not to a function. Binding the same key again replaces the previous id.Keys are watched only while at least one binding exists, and only on this computer: nothing is sent to the other players (call er2.runEveryone from the callback if you need that). The key is ignored while a menu or the chat has the focus, and on consoles there is no keyboard at all.Key names are Unity KeyCode names, case insensitive: F5, K, Alpha1 (the 1 above the letters), Keypad1, Space, LeftShift, Tab. Keys the game already uses still do their normal job.
Return value
| Return Type | Description |
|---|---|
| void | True if the key was bound. False if the key name is unknown or the callback id is empty. |
Parameters
| Parameter | Type | Description |
|---|---|---|
| 1 | string | The key, as a Unity KeyCode name: F5, K, Alpha1, Space... |
| 2 | string | The id of a callback registered with er2.setCallback. |
Examples
A key that calls artillery
-- F5 fires a barrage on the player's position, once every 30 seconds
local nextBarrage = 0
er2.setCallback("barrage", function()
if er2.time() < nextBarrage then
er2.print("Artillery is reloading")
return
end
nextBarrage = er2.time() + 30
local player = er2.getPlayer()
if player ~= nil then
local p = player:getPosition()
er2.explosion(vec3(p.x, p.y, p.z + 25), 400, 20, 12)
end
end)
er2.bindKey("F5", "barrage")
-- the key is released when the mission no longer needs it
-- er2.unbindKey("F5")