Kohi Game Engine
keymap.h File Reference

This file contains the structures and functions required to implement keymaps and keybindings, used to translate keyboard input events to events and/or call bound functions automatically. Keymaps replace the need for checks of key states in that they will instead invoke callback functions instead. Maps will be added onto a stack, where bindings are replaced along the way. For example, if the base keymap defines the "escape" key as an application quit, then another keymap added on re-defines the key to nothing while adding a new binding for "a", then "a"'s binding will work, and "escape" will do nothing. If "escape" were left undefined in the second keymap, the original mapping is left unchanged. Maps are pushed/popped as expected on a stack. More...

#include "defines.h"
#include "input.h"

Go to the source code of this file.

Data Structures

struct  keymap_binding
 Represents an individual binding, containing the keybind type, modifiers, callback, potential user data. Doubles as a linked-list node, as keys can have multiple bindings associated with them on the same map. More...
 
struct  keymap_entry
 An individual entry for a keymap, which contains the key to be bound and a linked list of bindings. More...
 
struct  keymap
 A keymap, which holds a list of keymap entries, each with a list of bindings. These are held in an internal stack, and override entries of the keymaps below it in the stack. More...
 

Typedefs

typedef enum keymap_modifier_bits keymap_modifier_bits
 An enumeration of modifier keys required by a keymap's keybinding to be activated. These may be combined (ORed) together to require multiple modifiers. More...
 
typedef u32 keymap_modifier
 A typedef for combined keymap modifiers. More...
 
typedef enum keymap_entry_bind_type keymap_entry_bind_type
 A collection of keymap binding types, which correspond to various types of key input events such as press, release or hold. More...
 
typedef void(* PFN_keybind_callback) (keys key, keymap_entry_bind_type type, keymap_modifier modifiers, void *user_data)
 A typedef of a keybinding callback to be made when a keybinding is activated. More...
 
typedef struct keymap_binding keymap_binding
 Represents an individual binding, containing the keybind type, modifiers, callback, potential user data. Doubles as a linked-list node, as keys can have multiple bindings associated with them on the same map. More...
 
typedef struct keymap_entry keymap_entry
 An individual entry for a keymap, which contains the key to be bound and a linked list of bindings. More...
 
typedef struct keymap keymap
 A keymap, which holds a list of keymap entries, each with a list of bindings. These are held in an internal stack, and override entries of the keymaps below it in the stack. More...
 

Enumerations

enum  keymap_modifier_bits { KEYMAP_MODIFIER_NONE_BIT = 0x0 , KEYMAP_MODIFIER_SHIFT_BIT = 0x1 , KEYMAP_MODIFIER_CONTROL_BIT = 0x2 , KEYMAP_MODIFIER_ALT_BIT = 0x4 }
 An enumeration of modifier keys required by a keymap's keybinding to be activated. These may be combined (ORed) together to require multiple modifiers. More...
 
enum  keymap_entry_bind_type {
  KEYMAP_BIND_TYPE_UNDEFINED = 0x0 , KEYMAP_BIND_TYPE_PRESS = 0x1 , KEYMAP_BIND_TYPE_RELEASE = 0x2 , KEYMAP_BIND_TYPE_HOLD = 0x4 ,
  KEYMAP_BIND_TYPE_UNSET = 0x8
}
 A collection of keymap binding types, which correspond to various types of key input events such as press, release or hold. More...
 

Functions

KAPI keymap keymap_create (void)
 Creates and returns a new keymap. More...
 
KAPI void keymap_binding_add (keymap *map, keys key, keymap_entry_bind_type type, keymap_modifier modifiers, void *user_data, PFN_keybind_callback callback)
 Adds a binding to the keymap provided. More...
 
KAPI void keymap_binding_remove (keymap *map, keys key, keymap_entry_bind_type type, keymap_modifier modifiers, PFN_keybind_callback callback)
 Removes the binding from the given keymap that also matches on key, bind type, modifiers and callback. If no match is found, nothing is done. More...
 
KAPI void keymap_clear (keymap *map)
 Clears all bindings from the given keymap. More...
 

Detailed Description

This file contains the structures and functions required to implement keymaps and keybindings, used to translate keyboard input events to events and/or call bound functions automatically. Keymaps replace the need for checks of key states in that they will instead invoke callback functions instead. Maps will be added onto a stack, where bindings are replaced along the way. For example, if the base keymap defines the "escape" key as an application quit, then another keymap added on re-defines the key to nothing while adding a new binding for "a", then "a"'s binding will work, and "escape" will do nothing. If "escape" were left undefined in the second keymap, the original mapping is left unchanged. Maps are pushed/popped as expected on a stack.

Author
Travis Vroman (travi.nosp@m.s@ko.nosp@m.hieng.nosp@m.ine..nosp@m.com)
Version
1.0
Date
2023-01-18

Typedef Documentation

◆ keymap

typedef struct keymap keymap

A keymap, which holds a list of keymap entries, each with a list of bindings. These are held in an internal stack, and override entries of the keymaps below it in the stack.

◆ keymap_binding

Represents an individual binding, containing the keybind type, modifiers, callback, potential user data. Doubles as a linked-list node, as keys can have multiple bindings associated with them on the same map.

◆ keymap_entry

typedef struct keymap_entry keymap_entry

An individual entry for a keymap, which contains the key to be bound and a linked list of bindings.

◆ keymap_entry_bind_type

A collection of keymap binding types, which correspond to various types of key input events such as press, release or hold.

◆ keymap_modifier

A typedef for combined keymap modifiers.

◆ keymap_modifier_bits

An enumeration of modifier keys required by a keymap's keybinding to be activated. These may be combined (ORed) together to require multiple modifiers.

◆ PFN_keybind_callback

typedef void(* PFN_keybind_callback) (keys key, keymap_entry_bind_type type, keymap_modifier modifiers, void *user_data)

A typedef of a keybinding callback to be made when a keybinding is activated.

Enumeration Type Documentation

◆ keymap_entry_bind_type

A collection of keymap binding types, which correspond to various types of key input events such as press, release or hold.

Enumerator
KEYMAP_BIND_TYPE_UNDEFINED 

An undefined mapping that can be overridden.

KEYMAP_BIND_TYPE_PRESS 

Callback is made when key is initially pressed.

KEYMAP_BIND_TYPE_RELEASE 

Callback is made when key is released.

KEYMAP_BIND_TYPE_HOLD 

Callback is made continuously while key is held.

KEYMAP_BIND_TYPE_UNSET 

Used to disable a key binding on a lower-level map.

◆ keymap_modifier_bits

An enumeration of modifier keys required by a keymap's keybinding to be activated. These may be combined (ORed) together to require multiple modifiers.

Enumerator
KEYMAP_MODIFIER_NONE_BIT 

The default modifier, means that no modifiers are required.

KEYMAP_MODIFIER_SHIFT_BIT 

A shift key must be pressed for the binding to fire.

KEYMAP_MODIFIER_CONTROL_BIT 

A control(ctrl)/cmd key must be pressed for the binding to fire.

KEYMAP_MODIFIER_ALT_BIT 

A alt/option key must be pressed for the binding to fire.

Function Documentation

◆ keymap_binding_add()

KAPI void keymap_binding_add ( keymap map,
keys  key,
keymap_entry_bind_type  type,
keymap_modifier  modifiers,
void *  user_data,
PFN_keybind_callback  callback 
)

Adds a binding to the keymap provided.

Parameters
mapA pointer to the keymap to add a binding to. Required.
keyThe key to bind to.
typeThe type of binding.
modifiersRequired modifier keys, if any (OR them together).
user_dataUser data, if any. Optional.
callbackThe callback function pointer. Required.

◆ keymap_binding_remove()

KAPI void keymap_binding_remove ( keymap map,
keys  key,
keymap_entry_bind_type  type,
keymap_modifier  modifiers,
PFN_keybind_callback  callback 
)

Removes the binding from the given keymap that also matches on key, bind type, modifiers and callback. If no match is found, nothing is done.

Parameters
mapA pointer to the keymap to remove the binding from. Required.
keyThe key to bind to.
typeThe type of binding.
modifiersThe modifiers required for the binding.
callbackThe callback to be made from the biding. Required.

◆ keymap_clear()

KAPI void keymap_clear ( keymap map)

Clears all bindings from the given keymap.

Parameters
mapA pointer to the map to be cleared.

◆ keymap_create()

KAPI keymap keymap_create ( void  )

Creates and returns a new keymap.