Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Android keycode translation and duplicate key constants #3733

Merged
merged 5 commits into from
Jan 20, 2024

Conversation

M374LX
Copy link
Contributor

@M374LX M374LX commented Jan 16, 2024

Fixes #3729
Fixes #3734

All of the keys I am able to test are correct. However, I cannot test the keypad (because I use a tenkeyless keyboard), the keyboard menu key (because my keyboard lacks it) and the Android menu key (because my phone lacks it and also because of #3734). Additionally, the Super (Windows) and Print Screen keys are grabbed by the system and apparently cannot be caught by the app.

Note: there seems to be an issue (already present, not introduced by this PR) in which the app freezes on Android if a keyboard is connected after the app starts. For this reason, I recommend testers to connect a keyboard and force stop the test app before starting it (fixed by including keyboard in the manifest's android:configChanges).

Update: with 1df491c, #3734 is also fixed, but it introduces a breaking change, as the value of KEY_MENU is changed.

Code example

#include <raylib.h>
#include <stdio.h>

static const char* get_key_name(int key);

int main()
{
	int last_key = 0;

	InitWindow(800, 600, "Android Keyboard Test");

	while (!WindowShouldClose()) {
		int pressed_key = GetKeyPressed();
		char str[64] = "";

		if (pressed_key != KEY_NULL) {
			last_key = pressed_key;
		}


		BeginDrawing();
		ClearBackground(GRAY);

		sprintf(str, "%d - %s", last_key, get_key_name(last_key));
		DrawText(str, 16, 16, 32, BLACK);

		sprintf(str, "%d", (int)GetTime());
		DrawText(str, 16, 64, 32, BLACK);

		EndDrawing();
	}

	CloseWindow();

	return 0;
}

static const char* get_key_name(int key)
{
	switch (key) {
		case KEY_APOSTROPHE:
			return "Apostrophe";

		case KEY_COMMA:
			return "Comma";

		case KEY_MINUS:
			return "Minus";

		case KEY_PERIOD:
			return "Period";

		case KEY_SLASH:
			return "Slash";

		case KEY_ZERO:
			return "0";

		case KEY_ONE:
			return "1";

		case KEY_TWO:
			return "2";

		case KEY_THREE:
			return "3";

		case KEY_FOUR:
			return "4";

		case KEY_FIVE:
			return "5";

		case KEY_SIX:
			return "6";

		case KEY_SEVEN:
			return "7";

		case KEY_EIGHT:
			return "8";

		case KEY_NINE:
			return "9";

		case KEY_SEMICOLON:
			return "Semicolon";

		case KEY_EQUAL:
			return "Equal";

		case KEY_A:
			return "A";

		case KEY_B:
			return "B";

		case KEY_C:
			return "C";

		case KEY_D:
			return "D";

		case KEY_E:
			return "E";

		case KEY_F:
			return "F";

		case KEY_G:
			return "G";

		case KEY_H:
			return "H";

		case KEY_I:
			return "I";

		case KEY_J:
			return "J";

		case KEY_K:
			return "K";

		case KEY_L:
			return "L";

		case KEY_M:
			return "M";

		case KEY_N:
			return "N";

		case KEY_O:
			return "O";

		case KEY_P:
			return "P";

		case KEY_Q:
			return "Q";

		case KEY_R:
			return "R";

		case KEY_S:
			return "S";

		case KEY_T:
			return "T";

		case KEY_U:
			return "U";

		case KEY_V:
			return "V";

		case KEY_W:
			return "W";

		case KEY_X:
			return "X";

		case KEY_Y:
			return "Y";

		case KEY_Z:
			return "Z";

		case KEY_LEFT_BRACKET:
			return "Left bracket";

		case KEY_BACKSLASH:
			return "Backslash";

		case KEY_RIGHT_BRACKET:
			return "Right bracket";

		case KEY_GRAVE:
			return "Grave";

		case KEY_SPACE:
			return "Space";

		case KEY_ESCAPE:
			return "Escape";

		case KEY_ENTER:
			return "Enter";

		case KEY_TAB:
			return "Tab";

		case KEY_BACKSPACE:
			return "Backspace";

		case KEY_INSERT:
			return "Insert";

		case KEY_DELETE:
			return "Delete";

		case KEY_RIGHT:
			return "Right";

		case KEY_LEFT:
			return "Left";

		case KEY_DOWN:
			return "Down";

		case KEY_UP:
			return "Up";

		case KEY_PAGE_UP:
			return "Page Up";

		case KEY_PAGE_DOWN:
			return "Page Down";

		case KEY_HOME:
			return "Home";

		case KEY_END:
			return "End";

		case KEY_CAPS_LOCK:
			return "Caps Lock";

		case KEY_SCROLL_LOCK:
			return "Scroll Lock";

		case KEY_NUM_LOCK:
			return "Num Lock";

		case KEY_PRINT_SCREEN:
			return "Print Screen";

		case KEY_PAUSE:
			return "Pause";

		case KEY_F1:
			return "F1";

		case KEY_F2:
			return "F2";

		case KEY_F3:
			return "F3";

		case KEY_F4:
			return "F4";

		case KEY_F5:
			return "F5";

		case KEY_F6:
			return "F6";

		case KEY_F7:
			return "F7";

		case KEY_F8:
			return "F8";

		case KEY_F9:
			return "F9";

		case KEY_F10:
			return "F10";

		case KEY_F11:
			return "F11";

		case KEY_F12:
			return "F12";

		case KEY_LEFT_SHIFT:
			return "Left Shift";

		case KEY_LEFT_CONTROL:
			return "Left Ctrl";

		case KEY_LEFT_ALT:
			return "Left Alt";

		case KEY_LEFT_SUPER:
			return "Left Super";

		case KEY_RIGHT_SHIFT:
			return "Right Shift";

		case KEY_RIGHT_CONTROL:
			return "Right Ctrl";

		case KEY_RIGHT_ALT:
			return "Right Alt";

		case KEY_RIGHT_SUPER:
			return "Right Super";

		case KEY_KP_0:
			return "Keypad 0";

		case KEY_KP_1:
			return "Keypad 1";

		case KEY_KP_2:
			return "Keypad 2";

		case KEY_KP_3:
			return "Keypad 3";

		case KEY_KP_4:
			return "Keypad 4";

		case KEY_KP_5:
			return "Keypad 5";

		case KEY_KP_6:
			return "Keypad 6";

		case KEY_KP_7:
			return "Keypad 7";

		case KEY_KP_8:
			return "Keypad 8";

		case KEY_KP_9:
			return "Keypad 9";

		case KEY_KP_DECIMAL:
			return "Keypad Decimal";

		case KEY_KP_DIVIDE:
			return "Keypad Divide";

		case KEY_KP_MULTIPLY:
			return "Keypad Multiply";

		case KEY_KP_SUBTRACT:
			return "Keypad Subtract";

		case KEY_KP_ADD:
			return "Keypad Add";

		case KEY_KP_ENTER:
			return "Keypad Enter";

		case KEY_KP_EQUAL:
			return "Keypad Equal";

		case KEY_BACK:
			return "Android Back";

		case KEY_MENU:
			return "Android Menu";

		case KEY_VOLUME_UP:
			return "Android Volume Up";

		case KEY_VOLUME_DOWN:
			return "Android Volume Down";

		default:
			return "Unknown";
    }
}

@M374LX M374LX marked this pull request as ready for review January 16, 2024 01:39
@M374LX M374LX changed the title Fix Android keycode translation Fix Android keycode translation and duplicate key constants Jan 18, 2024
@raysan5 raysan5 merged commit 5c25913 into raysan5:master Jan 20, 2024
@raysan5
Copy link
Owner

raysan5 commented Jan 20, 2024

@M374LX Thanks for the review with the Android keycodes remap. Just noted that latest keycodes.h includes about 316 possible keys... but no need to add them all to the mapping right now.

About the breaking change... I think we can assume that risk...

@M374LX M374LX deleted the android-keyboard-fix branch February 29, 2024 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[raylib.h] Duplicate key values: KEY_R and KEY_MENU [rcore_android] Android keycodes not translated
2 participants