Skip to content

Keys

The keys API allows access to the state of the keyboard without hooking any events. The downside is that you will not get information about when a key was pressed, only IF it is pressed or not. Additionally you will not give information about wether an uppercase or lowercase letter was pressed, you would also need to track the shift key to get this information. For simple player controls this might however be the easiest API to use.

Usage

To use this module, add the following require at the top of your script:

local Keys = require 'engine/keys'

Reference

__init

static __init()

This function initializes the keys module and installs the hooks

isPressed

static isPressed(key)

Return true, if the given key is pressed.

Parameters

  • key (string): the key to check

Returns

  • boolean

areAllPressed

static areAllPressed(keys)

Return true, if all the given keys in the array are pressed

Parameters

  • keys

Returns

  • boolean

isOnePressed

static isOnePressed(keys)

Return true, if at least one of the given keys in the array is pressed

Parameters

  • keys

Returns

  • boolean

Examples

local Keys = require 'engine/keys'
local Vector3 = require 'engine/math/vector3'

-- If any key is pressed that we care for, we need to move the character
local movement = Vector3.new()
if Keys.isOnePressed({ Keys.W, Keys.ArrowUp }) then
    movement.z = movement.z - SPEED
end
if Keys.isOnePressed({ Keys.S, Keys.ArrowDown }) then
    movement.z = movement.z + SPEED
end
if Keys.isOnePressed({ Keys.A, Keys.ArrowLeft }) then
    movement.x = movement.x - SPEED
end
if Keys.isOnePressed({ Keys.D, Keys.ArrowRight }) then
    movement.x = movement.x + SPEED
end
local keys = {
    Backspace = "Backspace",
    Tab = "Tab",
    Enter = "Enter",
    Shift = "Shift",
    Control = "Control",
    Alt = "Alt",
    Pause = "Pause",
    CapsLock = "CapsLock",
    Escape = "Escape",
    Space = "Space",
    PageUp = "PageUp",
    PageDown = "PageDown",
    End = "End",
    Home = "Home",
    ArrowLeft = "ArrowLeft",
    ArrowUp = "ArrowUp",
    ArrowRight = "ArrowRight",
    ArrowDown = "ArrowDown",
    PrintScreen = "PrintScreen",
    Insert = "Insert",
    Delete = "Delete",
    Digit0 = "Digit0",
    Digit1 = "Digit1",
    Digit2 = "Digit2",
    Digit3 = "Digit3",
    Digit4 = "Digit4",
    Digit5 = "Digit5",
    Digit6 = "Digit6",
    Digit7 = "Digit7",
    Digit8 = "Digit8",
    Digit9 = "Digit9",
    A = "KeyA",
    B = "KeyB",
    C = "KeyC",
    D = "KeyD",
    E = "KeyE",
    F = "KeyF",
    G = "KeyG",
    H = "KeyH",
    I = "KeyI",
    J = "KeyJ",
    K = "KeyK",
    L = "KeyL",
    M = "KeyM",
    N = "KeyN",
    O = "KeyO",
    P = "KeyP",
    Q = "KeyQ",
    R = "KeyR",
    S = "KeyS",
    T = "KeyT",
    U = "KeyU",
    V = "KeyV",
    W = "KeyW",
    X = "KeyX",
    Y = "KeyY",
    Z = "KeyZ",
    Meta = "Meta",
    ContextMenu = "ContextMenu",
    F1 = "F1",
    F2 = "F2",
    F3 = "F3",
    F4 = "F4",
    F5 = "F5",
    F6 = "F6",
    F7 = "F7",
    F8 = "F8",
    F9 = "F9",
    F10 = "F10",
    F11 = "F11",
    F12 = "F12",
    F13 = "F13",
    F14 = "F14",
    F15 = "F15",
    F16 = "F16",
    F17 = "F17",
    F18 = "F18",
    F19 = "F19",
    F20 = "F20",
    F21 = "F21",
    F22 = "F22",
    F23 = "F23",
    F24 = "F24",
    NumLock = "NumLock",
    ScrollLock = "ScrollLock",
    Semicolon = "Semicolon",
    Equal = "Equal",
    Comma = "Comma",
    Minus = "Minus",
    Period = "Period",
    Slash = "Slash",
    Backquote = "Backquote",
    BracketLeft = "BracketLeft",
    Backslash = "Backslash",
    BracketRight = "BracketRight",
    Quote = "Quote",
    AltGraph = "AltGraph",
    Dead = "Dead",
    NumPad0 = "NumPad0",
    NumPad1 = "NumPad1",
    NumPad2 = "NumPad2",
    NumPad3 = "NumPad3",
    NumPad4 = "NumPad4",
    NumPad5 = "NumPad5",
    NumPad6 = "NumPad6",
    NumPad7 = "NumPad7",
    NumPad8 = "NumPad8",
    NumPad9 = "NumPad9",
    NumPadMultiply = "NumPadMultiply",
    NumPadAdd = "NumPadAdd",
    NumPadSubtract = "NumPadSubtract",
    NumPadDecimal = "NumPadDecimal",
    NumPadDivide = "NumPadDivide",
    NumPadEnter = "NumPadEnter",
    NumPadEqual = "NumPadEqual"
}