Adds an empty keymap named section_name to the set and returns it. The name is what a [section]
line in a bindings file refers to.
set: Keymap_Set;
map := add_keymap(*set, "editor");
assert(map.section_name == "editor" && map.enabled);↗#import "Keymap";
Key bindings that you can load from a text file.
Build a Keymap_Set with add_keymap, register the procedures a binding may name with
add_proc, load bindings with load_from_memory or load_from_file, then pass each Event from
the Input module to handle_event. It returns true when a binding took it.
After a [1] version line, a binding is a key with optional S-, C- and A- prefixes (shift, control, alt), then
either a procedure name or : and a command string. [name] starts the bindings of a keymap.
#import "Input";
set: Keymap_Set;
map := add_keymap(*set, "editor");
set.default_keymap = map;
add_proc(map, "save", () { /* runs on C-s */ });
assert(load_from_memory(*set, "[1]\n[editor]\nC-s save\n"));
event: Event;
event.type = .KEYBOARD;
event.key_code = cast(Key_Code) #char "S";
event.key_pressed = 1;
event.ctrl_pressed = true;
assert(handle_event(*set, event));
reset_keymaps(*set);Adds an empty keymap named section_name to the set and returns it. The name is what a [section]
line in a bindings file refers to.
set: Keymap_Set;
map := add_keymap(*set, "editor");
assert(map.section_name == "editor" && map.enabled);↗Clears the bindings of every keymap in the set and frees them. Keeps the registered procedures.
Clears the bindings of one keymap and frees them. Keeps the registered procedures.
Turns a keymap on or off. A disabled keymap is skipped by handle_event.
set: Keymap_Set;
map := add_keymap(*set, "game");
enable(*set, map, false);
assert(!map.enabled);↗Registers a procedure under name so bindings can refer to it. Pass a plain procedure for a press,
or one that takes a bool for a key that is held.
set: Keymap_Set;
map := add_keymap(*set, "game");
add_proc(map, "jump", () { /* ... */ });
add_proc(map, "run", (down: bool) { /* ... */ });
assert(map.procs.count == 2);↗Binds key with the given modifiers to a registered procedure (is_proc) or a command string.
Rebinding the same key and modifiers replaces the old binding. Returns whether it worked, and
if not, why. Set respect_ignore_modifier_keys to false to have the binding ignore the keymap's
ignore_modifier_keys setting.
#import "Input";
set: Keymap_Set;
map := add_keymap(*set, "game");
add_proc(map, "jump", () { /* ... */ });
flags: Event.Modifier_Flags;
ok, reason := add_mapping(map, .SPACEBAR, flags, true, "jump", true);
assert(ok);
ok, reason = add_mapping(map, .ENTER, flags, true, "missing", true);
assert(!ok && reason == .UNKNOWN_COMMAND);
reset_keymaps(*set);↗Runs the binding that matches event in the first enabled keymap that has one. Returns true when
one did, so the caller can skip its own handling. Only keyboard and mouse wheel events match.
Like handle_event, but for a single keymap.
Replaces the procedures of dest with those of source, so the same bindings file can load into both.
Makes dest a copy of source: its bindings, procedures and settings. Command strings are copied.
Reads bindings from memory into the set. Returns false if any line was invalid; the valid
ones still apply.
set: Keymap_Set;
map := add_keymap(*set, "game");
set.default_keymap = map;
add_proc(map, "jump", () { /* ... */ });
assert(load_from_memory(*set, "[1]\nSpace jump\nS-F5 jump\n"));
assert(map.mappings.count == 2);
assert(!load_from_memory(*set, "[1]\nNOT_A_KEY jump"));
reset_keymaps(*set);↗Reads bindings from a file into the set, in the format of load_from_memory. Returns false when
the file cannot be read or has an invalid line. With optional, a missing file is not an error.
set: Keymap_Set;
map := add_keymap(*set, "game");
set.default_keymap = map;
add_proc(map, "jump", () { /* ... */ });
load_from_file(*set, "keys", "keys.txt", optional = true);↗This one is only checked to compile, not run.
Parses bindings from an open Text_File_Handler. The load_from_ procedures call it.
Turns a key name or a single character into a Key_Code. Returns false for a name it does not know.
#import "Input";
ok, key := map_string_to_key("F5");
assert(ok && key == .F5);
ok, key = map_string_to_key("a");
assert(ok && key == cast(Key_Code) #char "A");↗The name map_string_to_key accepts for a key.
assert(map_key_to_string(.ENTER) == "Enter");↗Writes the bindings of every keymap in the set in the format load_from_memory reads.
#import "Input";
set: Keymap_Set;
map := add_keymap(*set, "game");
add_proc(map, "jump", () { /* ... */ });
flags: Event.Modifier_Flags;
add_mapping(map, .SPACEBAR, flags, true, "jump", true);
builder: String_Builder;
print_to_builder(*builder, *set);
text := builder_to_string(*builder);
assert(text.count > 0);
free(text);
reset_keymaps(*set);↗Writes only the binding lines of a keymap, without its [section] header.
Writes the bindings of the set to a file, so load_from_file can read them back.
A procedure a key press runs.
A procedure for a key that is held: called with true when the key goes down, false when it is released.
A group of keymaps. handle_event tries each enabled one in turn; default_keymap receives
bindings that appear before any [section] line.
One section of bindings, plus the procedures they can name.
One binding: a key, its modifiers, and the procedure or command string it runs.
Why add_mapping refused a binding.