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

[3.x] Allow for mapping scancodes to current layout #56015

Merged
merged 1 commit into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/bind/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,10 @@ String _OS::keyboard_get_layout_name(int p_index) const {
return OS::get_singleton()->keyboard_get_layout_name(p_index);
}

uint32_t _OS::keyboard_get_scancode_from_physical(uint32_t p_scancode) const {
return OS::get_singleton()->keyboard_get_scancode_from_physical(p_scancode);
}

String _OS::get_model_name() const {
return OS::get_singleton()->get_model_name();
}
Expand Down Expand Up @@ -1354,6 +1358,7 @@ void _OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("keyboard_set_current_layout", "index"), &_OS::keyboard_set_current_layout);
ClassDB::bind_method(D_METHOD("keyboard_get_layout_language", "index"), &_OS::keyboard_get_layout_language);
ClassDB::bind_method(D_METHOD("keyboard_get_layout_name", "index"), &_OS::keyboard_get_layout_name);
ClassDB::bind_method(D_METHOD("keyboard_get_scancode_from_physical", "scancode"), &_OS::keyboard_get_scancode_from_physical);

ClassDB::bind_method(D_METHOD("can_draw"), &_OS::can_draw);
ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &_OS::is_userfs_persistent);
Expand Down
1 change: 1 addition & 0 deletions core/bind/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class _OS : public Object {
void keyboard_set_current_layout(int p_index);
String keyboard_get_layout_language(int p_index) const;
String keyboard_get_layout_name(int p_index) const;
uint32_t keyboard_get_scancode_from_physical(uint32_t p_scancode) const;

String get_model_name() const;

Expand Down
4 changes: 4 additions & 0 deletions core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ int OS::get_virtual_keyboard_height() const {
return 0;
}

uint32_t OS::keyboard_get_scancode_from_physical(uint32_t p_scancode) const {
return p_scancode;
}

void OS::set_cursor_shape(CursorShape p_shape) {
}

Expand Down
1 change: 1 addition & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ class OS {

// returns height of the currently shown virtual keyboard (0 if keyboard is hidden)
virtual int get_virtual_keyboard_height() const;
virtual uint32_t keyboard_get_scancode_from_physical(uint32_t p_scancode) const;

virtual void set_cursor_shape(CursorShape p_shape);
virtual CursorShape get_cursor_shape() const;
Expand Down
8 changes: 8 additions & 0 deletions doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,14 @@
[b]Note:[/b] This method is implemented on Linux, macOS and Windows.
</description>
</method>
<method name="keyboard_get_scancode_from_physical" qualifiers="const">
<return type="int" />
<argument index="0" name="scancode" type="int" />
<description>
Converts a physical (US QWERTY) [code]scancode[/code] to one in the active keyboard layout.
[b]Note:[/b] This method is implemented on Linux, macOS and Windows.
</description>
</method>
<method name="keyboard_set_current_layout">
<return type="void" />
<argument index="0" name="index" type="int" />
Expand Down
1 change: 1 addition & 0 deletions platform/osx/os_osx.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class OS_OSX : public OS_Unix {
virtual void keyboard_set_current_layout(int p_index);
virtual String keyboard_get_layout_language(int p_index) const;
virtual String keyboard_get_layout_name(int p_index) const;
virtual uint32_t keyboard_get_scancode_from_physical(uint32_t p_scancode) const;

virtual void move_window_to_foreground();

Expand Down
288 changes: 154 additions & 134 deletions platform/osx/os_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -932,145 +932,154 @@ static bool isNumpadKey(unsigned int key) {
return false;
}

// Keyboard symbol translation table
static const unsigned int _osx_to_godot_table[128] = {
/* 00 */ KEY_A,
/* 01 */ KEY_S,
/* 02 */ KEY_D,
/* 03 */ KEY_F,
/* 04 */ KEY_H,
/* 05 */ KEY_G,
/* 06 */ KEY_Z,
/* 07 */ KEY_X,
/* 08 */ KEY_C,
/* 09 */ KEY_V,
/* 0a */ KEY_SECTION, /* ISO Section */
/* 0b */ KEY_B,
/* 0c */ KEY_Q,
/* 0d */ KEY_W,
/* 0e */ KEY_E,
/* 0f */ KEY_R,
/* 10 */ KEY_Y,
/* 11 */ KEY_T,
/* 12 */ KEY_1,
/* 13 */ KEY_2,
/* 14 */ KEY_3,
/* 15 */ KEY_4,
/* 16 */ KEY_6,
/* 17 */ KEY_5,
/* 18 */ KEY_EQUAL,
/* 19 */ KEY_9,
/* 1a */ KEY_7,
/* 1b */ KEY_MINUS,
/* 1c */ KEY_8,
/* 1d */ KEY_0,
/* 1e */ KEY_BRACERIGHT,
/* 1f */ KEY_O,
/* 20 */ KEY_U,
/* 21 */ KEY_BRACELEFT,
/* 22 */ KEY_I,
/* 23 */ KEY_P,
/* 24 */ KEY_ENTER,
/* 25 */ KEY_L,
/* 26 */ KEY_J,
/* 27 */ KEY_APOSTROPHE,
/* 28 */ KEY_K,
/* 29 */ KEY_SEMICOLON,
/* 2a */ KEY_BACKSLASH,
/* 2b */ KEY_COMMA,
/* 2c */ KEY_SLASH,
/* 2d */ KEY_N,
/* 2e */ KEY_M,
/* 2f */ KEY_PERIOD,
/* 30 */ KEY_TAB,
/* 31 */ KEY_SPACE,
/* 32 */ KEY_QUOTELEFT,
/* 33 */ KEY_BACKSPACE,
/* 34 */ KEY_UNKNOWN,
/* 35 */ KEY_ESCAPE,
/* 36 */ KEY_META,
/* 37 */ KEY_META,
/* 38 */ KEY_SHIFT,
/* 39 */ KEY_CAPSLOCK,
/* 3a */ KEY_ALT,
/* 3b */ KEY_CONTROL,
/* 3c */ KEY_SHIFT,
/* 3d */ KEY_ALT,
/* 3e */ KEY_CONTROL,
/* 3f */ KEY_UNKNOWN, /* Function */
/* 40 */ KEY_UNKNOWN, /* F17 */
/* 41 */ KEY_KP_PERIOD,
/* 42 */ KEY_UNKNOWN,
/* 43 */ KEY_KP_MULTIPLY,
/* 44 */ KEY_UNKNOWN,
/* 45 */ KEY_KP_ADD,
/* 46 */ KEY_UNKNOWN,
/* 47 */ KEY_NUMLOCK, /* Really KeypadClear... */
/* 48 */ KEY_VOLUMEUP, /* VolumeUp */
/* 49 */ KEY_VOLUMEDOWN, /* VolumeDown */
/* 4a */ KEY_VOLUMEMUTE, /* Mute */
/* 4b */ KEY_KP_DIVIDE,
/* 4c */ KEY_KP_ENTER,
/* 4d */ KEY_UNKNOWN,
/* 4e */ KEY_KP_SUBTRACT,
/* 4f */ KEY_UNKNOWN, /* F18 */
/* 50 */ KEY_UNKNOWN, /* F19 */
/* 51 */ KEY_EQUAL, /* KeypadEqual */
/* 52 */ KEY_KP_0,
/* 53 */ KEY_KP_1,
/* 54 */ KEY_KP_2,
/* 55 */ KEY_KP_3,
/* 56 */ KEY_KP_4,
/* 57 */ KEY_KP_5,
/* 58 */ KEY_KP_6,
/* 59 */ KEY_KP_7,
/* 5a */ KEY_UNKNOWN, /* F20 */
/* 5b */ KEY_KP_8,
/* 5c */ KEY_KP_9,
/* 5d */ KEY_YEN, /* JIS Yen */
/* 5e */ KEY_UNDERSCORE, /* JIS Underscore */
/* 5f */ KEY_COMMA, /* JIS KeypadComma */
/* 60 */ KEY_F5,
/* 61 */ KEY_F6,
/* 62 */ KEY_F7,
/* 63 */ KEY_F3,
/* 64 */ KEY_F8,
/* 65 */ KEY_F9,
/* 66 */ KEY_UNKNOWN, /* JIS Eisu */
/* 67 */ KEY_F11,
/* 68 */ KEY_UNKNOWN, /* JIS Kana */
/* 69 */ KEY_F13,
/* 6a */ KEY_F16,
/* 6b */ KEY_F14,
/* 6c */ KEY_UNKNOWN,
/* 6d */ KEY_F10,
/* 6e */ KEY_MENU,
/* 6f */ KEY_F12,
/* 70 */ KEY_UNKNOWN,
/* 71 */ KEY_F15,
/* 72 */ KEY_INSERT, /* Really Help... */
/* 73 */ KEY_HOME,
/* 74 */ KEY_PAGEUP,
/* 75 */ KEY_DELETE,
/* 76 */ KEY_F4,
/* 77 */ KEY_END,
/* 78 */ KEY_F2,
/* 79 */ KEY_PAGEDOWN,
/* 7a */ KEY_F1,
/* 7b */ KEY_LEFT,
/* 7c */ KEY_RIGHT,
/* 7d */ KEY_DOWN,
/* 7e */ KEY_UP,
/* 7f */ KEY_UNKNOWN,
};

// Translates a OS X keycode to a Godot keycode
//
static int translateKey(unsigned int key) {
// Keyboard symbol translation table
static const unsigned int table[128] = {
/* 00 */ KEY_A,
/* 01 */ KEY_S,
/* 02 */ KEY_D,
/* 03 */ KEY_F,
/* 04 */ KEY_H,
/* 05 */ KEY_G,
/* 06 */ KEY_Z,
/* 07 */ KEY_X,
/* 08 */ KEY_C,
/* 09 */ KEY_V,
/* 0a */ KEY_SECTION, /* ISO Section */
/* 0b */ KEY_B,
/* 0c */ KEY_Q,
/* 0d */ KEY_W,
/* 0e */ KEY_E,
/* 0f */ KEY_R,
/* 10 */ KEY_Y,
/* 11 */ KEY_T,
/* 12 */ KEY_1,
/* 13 */ KEY_2,
/* 14 */ KEY_3,
/* 15 */ KEY_4,
/* 16 */ KEY_6,
/* 17 */ KEY_5,
/* 18 */ KEY_EQUAL,
/* 19 */ KEY_9,
/* 1a */ KEY_7,
/* 1b */ KEY_MINUS,
/* 1c */ KEY_8,
/* 1d */ KEY_0,
/* 1e */ KEY_BRACERIGHT,
/* 1f */ KEY_O,
/* 20 */ KEY_U,
/* 21 */ KEY_BRACELEFT,
/* 22 */ KEY_I,
/* 23 */ KEY_P,
/* 24 */ KEY_ENTER,
/* 25 */ KEY_L,
/* 26 */ KEY_J,
/* 27 */ KEY_APOSTROPHE,
/* 28 */ KEY_K,
/* 29 */ KEY_SEMICOLON,
/* 2a */ KEY_BACKSLASH,
/* 2b */ KEY_COMMA,
/* 2c */ KEY_SLASH,
/* 2d */ KEY_N,
/* 2e */ KEY_M,
/* 2f */ KEY_PERIOD,
/* 30 */ KEY_TAB,
/* 31 */ KEY_SPACE,
/* 32 */ KEY_QUOTELEFT,
/* 33 */ KEY_BACKSPACE,
/* 34 */ KEY_UNKNOWN,
/* 35 */ KEY_ESCAPE,
/* 36 */ KEY_META,
/* 37 */ KEY_META,
/* 38 */ KEY_SHIFT,
/* 39 */ KEY_CAPSLOCK,
/* 3a */ KEY_ALT,
/* 3b */ KEY_CONTROL,
/* 3c */ KEY_SHIFT,
/* 3d */ KEY_ALT,
/* 3e */ KEY_CONTROL,
/* 3f */ KEY_UNKNOWN, /* Function */
/* 40 */ KEY_UNKNOWN, /* F17 */
/* 41 */ KEY_KP_PERIOD,
/* 42 */ KEY_UNKNOWN,
/* 43 */ KEY_KP_MULTIPLY,
/* 44 */ KEY_UNKNOWN,
/* 45 */ KEY_KP_ADD,
/* 46 */ KEY_UNKNOWN,
/* 47 */ KEY_NUMLOCK, /* Really KeypadClear... */
/* 48 */ KEY_VOLUMEUP, /* VolumeUp */
/* 49 */ KEY_VOLUMEDOWN, /* VolumeDown */
/* 4a */ KEY_VOLUMEMUTE, /* Mute */
/* 4b */ KEY_KP_DIVIDE,
/* 4c */ KEY_KP_ENTER,
/* 4d */ KEY_UNKNOWN,
/* 4e */ KEY_KP_SUBTRACT,
/* 4f */ KEY_UNKNOWN, /* F18 */
/* 50 */ KEY_UNKNOWN, /* F19 */
/* 51 */ KEY_EQUAL, /* KeypadEqual */
/* 52 */ KEY_KP_0,
/* 53 */ KEY_KP_1,
/* 54 */ KEY_KP_2,
/* 55 */ KEY_KP_3,
/* 56 */ KEY_KP_4,
/* 57 */ KEY_KP_5,
/* 58 */ KEY_KP_6,
/* 59 */ KEY_KP_7,
/* 5a */ KEY_UNKNOWN, /* F20 */
/* 5b */ KEY_KP_8,
/* 5c */ KEY_KP_9,
/* 5d */ KEY_YEN, /* JIS Yen */
/* 5e */ KEY_UNDERSCORE, /* JIS Underscore */
/* 5f */ KEY_COMMA, /* JIS KeypadComma */
/* 60 */ KEY_F5,
/* 61 */ KEY_F6,
/* 62 */ KEY_F7,
/* 63 */ KEY_F3,
/* 64 */ KEY_F8,
/* 65 */ KEY_F9,
/* 66 */ KEY_UNKNOWN, /* JIS Eisu */
/* 67 */ KEY_F11,
/* 68 */ KEY_UNKNOWN, /* JIS Kana */
/* 69 */ KEY_F13,
/* 6a */ KEY_F16,
/* 6b */ KEY_F14,
/* 6c */ KEY_UNKNOWN,
/* 6d */ KEY_F10,
/* 6e */ KEY_MENU,
/* 6f */ KEY_F12,
/* 70 */ KEY_UNKNOWN,
/* 71 */ KEY_F15,
/* 72 */ KEY_INSERT, /* Really Help... */
/* 73 */ KEY_HOME,
/* 74 */ KEY_PAGEUP,
/* 75 */ KEY_DELETE,
/* 76 */ KEY_F4,
/* 77 */ KEY_END,
/* 78 */ KEY_F2,
/* 79 */ KEY_PAGEDOWN,
/* 7a */ KEY_F1,
/* 7b */ KEY_LEFT,
/* 7c */ KEY_RIGHT,
/* 7d */ KEY_DOWN,
/* 7e */ KEY_UP,
/* 7f */ KEY_UNKNOWN,
};

if (key >= 128)
return KEY_UNKNOWN;

return table[key];
return _osx_to_godot_table[key];
}

// Translates a Godot keycode back to a OSX keycode
static unsigned int unmapKey(int key) {
for (int i = 0; i <= 126; i++) {
if (_osx_to_godot_table[i] == key) {
return i;
}
}
return 127;
}

struct _KeyCodeMap {
Expand Down Expand Up @@ -3207,6 +3216,17 @@ void _update_keyboard_layouts() {
return kbd_layouts[p_index].name;
}

uint32_t OS_OSX::keyboard_get_scancode_from_physical(uint32_t p_scancode) const {
if (p_scancode == KEY_PAUSE) {
return p_scancode;
}

unsigned int modifiers = p_scancode & KEY_MODIFIER_MASK;
unsigned int scancode_no_mod = p_scancode & KEY_CODE_MASK;
unsigned int osx_scancode = unmapKey((uint32_t)scancode_no_mod);
return (uint32_t)(remapKey(osx_scancode, 0) | modifiers);
}

void OS_OSX::process_events() {
while (true) {
NSEvent *event = [NSApp
Expand Down
10 changes: 10 additions & 0 deletions platform/windows/key_mapping_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,16 @@ unsigned int KeyMappingWindows::get_keysym(unsigned int p_code) {
return KEY_UNKNOWN;
}

unsigned int KeyMappingWindows::get_scancode(unsigned int p_keycode) {
for (int i = 0; _scancode_to_keycode[i].keysym != KEY_UNKNOWN; i++) {
if (_scancode_to_keycode[i].keysym == p_keycode) {
return _scancode_to_keycode[i].keycode;
}
}

return 0;
}

unsigned int KeyMappingWindows::get_scansym(unsigned int p_code, bool p_extended) {
unsigned int keycode = KEY_UNKNOWN;
for (int i = 0; _scancode_to_keycode[i].keysym != KEY_UNKNOWN; i++) {
Expand Down
Loading