-
Notifications
You must be signed in to change notification settings - Fork 28
Input
Punity supports mouse and keyboard input handling in immediate mode fashion. That means there are no events for you to handle, just a bunch of properties available through CORE
pointer.
Punity defines KEY_*
constants for good amount of key codes (see Key codes section at the end of this document). Following functions can be used to check for current frame's state of keys:
-
key_pressed(k)
- Returns
1
if keyk
was pressed down in this frame.
- Returns
-
key_released(k)
- Returns
1
if keyk
was released in this frame.
- Returns
-
key_down(k)
- Returns
1
if keyk
is being held down in this frame.
- Returns
Example:
int
step()
{
f32 dx = 0;
if (key_down(KEY_RIGHT)) { dx = 1; }
if (key_down(KEY_LEFT)) { dx = 1; }
if (key_pressed(KEY_UP)) { state = JUMP; }
// ...
}
Key modifiers are also You can also check for the modifiers being held down:
if (CORE->key_modifiers & KEY_MOD_CONTROL) { ... }
Text typed in the previous frame is available through:
char CORE->key_text[8]
u8 CORE->key_text_length
If PUN_TEXT_UNICODE
is set to 1
, the CORE->key_text
uses u32
instead of char
and entire codepoints are provided.
Example for simple text input:
static char input[256];
static int input_length = 0;
if (CORE->key_text_length) {
for (int i = 0;
i != CORE->key_text_length && input_length != 254;
++i, ++input_length) {
input[input_length] = CORE->key_text[i];
}
input[input_length] = 0;
}
text_draw(input, 0, 0, 1);
Mouse position (relative to the current viewport) is available through:
CORE->mouse_x
CORE->mouse_y
Mouse buttons are handled via keyboard API using these key constants:
KEY_MOUSE_LEFT
KEY_MOUSE_RIGHT
KEY_MOUSE_MIDDLE
Example:
int color = 1;
if (key_down(KEY_MOUSE_LEFT)) { color = 1; }
if (key_down(KEY_MOUSE_RIGHT)) { color = 2; }
if (key_down(KEY_MOUSE_MIDDLE)) { color = 3; }
rect_draw(rect_make_size(CORE->mouse_x - 2, CORE->mouse_y - 2, 4, 4), color);
KEY_MOD_SHIFT
KEY_MOD_CONTROL
KEY_MOD_ALT
KEY_MOD_SUPER
KEY_MOUSE_LEFT
KEY_MOUSE_RIGHT
KEY_MOUSE_MIDDLE
KEY_BACKSPACE
KEY_TAB
KEY_RETURN
KEY_ESCAPE
KEY_PAGEUP
KEY_PAGEDOWN
KEY_END
KEY_HOME
KEY_LEFT
KEY_UP
KEY_RIGHT
KEY_DOWN
KEY_PRINTSCREEN
KEY_F1
KEY_F2
KEY_F3
KEY_F4
KEY_F5
KEY_F6
KEY_F7
KEY_F8
KEY_F9
KEY_F10
KEY_F11
KEY_F12
KEY_SPACE
KEY_APOSTROPHE
KEY_COMMA
KEY_MINUS
KEY_PERIOD
KEY_SLASH
KEY_0
KEY_1
KEY_2
KEY_3
KEY_4
KEY_5
KEY_6
KEY_7
KEY_8
KEY_9
KEY_SEMICOLON
KEY_EQUAL
KEY_A
KEY_B
KEY_C
KEY_D
KEY_E
KEY_F
KEY_G
KEY_H
KEY_I
KEY_J
KEY_K
KEY_L
KEY_M
KEY_N
KEY_O
KEY_P
KEY_Q
KEY_R
KEY_S
KEY_T
KEY_U
KEY_V
KEY_W
KEY_X
KEY_Y
KEY_Z
KEY_LEFT_BRACKET
KEY_BACK_SLASH
KEY_RIGHT_BRACKET
KEY_BACK_QUOTE