Raw Input¶
The input module allows retrieving information about input events, like taps on the screen, directly from the input manager. This is especially useful if you want to implement your own UI elements where the game’s current input handling is too limiting.
Changed in version 22: Added input simulation, keyboard, mouse, and button support
Note
Input simulation functions should be called from onFrameStart() so that
the input is present for the full frame. Calling from the normal draw() or
frame() functions is unlikely to give good results.
Touch¶
QiInput can handle up to 32 touch events at once, though most
devices cannot reach this limit. Each event is assigned an index from 0
to 31, with new events (e.g. new touches) taking the lowest available
index.
If you want to handle more than once touch at a time, it is your job to
check if each possible index (0 to 31) contains a valid touch event as
there is no precise way to query which indexes have currently valid
touch events without using knHasTouch().
Otherwise if just want to use the most recent touch at index zero, you
can exclude the index argument for every function call and it will
default to zero. All built-in UI elements do this.
-
knGetTouchCount():
integer¶ Get the number of touches. May not correspond with the maximum touch index.
-
knHasTouch([
index]):boolean¶ Check if a valid touch exists at the given
index. Returnstrueif so, orfalseif not.
-
knGetTouchPos([
index]):integer,integer¶ Returns the x and y position of the touch in screen coordinates. Note that these may not always line up with the virtual in-game coordinates.
Example:
local x, y = knGetTouchPos(0)
-
knWasTouchPressed([
index]):boolean¶ Return
trueif the touch at the given index was just pressed andfalseotherwise. This will only happen once per touch at the very start of the touch.
-
knWasTouchReleased([
index]):boolean¶ Currently broken and always returns ``false``, but should(?) return
trueif the touch at the given index was just released andfalseotherwise. This will only happen once per touch at the very end of the touch.
Keyboard¶
QiInput’s keyboard support is rather limited. It can detect key up and key
down events, as well as use them to check if a key was pressed once, but doesn’t
do much more, and has virtually no support for keyboard layouts outside of those
based on QWERTY.
Key codes are given as the ASCII characters those keys represent, with a few extra codes for various command and modifier keys. The currently implemented extra key codes are:
Key |
ID |
|---|---|
|
0x100 |
|
0x101 |
|
0x102 |
|
0x103 |
|
0x104 |
|
0x105 |
|
0x106 |
|
0x107 |
|
0x108 |
|
0x109 |
|
0x10a |
|
0x10b |
|
0x10c |
|
0x10d |
|
0x10e |
Many of the alphanumeric keys are implemented but they assume a QWERTY keyboard layout so may not behave as expected, especially when Shift is involved.
-
knIsKeyDown(key:
string|integer):boolean¶ Return
trueif the given key is currently down, orfalseif it isn’t. For example:if knIsKeyDown('a') then mgSetText(someText, "Key is being held!") else mgSetText(someText, "Key is up.") end
-
knWasKeyPressed(key:
string|integer):boolean¶ Return
trueif the given key has been initally pressed, orfalseif it has’t.
-
knWasKeyReleased(key:
string|integer):boolean¶ Return
trueif the given key was just released, orfalseif it was’t.
-
knRegisterKeyDown(key:
string|integer)¶ Simulate the start of a key press.
-
knRegisterKeyUp(key:
string|integer)¶ Simulate the end of a key press.
Mouse¶
Mouse support only includes checking the position of the mouse.
-
knGetMousePos():
integer,integer¶ Return the
xandyposition of a physical mouse.Warning
When the mouse is in relative mode, this function will continue to return the delta from the last mouse input event, which is never completely zero even if the mouse isn’t moving. Using
knGetMouseDelta()gives the result you probably expect.
-
knGetMouseDelta():
integer,integer¶ When the mouse is in relative mode, this gets the total movement of the mouse in the
xandydirections since this function was last called.Warning
This function only produces sane results when in relative mode - that is, after a successful call to
knCaptureMouse().This function can only be called once per frame. After that, it will return 0, 0 until more mouse movements occur.
-
knCaptureMouse(capture:
boolean)¶ If possible, “capture” the mouse and turn it to relative mode. This is useful for implementing free looking, first person cameras that can be moved around with the mouse.
It is recommended to call this every frame with
truewhen you want the mouse locked andfalsewhen you wish for it to be unlocked:-- In the HUD function draw() knCaptureMouse(true) -- ... end -- In the menu function draw() knCaptureMouse(false) -- ... end