Skip to content

Commit

Permalink
Rename scancode to keycode.
Browse files Browse the repository at this point in the history
Add `physical_keycode` (keyboard layout independent keycodes) to InputEventKey and InputMap.
Fix non-latin keyboard layout keycodes on Linux/X11 (fallback to physical keycodes).
  • Loading branch information
bruvzg committed Feb 25, 2020
1 parent 376a825 commit 1af06d3
Show file tree
Hide file tree
Showing 78 changed files with 736 additions and 299 deletions.
14 changes: 8 additions & 6 deletions core/bind/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,15 +1113,17 @@ String _OS::get_system_dir(SystemDir p_dir) const {
return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir));
}

String _OS::get_scancode_string(uint32_t p_code) const {
String _OS::get_keycode_string(uint32_t p_code) const {

return keycode_get_string(p_code);
}
bool _OS::is_scancode_unicode(uint32_t p_unicode) const {

bool _OS::is_keycode_unicode(uint32_t p_unicode) const {

return keycode_has_unicode(p_unicode);
}
int _OS::find_scancode_from_string(const String &p_code) const {

int _OS::find_keycode_from_string(const String &p_code) const {

return find_keycode(p_code);
}
Expand Down Expand Up @@ -1312,9 +1314,9 @@ void _OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("native_video_pause"), &_OS::native_video_pause);
ClassDB::bind_method(D_METHOD("native_video_unpause"), &_OS::native_video_unpause);

ClassDB::bind_method(D_METHOD("get_scancode_string", "code"), &_OS::get_scancode_string);
ClassDB::bind_method(D_METHOD("is_scancode_unicode", "code"), &_OS::is_scancode_unicode);
ClassDB::bind_method(D_METHOD("find_scancode_from_string", "string"), &_OS::find_scancode_from_string);
ClassDB::bind_method(D_METHOD("get_keycode_string", "code"), &_OS::get_keycode_string);
ClassDB::bind_method(D_METHOD("is_keycode_unicode", "code"), &_OS::is_keycode_unicode);
ClassDB::bind_method(D_METHOD("find_keycode_from_string", "string"), &_OS::find_keycode_from_string);

ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &_OS::set_use_file_access_save_and_swap);

Expand Down
6 changes: 3 additions & 3 deletions core/bind/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ class _OS : public Object {

String get_unique_id() const;

String get_scancode_string(uint32_t p_code) const;
bool is_scancode_unicode(uint32_t p_unicode) const;
int find_scancode_from_string(const String &p_code) const;
String get_keycode_string(uint32_t p_code) const;
bool is_keycode_unicode(uint32_t p_unicode) const;
int find_keycode_from_string(const String &p_code) const;

void set_use_file_access_save_and_swap(bool p_enable);

Expand Down
30 changes: 15 additions & 15 deletions core/input_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,76 +256,76 @@ void InputMap::load_default() {

add_action("ui_accept");
key.instance();
key->set_scancode(KEY_ENTER);
key->set_keycode(KEY_ENTER);
action_add_event("ui_accept", key);

key.instance();
key->set_scancode(KEY_KP_ENTER);
key->set_keycode(KEY_KP_ENTER);
action_add_event("ui_accept", key);

key.instance();
key->set_scancode(KEY_SPACE);
key->set_keycode(KEY_SPACE);
action_add_event("ui_accept", key);

add_action("ui_select");
key.instance();
key->set_scancode(KEY_SPACE);
key->set_keycode(KEY_SPACE);
action_add_event("ui_select", key);

add_action("ui_cancel");
key.instance();
key->set_scancode(KEY_ESCAPE);
key->set_keycode(KEY_ESCAPE);
action_add_event("ui_cancel", key);

add_action("ui_focus_next");
key.instance();
key->set_scancode(KEY_TAB);
key->set_keycode(KEY_TAB);
action_add_event("ui_focus_next", key);

add_action("ui_focus_prev");
key.instance();
key->set_scancode(KEY_TAB);
key->set_keycode(KEY_TAB);
key->set_shift(true);
action_add_event("ui_focus_prev", key);

add_action("ui_left");
key.instance();
key->set_scancode(KEY_LEFT);
key->set_keycode(KEY_LEFT);
action_add_event("ui_left", key);

add_action("ui_right");
key.instance();
key->set_scancode(KEY_RIGHT);
key->set_keycode(KEY_RIGHT);
action_add_event("ui_right", key);

add_action("ui_up");
key.instance();
key->set_scancode(KEY_UP);
key->set_keycode(KEY_UP);
action_add_event("ui_up", key);

add_action("ui_down");
key.instance();
key->set_scancode(KEY_DOWN);
key->set_keycode(KEY_DOWN);
action_add_event("ui_down", key);

add_action("ui_page_up");
key.instance();
key->set_scancode(KEY_PAGEUP);
key->set_keycode(KEY_PAGEUP);
action_add_event("ui_page_up", key);

add_action("ui_page_down");
key.instance();
key->set_scancode(KEY_PAGEDOWN);
key->set_keycode(KEY_PAGEDOWN);
action_add_event("ui_page_down", key);

add_action("ui_home");
key.instance();
key->set_scancode(KEY_HOME);
key->set_keycode(KEY_HOME);
action_add_event("ui_home", key);

add_action("ui_end");
key.instance();
key->set_scancode(KEY_END);
key->set_keycode(KEY_END);
action_add_event("ui_end", key);

//set("display/window/handheld/orientation", "landscape");
Expand Down
2 changes: 1 addition & 1 deletion core/os/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Input::MouseMode Input::get_mouse_mode() const {

void Input::_bind_methods() {

ClassDB::bind_method(D_METHOD("is_key_pressed", "scancode"), &Input::is_key_pressed);
ClassDB::bind_method(D_METHOD("is_key_pressed", "keycode"), &Input::is_key_pressed);
ClassDB::bind_method(D_METHOD("is_mouse_button_pressed", "button"), &Input::is_mouse_button_pressed);
ClassDB::bind_method(D_METHOD("is_joy_button_pressed", "device", "button"), &Input::is_joy_button_pressed);
ClassDB::bind_method(D_METHOD("is_action_pressed", "action"), &Input::is_action_pressed);
Expand Down
2 changes: 1 addition & 1 deletion core/os/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Input : public Object {

static Input *get_singleton();

virtual bool is_key_pressed(int p_scancode) const = 0;
virtual bool is_key_pressed(int p_keycode) const = 0;
virtual bool is_mouse_button_pressed(int p_button) const = 0;
virtual bool is_joy_button_pressed(int p_device, int p_button) const = 0;
virtual bool is_action_pressed(const StringName &p_action) const = 0;
Expand Down
76 changes: 59 additions & 17 deletions core/os/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,31 @@ bool InputEventKey::is_pressed() const {
return pressed;
}

void InputEventKey::set_scancode(uint32_t p_scancode) {
void InputEventKey::set_keycode(uint32_t p_keycode) {

scancode = p_scancode;
keycode = p_keycode;
}
uint32_t InputEventKey::get_scancode() const {

return scancode;
uint32_t InputEventKey::get_keycode() const {

return keycode;
}

void InputEventKey::set_physical_keycode(uint32_t p_keycode) {

physical_keycode = p_keycode;
}

uint32_t InputEventKey::get_physical_keycode() const {

return physical_keycode;
}

void InputEventKey::set_unicode(uint32_t p_unicode) {

unicode = p_unicode;
}

uint32_t InputEventKey::get_unicode() const {

return unicode;
Expand All @@ -259,14 +271,30 @@ void InputEventKey::set_echo(bool p_enable) {

echo = p_enable;
}

bool InputEventKey::is_echo() const {

return echo;
}

uint32_t InputEventKey::get_scancode_with_modifiers() const {
uint32_t InputEventKey::get_keycode_with_modifiers() const {

uint32_t sc = scancode;
uint32_t sc = keycode;
if (get_control())
sc |= KEY_MASK_CTRL;
if (get_alt())
sc |= KEY_MASK_ALT;
if (get_shift())
sc |= KEY_MASK_SHIFT;
if (get_metakey())
sc |= KEY_MASK_META;

return sc;
}

uint32_t InputEventKey::get_physical_keycode_with_modifiers() const {

uint32_t sc = physical_keycode;
if (get_control())
sc |= KEY_MASK_CTRL;
if (get_alt())
Expand All @@ -281,7 +309,7 @@ uint32_t InputEventKey::get_scancode_with_modifiers() const {

String InputEventKey::as_text() const {

String kc = keycode_get_string(scancode);
String kc = keycode_get_string(keycode);
if (kc == String())
return kc;

Expand All @@ -306,10 +334,18 @@ bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool *p_pressed
if (key.is_null())
return false;

uint32_t code = get_scancode_with_modifiers();
uint32_t event_code = key->get_scancode_with_modifiers();
bool match = false;
if (get_keycode() == 0) {
uint32_t code = get_physical_keycode_with_modifiers();
uint32_t event_code = key->get_physical_keycode_with_modifiers();

bool match = get_scancode() == key->get_scancode() && (!key->is_pressed() || (code & event_code) == code);
match = get_physical_keycode() == key->get_physical_keycode() && (!key->is_pressed() || (code & event_code) == code);
} else {
uint32_t code = get_keycode_with_modifiers();
uint32_t event_code = key->get_keycode_with_modifiers();

match = get_keycode() == key->get_keycode() && (!key->is_pressed() || (code & event_code) == code);
}
if (match) {
if (p_pressed != NULL)
*p_pressed = key->is_pressed();
Expand All @@ -325,8 +361,8 @@ bool InputEventKey::shortcut_match(const Ref<InputEvent> &p_event) const {
if (key.is_null())
return false;

uint32_t code = get_scancode_with_modifiers();
uint32_t event_code = key->get_scancode_with_modifiers();
uint32_t code = get_keycode_with_modifiers();
uint32_t event_code = key->get_keycode_with_modifiers();

return code == event_code;
}
Expand All @@ -335,26 +371,32 @@ void InputEventKey::_bind_methods() {

ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventKey::set_pressed);

ClassDB::bind_method(D_METHOD("set_scancode", "scancode"), &InputEventKey::set_scancode);
ClassDB::bind_method(D_METHOD("get_scancode"), &InputEventKey::get_scancode);
ClassDB::bind_method(D_METHOD("set_keycode", "keycode"), &InputEventKey::set_keycode);
ClassDB::bind_method(D_METHOD("get_keycode"), &InputEventKey::get_keycode);

ClassDB::bind_method(D_METHOD("set_physical_keycode", "physical_keycode"), &InputEventKey::set_physical_keycode);
ClassDB::bind_method(D_METHOD("get_physical_keycode"), &InputEventKey::get_physical_keycode);

ClassDB::bind_method(D_METHOD("set_unicode", "unicode"), &InputEventKey::set_unicode);
ClassDB::bind_method(D_METHOD("get_unicode"), &InputEventKey::get_unicode);

ClassDB::bind_method(D_METHOD("set_echo", "echo"), &InputEventKey::set_echo);

ClassDB::bind_method(D_METHOD("get_scancode_with_modifiers"), &InputEventKey::get_scancode_with_modifiers);
ClassDB::bind_method(D_METHOD("get_keycode_with_modifiers"), &InputEventKey::get_keycode_with_modifiers);
ClassDB::bind_method(D_METHOD("get_physical_keycode_with_modifiers"), &InputEventKey::get_physical_keycode_with_modifiers);

ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
ADD_PROPERTY(PropertyInfo(Variant::INT, "scancode"), "set_scancode", "get_scancode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "keycode"), "set_keycode", "get_keycode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "physical_keycode"), "set_physical_keycode", "get_physical_keycode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "unicode"), "set_unicode", "get_unicode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "echo"), "set_echo", "is_echo");
}

InputEventKey::InputEventKey() {

pressed = false;
scancode = 0;
keycode = 0;
physical_keycode = 0;
unicode = 0; ///unicode
echo = false;
}
Expand Down
13 changes: 9 additions & 4 deletions core/os/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ class InputEventKey : public InputEventWithModifiers {

bool pressed; /// otherwise release

uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks
uint32_t keycode; ///< check keyboard.h , KeyCode enum, without modifier masks
uint32_t physical_keycode;
uint32_t unicode; ///unicode

bool echo; /// true if this is an echo key
Expand All @@ -268,16 +269,20 @@ class InputEventKey : public InputEventWithModifiers {
void set_pressed(bool p_pressed);
virtual bool is_pressed() const;

void set_scancode(uint32_t p_scancode);
uint32_t get_scancode() const;
void set_keycode(uint32_t p_keycode);
uint32_t get_keycode() const;

void set_physical_keycode(uint32_t p_keycode);
uint32_t get_physical_keycode() const;

void set_unicode(uint32_t p_unicode);
uint32_t get_unicode() const;

void set_echo(bool p_enable);
virtual bool is_echo() const;

uint32_t get_scancode_with_modifiers() const;
uint32_t get_keycode_with_modifiers() const;
uint32_t get_physical_keycode_with_modifiers() const;

virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
Expand Down
Loading

0 comments on commit 1af06d3

Please sign in to comment.