Skip to content

Commit

Permalink
Can get input key from string.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chukobyte committed Mar 26, 2024
1 parent d0e0f23 commit a5be58d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions seika/input/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions seika/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit a5be58d

Please sign in to comment.