@@ -172,9 +172,85 @@ void fossil_io_show_progress(int progress);
172172 */
173173int fossil_io_gets (char *buffer, size_t size);
174174
175+ /* *
176+ * @brief Type for representing a key binding.
177+ */
178+ typedef void (*fossil_io_action_callback_t )(void );
179+
180+ typedef struct {
181+ int key_code; /* *< The integer key code (e.g., ASCII or special code). */
182+ const char *action; /* *< The action string associated with this key. */
183+ fossil_io_action_callback_t callback; /* *< Optional function to call when key is pressed. */
184+ } fossil_io_keybinding_t ;
185+
186+ /* *
187+ * @brief Registers a new key binding.
188+ *
189+ * Adds a key-action mapping to the input library.
190+ *
191+ * @param key_code The integer key code to bind.
192+ * @param action The action string associated with the key.
193+ * @return 0 on success, non-zero on failure (e.g., duplicate key).
194+ */
195+ int fossil_io_register_keybinding (int key_code, const char *action);
196+
197+ /* *
198+ * @brief Removes an existing key binding.
199+ *
200+ * @param key_code The key code of the binding to remove.
201+ * @return 0 on success, non-zero if the key was not found.
202+ */
203+ int fossil_io_unregister_keybinding (int key_code);
204+
205+ /* *
206+ * Registers a new key binding with optional callback.
207+ */
208+ int fossil_io_register_keybinding_with_callback (int key_code, const char *action, fossil_io_action_callback_t cb);
209+
210+ /* *
211+ * @brief Retrieves the action associated with a key code.
212+ *
213+ * @param key_code The key code to look up.
214+ * @return The action string if found, NULL otherwise.
215+ */
216+ const char * fossil_io_get_keybinding_action (int key_code);
217+
218+ /* *
219+ * @brief Processes a key event.
220+ *
221+ * Checks if a key has a registered action and triggers it if found.
222+ *
223+ * @param key_code The key code that was pressed.
224+ * @return 1 if an action was triggered, 0 if no binding exists.
225+ */
226+ int fossil_io_process_keybinding (int key_code);
227+
228+ /* *
229+ * @brief Lists all currently registered key bindings.
230+ *
231+ * @param bindings Array of fossil_io_keybinding_t to populate.
232+ * @param max_bindings Maximum number of bindings to populate.
233+ * @return Number of bindings populated in the array.
234+ */
235+ size_t fossil_io_list_keybindings (fossil_io_keybinding_t *bindings, size_t max_bindings);
236+
237+ /* *
238+ * @brief Clears all registered key bindings.
239+ *
240+ * Frees any internal memory and removes all key-action mappings.
241+ */
242+ void fossil_io_clear_keybindings (void );
243+
175244#ifdef __cplusplus
176245}
177246
247+ #include < cstddef>
248+ #include < string>
249+ #include < vector>
250+ #include < utility>
251+ #include < cstdarg>
252+ #include < istream>
253+
178254/* *
179255 * Namespace for the Fossil Logic I/O library.
180256 */
@@ -397,6 +473,82 @@ namespace fossil {
397473 return input_stream;
398474 }
399475
476+ /* *
477+ * Registers a key binding with an associated action string.
478+ *
479+ * @param key_code The integer key code.
480+ * @param action The action string to associate with the key.
481+ * @return 0 on success, non-zero on failure.
482+ */
483+ static int register_keybinding (int key_code, const std::string &action) {
484+ return fossil_io_register_keybinding (key_code, action.c_str ());
485+ }
486+
487+ /* *
488+ * Registers a key binding with an optional callback function.
489+ *
490+ * @param key_code The integer key code.
491+ * @param action The action string to associate with the key.
492+ * @param callback Function pointer to execute when the key is pressed.
493+ * @return 0 on success, non-zero on failure.
494+ */
495+ static int register_keybinding_callback (int key_code, const std::string &action, void (*callback)()) {
496+ return fossil_io_register_keybinding_with_callback (key_code, action.c_str (), callback);
497+ }
498+
499+ /* *
500+ * Unregisters a key binding.
501+ *
502+ * @param key_code The key code of the binding to remove.
503+ * @return 0 on success, non-zero if the key was not found.
504+ */
505+ static int unregister_keybinding (int key_code) {
506+ return fossil_io_unregister_keybinding (key_code);
507+ }
508+
509+ /* *
510+ * Processes a key event.
511+ *
512+ * @param key_code The key code to process.
513+ * @return 1 if an action was triggered, 0 if no binding exists.
514+ */
515+ static int process_keybinding (int key_code) {
516+ return fossil_io_process_keybinding (key_code);
517+ }
518+
519+ /* *
520+ * Retrieves the action associated with a key code.
521+ *
522+ * @param key_code The key code to look up.
523+ * @return The action string if found, empty string otherwise.
524+ */
525+ static std::string get_keybinding_action (int key_code) {
526+ const char *action = fossil_io_get_keybinding_action (key_code);
527+ return action ? std::string (action) : std::string ();
528+ }
529+
530+ /* *
531+ * Lists all registered key bindings.
532+ *
533+ * @return A vector of key-action pairs representing all bindings.
534+ */
535+ static std::vector<std::pair<int , std::string>> list_keybindings () {
536+ std::vector<std::pair<int , std::string>> bindings;
537+ fossil_io_keybinding_t arr[256 ];
538+ size_t count = fossil_io_list_keybindings (arr, 256 );
539+ for (size_t i = 0 ; i < count; i++) {
540+ bindings.emplace_back (arr[i].key_code , arr[i].action ? arr[i].action : " " );
541+ }
542+ return bindings;
543+ }
544+
545+ /* *
546+ * Clears all registered key bindings.
547+ */
548+ static void clear_keybindings () {
549+ fossil_io_clear_keybindings ();
550+ }
551+
400552 };
401553
402554 }
0 commit comments