From a5be58d6d3eb90207d6e4cda891218b2b1147207 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Mon, 25 Mar 2024 23:10:16 -0400 Subject: [PATCH] Can get input key from string. --- seika/input/input.c | 9 +++++++++ seika/input/input.h | 1 + test/test.c | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/seika/input/input.c b/seika/input/input.c index 9ed3d1f..caa9d9e 100644 --- a/seika/input/input.c +++ b/seika/input/input.c @@ -210,6 +210,15 @@ const char* ska_input_key_to_string(SkaInputKey key) { return keyNames[key]; } +SkaInputKey ska_input_string_to_key(const char* keyName) { + for (SkaInputKey key = 0; key < SkaInputKey_NUMBER_OF_KEYS; ++key) { + if (strcmp(keyName, ska_input_key_to_string(key)) == 0) { + return key; + } + } + return SkaInputKey_INVALID; // Return invalid if not found +} + bool ska_input_is_keyboard_key(SkaInputKey key) { return key >= SKA_INPUT_KEY_KEYBOARD_ENUM_START && key <= SKA_INPUT_KEY_KEYBOARD_ENUM_END; } diff --git a/seika/input/input.h b/seika/input/input.h index c569a7a..c285003 100644 --- a/seika/input/input.h +++ b/seika/input/input.h @@ -251,6 +251,7 @@ typedef struct SkaInputAction { } SkaInputAction; const char* ska_input_key_to_string(SkaInputKey key); +SkaInputKey ska_input_string_to_key(const char* keyName); bool ska_input_is_keyboard_key(SkaInputKey key); bool ska_input_is_mouse_key(SkaInputKey key); diff --git a/test/test.c b/test/test.c index 1458a8e..2d09753 100644 --- a/test/test.c +++ b/test/test.c @@ -62,6 +62,11 @@ void seika_input_test(void) { TEST_ASSERT_TRUE(ska_input_is_mouse_key(key)); } + const char* backspaceKeyName = ska_input_key_to_string(SkaInputKey_KEYBOARD_BACKSPACE); + const SkaInputKey backSpaceKey = ska_input_string_to_key(backspaceKeyName); + TEST_ASSERT_EQUAL_STRING("Backspace", backspaceKeyName); + TEST_ASSERT_EQUAL_INT(SkaInputKey_KEYBOARD_BACKSPACE, backSpaceKey); + TEST_MESSAGE("Testing events"); ska_input_register_input_event3(SkaInputSourceType_KEYBOARD, SkaInputKey_KEYBOARD_BACKSPACE, SkaInputTriggerType_PRESSED); TEST_ASSERT_TRUE(ska_input_is_key_pressed(SkaInputKey_KEYBOARD_BACKSPACE, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));