DatumHuePlatform v0.0.24datumhue.com
Contents

Read keys, mouse, touch and gamepads

Read the keyboard, the mouse, touches and gamepads yourself, for a game or a canvas that people play with directly.

Widgets or reads

Widgets need none of this: buttons, fields and focus navigation already answer every device (Keyboard and gamepad). Read input yourself when what you draw is what people play with, as in this program, where the keys, a stick, a click or a tap move a ball.

move.lualua
local input = datumhue.input
local vec2 = datumhue.math.vec2
local color = datumhue.color

local box = datumhue.ui.root:panel({ gap = 8, align_items = "center" })
local canvas = datumhue.draw.new({
    width = 320,
    height = 240,
    background = color("#2d353b"),
})
local view = canvas:mount({ parent = box, width = 640, height = 480 })
local ball = canvas:circle({ radius = 12, color = color("#e67e80") })
local hint = box:label({ text = "" })

input.bind_action("left", { keys = { "arrow_left", "a" } })
input.bind_action("right", { keys = { "arrow_right", "d" } })
input.bind_action("up", { keys = { "arrow_up", "w" } })
input.bind_action("down", { keys = { "arrow_down", "s" } })
input.bind_action("paint", {
    keys = { "space" },
    gamepad_buttons = { "south" },
})

local paints = { color("#e67e80"), color("#a7c080"), color("#7fbbb3") }
local paint = 1
local pos = vec2(0, 0)
local low, high = vec2(-160, -120), vec2(160, 120)

local function move_to(at)
    local world = at and canvas:screen_to_world(at)
    if world then
        pos = world:clamp(low, high)
    end
end

datumhue.on_update(function(dt)
    -- action_vector's y grows down; the canvas's grows up.
    local x, y = input.action_vector("left", "right", "up", "down")
    pos = (pos + vec2(x, -y) * (160 * dt)):clamp(low, high)
    if input.is_action_pressed("paint") then
        paint = paint % #paints + 1
        ball:update({ color = paints[paint] })
    end
    if input.is_mouse_pressed("left") then
        move_to(view:mouse_position())
    end
    for _, touch in ipairs(input.touches()) do
        local at = input.app_touch_position(touch.id)
        local box_at = view.bounds
        if input.is_touch_pressed(touch.id) and at and box_at then
            move_to(at - box_at.min)
        end
    end
    ball.pos = pos
    hint.text = "Last input: " .. input.last_source
end)

In this example: datumhue.input datumhue.math.vec2 datumhue.color datumhue.ui.root UiElement:panel datumhue.draw.new DrawCanvas:mount DrawCanvas:circle UiElement:label datumhue.input.bind_action DrawCanvas:screen_to_world datumhue.on_update datumhue.input.action_vector datumhue.input.is_action_pressed DrawPrimitive:update datumhue.input.is_mouse_pressed UiElement:mouse_position datumhue.input.touches datumhue.input.app_touch_position UiElement.bounds datumhue.input.is_touch_pressed DrawPrimitive.pos UiElement.text datumhue.input.last_source

  • An action names the inputs that drive it, so people can remap them in one place. input.action_vector adds the left stick and D-pad of the gamepad in use, and each touch stick your app holds, to four actions, and keeps the result no longer than 1.
  • input.action_vector grows y downward, as the screen does, and the canvas grows y upward, so the program flips it.
  • Space or gamepad A changes the ball's color, once per press.
  • A click goes through UiElement:mouse_position and DrawCanvas:screen_to_world to a point on the canvas. A tap starts from input.app_touch_position, less the canvas's top-left corner.
  • input.last_source names the device that sent the latest input.

Presses between frames

input.is_key_pressed, input.is_action_pressed and the other _pressed and _released reads report a press or a release since your app's previous update, once each, so none is lost when an update spans several frames. The _down reads report what is held now. input.on_key and input.on_button call you on each press and release instead.

Key names

Keys are named by their place on the keyboard, not by the letter they type: "a" is the key where a QWERTY keyboard has A, whatever the layout. "shift", "control", "alt" and "super" match either side, and names such as "shift_left" name one. "super" is the Super, Command or Windows key, and has no side names. For text people type, use input.on_text, which hears the characters their layout produces.

Pointer and touch positions

ReadGives
input.app_mouse_position, input.app_touch_positionUI px from your content area's top-left, as UiElement.bounds
UiElement:mouse_positionUI px from the element's top-left
input.mouse_position, input.touchesthe window's logical pixels, not scaled by the zoom

A touch is a device of its own and never presses a mouse button, so you can read touches beside the mouse buttons without counting a tap twice. Image:touch gives a touch's position in an image's own pixels, as Image:cursor gives the mouse's.

Touch controls

UiElement:touch_stick and UiElement:touch_button put a thumb stick and buttons on a touchscreen. A touch that starts on one belongs to it until the touch ends: input.touches() and the elements under it never see that touch. A stick adds to input.action_vector, and a button holds a named action down, so one set of actions serves keys, gamepads and touches. By default each control shows only while input.last_source reads "touch", and the mouse never presses one.

Gamepads

input.gamepads lists the connected pads. An id is a pad's place in that list and shifts when pads come and go, so list them again rather than keep an id. input.last_gamepad is the id of the pad in use, the one last pressed or pushed: a game for one player reads that pad, so whichever pad someone picks up plays. Read a stick with input.stick, a trigger with input.trigger and a button with input.is_button_down; input.rumble shakes a pad.

Prompts and rebinding

input.action_label gives the words a prompt shows for an action: its key in the keyboard layout people type with (the key in the QWERTY "q" place reads "A" on a French keyboard), its pad button named as the pad in use names it, its mouse button, or the text of a touch button that presses it. Without a device it names the one input.last_source reads. Rebinding stays with your app: hear the next key with input.on_key, bind it with input.bind_action, and keep it in datumhue.storage for the next run.

Only while your app is focused

Only the focused app and the apps above it read real input. Every other app reads false, nil and zero, so an app in the background cannot watch keystrokes. The cursor calls (input.set_cursor_visible, input.set_cursor_locked and input.set_cursor_icon) set your app's own cursor, which shows while your app is focused.

See also

Reference used on this page: datumhue.input datumhue.math.vec2 datumhue.color datumhue.ui.root UiElement:panel datumhue.draw.new DrawCanvas:mount DrawCanvas:circle UiElement:label datumhue.input.bind_action DrawCanvas:screen_to_world datumhue.on_update datumhue.input.action_vector datumhue.input.is_action_pressed DrawPrimitive:update datumhue.input.is_mouse_pressed UiElement:mouse_position datumhue.input.touches datumhue.input.app_touch_position UiElement.bounds datumhue.input.is_touch_pressed DrawPrimitive.pos UiElement.text datumhue.input.last_source datumhue.input.app_mouse_position datumhue.input.mouse_position datumhue.input.is_key_pressed datumhue.input.on_key datumhue.input.on_button datumhue.input.on_text Image:touch Image:cursor UiElement:touch_stick UiElement:touch_button datumhue.input.gamepads datumhue.input.last_gamepad datumhue.input.stick datumhue.input.trigger datumhue.input.is_button_down datumhue.input.rumble datumhue.input.action_label datumhue.storage datumhue.input.set_cursor_visible datumhue.input.set_cursor_locked datumhue.input.set_cursor_icon

datumhue.com

(c) 2026 Shuusei AB. Quote excerpts of this manual, with attribution to DatumHue, in your own documentation, tutorials, course materials, reviews, and other writing or video. Republishing it in whole or in substantial part needs written permission: support@datumhue.com.

Victor Mono is licensed under the SIL Open Font License 1.1
Copyright (c) 2024, Rune Bjørnerås (https://github.com/rubjo)

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

Try

    ↑↓ selectEnter openTab filter

    Settings

    Theme
    Text
    Print

    Keyboard shortcuts

    / or Ctrl KSearch
    ↑ ↓ EnterPick a result
    TabNext result filter
    j kNext or previous entry
    [ ]Previous or next page
    yCopy a link to the current entry
    ?These shortcuts
    EscClose

    On this page