Modules

Input

moduleSource

Available on macOS, Linux, Windows

#import "Input";

Keyboard, mouse, touch and window events for windows made with Window_Creation.

Call update_window_events once per frame, then read events_this_frame for what happened and input_button_states for what is held. Each state is a Key_Current_State bit set indexed by Key_Code.

Example↗
#import "Window_Creation";
#import "Input";

window := create_window(800, 600, "input");
while true {
    update_window_events();
    for events_this_frame {
        if it.type == .QUIT return;
        if it.type == .KEYBOARD && it.key_code == .ESCAPE return;
    }
    if input_button_states[Key_Code.MOUSE_BUTTON_LEFT] & .START print("click at %, %\n", mouse_delta_x, mouse_delta_y);
}

This one is only checked to compile, not run.

Procedures#

General#

X11 keys#

X11 events#

(is_custom: bool)
(is_custom: bool)
(is_custom: bool)

Tells the library whether the program manages the mouse cursor itself.

()
()
()

Collects the events of all windows into events_this_frame and the button states. Call once per frame.

Windows keys#

Windows events#

Types#

:: enum

What kind of thing an Event reports.

UNINITIALIZED
KEYBOARD
TEXT_INPUT
WINDOW
MOUSE_WHEEL
QUIT
DRAG_AND_DROP_FILES
TOUCH
:: enum

What happened to the window, for events of type .WINDOW.

UNINITIALIZED
GAINED_FOCUS
LOST_FOCUS
MOVED_TO_BACKGROUND
MOVED_TO_FOREGROUND
:: enum_flags

The state of a key or button this frame. DOWN lasts while it is held; START and END are set only on the frame it went down or up.

NONE
DOWN
START
END
state := Key_Current_State.DOWN | .START;
assert((state & .START) != 0);
assert((state & .END) == 0);
↗
:: struct

One input event. Check type first; it decides which other fields mean anything: key_code and repeat for .KEYBOARD, utf32 for .TEXT_INPUT, wheel_delta for .MOUSE_WHEEL, files for .DRAG_AND_DROP_FILES, and so on. The modifier flags (shift_pressed, ctrl_pressed, alt_pressed, cmd_meta_pressed) apply to every event.

modifier_flags: Modifier_Flags
key_pressed: u32
key_code: := Key_Code.UNKNOWN
utf32: u32
repeat: := false
text_input_count: u16
typical_wheel_delta: s32
wheel_delta: s32
files: [..] string
touch_type: enum u8 { MOVED :: 0; PRESSED :: 1; RELEASED :: 2; }
touch_index: s32
touch_x: float
touch_y: float
window_event_type: Window_Event_Type
window: Window_Type
event: Event;
event.type = .KEYBOARD;
event.key_code = .ENTER;
event.shift_pressed = true;
assert(event.key_code == .ENTER && event.packed != 0);
↗
:: enum

Keys, mouse buttons and gamepad buttons. Printable keys use their ASCII value, upper case for letters (#char "A" is the A key); the rest have names. Index input_button_states with it.

UNKNOWN
BACKSPACE
TAB
LINEFEED
ENTER
ESCAPE
SPACEBAR
DELETE
ARROW_UP
ARROW_DOWN
ARROW_LEFT
ARROW_RIGHT
PAGE_UP
PAGE_DOWN
HOME
END
INSERT
PAUSE
SCROLL_LOCK
ALT
CTRL
SHIFT
CMD
META
F1
F2
F3
F4
F5
F6
F7
F8
F9
F10
F11
F12
F13
F14
F15
F16
F17
F18
F19
F20
F21
F22
F23
F24
PRINT_SCREEN
MOUSE_BUTTON_LEFT
MOUSE_BUTTON_MIDDLE
MOUSE_BUTTON_RIGHT
MOUSE_WHEEL_UP
MOUSE_WHEEL_DOWN
MOUSE_BUTTON_EXTRA_1
MOUSE_BUTTON_EXTRA_2
GAMEPAD_0_BEGIN
GAMEPAD_0_END
GAMEPAD_1_BEGIN
GAMEPAD_1_END
GAMEPAD_2_BEGIN
GAMEPAD_2_END
GAMEPAD_3_BEGIN
GAMEPAD_3_END
TOUCH
assert(cast(u32) Key_Code.ENTER == 13);
assert(cast(Key_Code) #char "A" != .UNKNOWN);
↗

Constants#