Added core input handling classes form the godot engine. They are not in the compile yet, they need some changes.

This commit is contained in:
Relintai 2021-11-15 21:11:19 +01:00
parent 9b78fb066e
commit 748a3ac49a
6 changed files with 3064 additions and 0 deletions

165
core/input/input.cpp Normal file
View File

@ -0,0 +1,165 @@
/*************************************************************************/
/* input.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "input.h"
#include "core/input_map.h"
#include "core/os/os.h"
#include "core/project_settings.h"
#ifdef TOOLS_ENABLED
#include "editor/editor_settings.h"
#endif
Input *Input::singleton = nullptr;
Input *Input::get_singleton() {
return singleton;
}
void Input::set_mouse_mode(MouseMode p_mode) {
ERR_FAIL_INDEX((int)p_mode, 4);
OS::get_singleton()->set_mouse_mode((OS::MouseMode)p_mode);
}
Input::MouseMode Input::get_mouse_mode() const {
return (MouseMode)OS::get_singleton()->get_mouse_mode();
}
void Input::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_key_pressed", "scancode"), &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", "exact"), &Input::is_action_pressed, DEFVAL(false));
ClassDB::bind_method(D_METHOD("is_action_just_pressed", "action", "exact"), &Input::is_action_just_pressed, DEFVAL(false));
ClassDB::bind_method(D_METHOD("is_action_just_released", "action", "exact"), &Input::is_action_just_released, DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_action_strength", "action", "exact"), &Input::get_action_strength, DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_action_raw_strength", "action", "exact"), &Input::get_action_raw_strength, DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_axis", "negative_action", "positive_action"), &Input::get_axis);
ClassDB::bind_method(D_METHOD("get_vector", "negative_x", "positive_x", "negative_y", "positive_y", "deadzone"), &Input::get_vector, DEFVAL(-1.0f));
ClassDB::bind_method(D_METHOD("add_joy_mapping", "mapping", "update_existing"), &Input::add_joy_mapping, DEFVAL(false));
ClassDB::bind_method(D_METHOD("remove_joy_mapping", "guid"), &Input::remove_joy_mapping);
ClassDB::bind_method(D_METHOD("joy_connection_changed", "device", "connected", "name", "guid"), &Input::joy_connection_changed);
ClassDB::bind_method(D_METHOD("is_joy_known", "device"), &Input::is_joy_known);
ClassDB::bind_method(D_METHOD("get_joy_axis", "device", "axis"), &Input::get_joy_axis);
ClassDB::bind_method(D_METHOD("get_joy_name", "device"), &Input::get_joy_name);
ClassDB::bind_method(D_METHOD("get_joy_guid", "device"), &Input::get_joy_guid);
ClassDB::bind_method(D_METHOD("get_connected_joypads"), &Input::get_connected_joypads);
ClassDB::bind_method(D_METHOD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength);
ClassDB::bind_method(D_METHOD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration);
ClassDB::bind_method(D_METHOD("get_joy_button_string", "button_index"), &Input::get_joy_button_string);
ClassDB::bind_method(D_METHOD("get_joy_button_index_from_string", "button"), &Input::get_joy_button_index_from_string);
ClassDB::bind_method(D_METHOD("get_joy_axis_string", "axis_index"), &Input::get_joy_axis_string);
ClassDB::bind_method(D_METHOD("get_joy_axis_index_from_string", "axis"), &Input::get_joy_axis_index_from_string);
ClassDB::bind_method(D_METHOD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0));
ClassDB::bind_method(D_METHOD("stop_joy_vibration", "device"), &Input::stop_joy_vibration);
ClassDB::bind_method(D_METHOD("vibrate_handheld", "duration_ms"), &Input::vibrate_handheld, DEFVAL(500));
ClassDB::bind_method(D_METHOD("get_gravity"), &Input::get_gravity);
ClassDB::bind_method(D_METHOD("get_accelerometer"), &Input::get_accelerometer);
ClassDB::bind_method(D_METHOD("get_magnetometer"), &Input::get_magnetometer);
ClassDB::bind_method(D_METHOD("get_gyroscope"), &Input::get_gyroscope);
ClassDB::bind_method(D_METHOD("set_gravity", "value"), &Input::set_gravity);
ClassDB::bind_method(D_METHOD("set_accelerometer", "value"), &Input::set_accelerometer);
ClassDB::bind_method(D_METHOD("set_magnetometer", "value"), &Input::set_magnetometer);
ClassDB::bind_method(D_METHOD("set_gyroscope", "value"), &Input::set_gyroscope);
//ClassDB::bind_method(D_METHOD("get_mouse_position"),&Input::get_mouse_position); - this is not the function you want
ClassDB::bind_method(D_METHOD("get_last_mouse_speed"), &Input::get_last_mouse_speed);
ClassDB::bind_method(D_METHOD("get_mouse_button_mask"), &Input::get_mouse_button_mask);
ClassDB::bind_method(D_METHOD("set_mouse_mode", "mode"), &Input::set_mouse_mode);
ClassDB::bind_method(D_METHOD("get_mouse_mode"), &Input::get_mouse_mode);
ClassDB::bind_method(D_METHOD("warp_mouse_position", "to"), &Input::warp_mouse_position);
ClassDB::bind_method(D_METHOD("action_press", "action", "strength"), &Input::action_press, DEFVAL(1.f));
ClassDB::bind_method(D_METHOD("action_release", "action"), &Input::action_release);
ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Input::set_default_cursor_shape, DEFVAL(CURSOR_ARROW));
ClassDB::bind_method(D_METHOD("get_current_cursor_shape"), &Input::get_current_cursor_shape);
ClassDB::bind_method(D_METHOD("set_custom_mouse_cursor", "image", "shape", "hotspot"), &Input::set_custom_mouse_cursor, DEFVAL(CURSOR_ARROW), DEFVAL(Vector2()));
ClassDB::bind_method(D_METHOD("parse_input_event", "event"), &Input::parse_input_event);
ClassDB::bind_method(D_METHOD("set_use_accumulated_input", "enable"), &Input::set_use_accumulated_input);
ClassDB::bind_method(D_METHOD("flush_buffered_events"), &Input::flush_buffered_events);
BIND_ENUM_CONSTANT(MOUSE_MODE_VISIBLE);
BIND_ENUM_CONSTANT(MOUSE_MODE_HIDDEN);
BIND_ENUM_CONSTANT(MOUSE_MODE_CAPTURED);
BIND_ENUM_CONSTANT(MOUSE_MODE_CONFINED);
BIND_ENUM_CONSTANT(CURSOR_ARROW);
BIND_ENUM_CONSTANT(CURSOR_IBEAM);
BIND_ENUM_CONSTANT(CURSOR_POINTING_HAND);
BIND_ENUM_CONSTANT(CURSOR_CROSS);
BIND_ENUM_CONSTANT(CURSOR_WAIT);
BIND_ENUM_CONSTANT(CURSOR_BUSY);
BIND_ENUM_CONSTANT(CURSOR_DRAG);
BIND_ENUM_CONSTANT(CURSOR_CAN_DROP);
BIND_ENUM_CONSTANT(CURSOR_FORBIDDEN);
BIND_ENUM_CONSTANT(CURSOR_VSIZE);
BIND_ENUM_CONSTANT(CURSOR_HSIZE);
BIND_ENUM_CONSTANT(CURSOR_BDIAGSIZE);
BIND_ENUM_CONSTANT(CURSOR_FDIAGSIZE);
BIND_ENUM_CONSTANT(CURSOR_MOVE);
BIND_ENUM_CONSTANT(CURSOR_VSPLIT);
BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
BIND_ENUM_CONSTANT(CURSOR_HELP);
ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "device"), PropertyInfo(Variant::BOOL, "connected")));
}
void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
#ifdef TOOLS_ENABLED
const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
String pf = p_function;
if (p_idx == 0 &&
(pf == "is_action_pressed" || pf == "action_press" || pf == "action_release" ||
pf == "is_action_just_pressed" || pf == "is_action_just_released" ||
pf == "get_action_strength" || pf == "get_action_raw_strength" ||
pf == "get_axis" || pf == "get_vector")) {
List<PropertyInfo> pinfo;
ProjectSettings::get_singleton()->get_property_list(&pinfo);
for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
const PropertyInfo &pi = E->get();
if (!pi.name.begins_with("input/")) {
continue;
}
String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
r_options->push_back(quote_style + name + quote_style);
}
}
#endif
}
Input::Input() {
singleton = this;
}
//////////////////////////////////////////////////////////

154
core/input/input.h Normal file
View File

@ -0,0 +1,154 @@
/*************************************************************************/
/* input.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef INPUT_H
#define INPUT_H
#include "core/object.h"
#include "core/os/main_loop.h"
#include "core/os/thread_safe.h"
class Input : public Object {
GDCLASS(Input, Object);
static Input *singleton;
protected:
static void _bind_methods();
public:
enum MouseMode {
MOUSE_MODE_VISIBLE,
MOUSE_MODE_HIDDEN,
MOUSE_MODE_CAPTURED,
MOUSE_MODE_CONFINED
};
#undef CursorShape
enum CursorShape {
CURSOR_ARROW,
CURSOR_IBEAM,
CURSOR_POINTING_HAND,
CURSOR_CROSS,
CURSOR_WAIT,
CURSOR_BUSY,
CURSOR_DRAG,
CURSOR_CAN_DROP,
CURSOR_FORBIDDEN,
CURSOR_VSIZE,
CURSOR_HSIZE,
CURSOR_BDIAGSIZE,
CURSOR_FDIAGSIZE,
CURSOR_MOVE,
CURSOR_VSPLIT,
CURSOR_HSPLIT,
CURSOR_HELP,
CURSOR_MAX
};
void set_mouse_mode(MouseMode p_mode);
MouseMode get_mouse_mode() const;
static Input *get_singleton();
virtual bool is_key_pressed(int p_scancode) 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, bool p_exact = false) const = 0;
virtual bool is_action_just_pressed(const StringName &p_action, bool p_exact = false) const = 0;
virtual bool is_action_just_released(const StringName &p_action, bool p_exact = false) const = 0;
virtual float get_action_strength(const StringName &p_action, bool p_exact = false) const = 0;
virtual float get_action_raw_strength(const StringName &p_action, bool p_exact = false) const = 0;
float get_axis(const StringName &p_negative_action, const StringName &p_positive_action) const;
Vector2 get_vector(const StringName &p_negative_x, const StringName &p_positive_x, const StringName &p_negative_y, const StringName &p_positive_y, float p_deadzone = -1.0f) const;
virtual float get_joy_axis(int p_device, int p_axis) const = 0;
virtual String get_joy_name(int p_idx) = 0;
virtual Array get_connected_joypads() = 0;
virtual void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid) = 0;
virtual void add_joy_mapping(String p_mapping, bool p_update_existing = false) = 0;
virtual void remove_joy_mapping(String p_guid) = 0;
virtual bool is_joy_known(int p_device) = 0;
virtual String get_joy_guid(int p_device) const = 0;
virtual Vector2 get_joy_vibration_strength(int p_device) = 0;
virtual float get_joy_vibration_duration(int p_device) = 0;
virtual uint64_t get_joy_vibration_timestamp(int p_device) = 0;
virtual void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0) = 0;
virtual void stop_joy_vibration(int p_device) = 0;
virtual void vibrate_handheld(int p_duration_ms = 500) = 0;
virtual Point2 get_mouse_position() const = 0;
virtual Point2 get_last_mouse_speed() const = 0;
virtual int get_mouse_button_mask() const = 0;
virtual void warp_mouse_position(const Vector2 &p_to) = 0;
virtual Point2i warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect) = 0;
virtual Vector3 get_gravity() const = 0;
virtual Vector3 get_accelerometer() const = 0;
virtual Vector3 get_magnetometer() const = 0;
virtual Vector3 get_gyroscope() const = 0;
virtual void set_gravity(const Vector3 &p_gravity) = 0;
virtual void set_accelerometer(const Vector3 &p_accel) = 0;
virtual void set_magnetometer(const Vector3 &p_magnetometer) = 0;
virtual void set_gyroscope(const Vector3 &p_gyroscope) = 0;
virtual void action_press(const StringName &p_action, float p_strength = 1.f) = 0;
virtual void action_release(const StringName &p_action) = 0;
void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
virtual bool is_emulating_touch_from_mouse() const = 0;
virtual bool is_emulating_mouse_from_touch() const = 0;
virtual CursorShape get_default_cursor_shape() const = 0;
virtual void set_default_cursor_shape(CursorShape p_shape) = 0;
virtual CursorShape get_current_cursor_shape() const = 0;
virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()) = 0;
virtual String get_joy_button_string(int p_button) = 0;
virtual String get_joy_axis_string(int p_axis) = 0;
virtual int get_joy_button_index_from_string(String p_button) = 0;
virtual int get_joy_axis_index_from_string(String p_axis) = 0;
virtual void parse_input_event(const Ref<InputEvent> &p_event) = 0;
virtual void flush_buffered_events() = 0;
virtual bool is_using_input_buffering() = 0;
virtual void set_use_input_buffering(bool p_enable) = 0;
virtual void set_use_accumulated_input(bool p_enable) = 0;
Input();
};
VARIANT_ENUM_CAST(Input::MouseMode);
VARIANT_ENUM_CAST(Input::CursorShape);
#endif // INPUT_H

1307
core/input/input_event.cpp Normal file

File diff suppressed because it is too large Load Diff

637
core/input/input_event.h Normal file
View File

@ -0,0 +1,637 @@
/*************************************************************************/
/* input_event.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef INPUT_EVENT_H
#define INPUT_EVENT_H
#include "core/math/transform_2d.h"
#include "core/resource.h"
#include "core/typedefs.h"
#include "core/ustring.h"
/**
* Input Event classes. These are used in the main loop.
* The events are pretty obvious.
*/
enum ButtonList {
BUTTON_LEFT = 1,
BUTTON_RIGHT = 2,
BUTTON_MIDDLE = 3,
BUTTON_WHEEL_UP = 4,
BUTTON_WHEEL_DOWN = 5,
BUTTON_WHEEL_LEFT = 6,
BUTTON_WHEEL_RIGHT = 7,
BUTTON_XBUTTON1 = 8,
BUTTON_XBUTTON2 = 9,
BUTTON_MASK_LEFT = (1 << (BUTTON_LEFT - 1)),
BUTTON_MASK_RIGHT = (1 << (BUTTON_RIGHT - 1)),
BUTTON_MASK_MIDDLE = (1 << (BUTTON_MIDDLE - 1)),
BUTTON_MASK_XBUTTON1 = (1 << (BUTTON_XBUTTON1 - 1)),
BUTTON_MASK_XBUTTON2 = (1 << (BUTTON_XBUTTON2 - 1))
};
enum JoystickList {
JOY_INVALID_OPTION = -1,
JOY_BUTTON_0 = 0,
JOY_BUTTON_1 = 1,
JOY_BUTTON_2 = 2,
JOY_BUTTON_3 = 3,
JOY_BUTTON_4 = 4,
JOY_BUTTON_5 = 5,
JOY_BUTTON_6 = 6,
JOY_BUTTON_7 = 7,
JOY_BUTTON_8 = 8,
JOY_BUTTON_9 = 9,
JOY_BUTTON_10 = 10,
JOY_BUTTON_11 = 11,
JOY_BUTTON_12 = 12,
JOY_BUTTON_13 = 13,
JOY_BUTTON_14 = 14,
JOY_BUTTON_15 = 15,
JOY_BUTTON_16 = 16,
JOY_BUTTON_17 = 17,
JOY_BUTTON_18 = 18,
JOY_BUTTON_19 = 19,
JOY_BUTTON_20 = 20,
JOY_BUTTON_21 = 21,
JOY_BUTTON_22 = 22,
JOY_BUTTON_MAX = 23,
JOY_L = JOY_BUTTON_4,
JOY_R = JOY_BUTTON_5,
JOY_L2 = JOY_BUTTON_6,
JOY_R2 = JOY_BUTTON_7,
JOY_L3 = JOY_BUTTON_8,
JOY_R3 = JOY_BUTTON_9,
JOY_SELECT = JOY_BUTTON_10,
JOY_START = JOY_BUTTON_11,
JOY_DPAD_UP = JOY_BUTTON_12,
JOY_DPAD_DOWN = JOY_BUTTON_13,
JOY_DPAD_LEFT = JOY_BUTTON_14,
JOY_DPAD_RIGHT = JOY_BUTTON_15,
JOY_GUIDE = JOY_BUTTON_16,
JOY_MISC1 = JOY_BUTTON_17,
JOY_PADDLE1 = JOY_BUTTON_18,
JOY_PADDLE2 = JOY_BUTTON_19,
JOY_PADDLE3 = JOY_BUTTON_20,
JOY_PADDLE4 = JOY_BUTTON_21,
JOY_TOUCHPAD = JOY_BUTTON_22,
JOY_SONY_CIRCLE = JOY_BUTTON_1,
JOY_SONY_X = JOY_BUTTON_0,
JOY_SONY_SQUARE = JOY_BUTTON_2,
JOY_SONY_TRIANGLE = JOY_BUTTON_3,
JOY_XBOX_A = JOY_BUTTON_0,
JOY_XBOX_B = JOY_BUTTON_1,
JOY_XBOX_X = JOY_BUTTON_2,
JOY_XBOX_Y = JOY_BUTTON_3,
JOY_DS_A = JOY_BUTTON_1,
JOY_DS_B = JOY_BUTTON_0,
JOY_DS_X = JOY_BUTTON_3,
JOY_DS_Y = JOY_BUTTON_2,
JOY_WII_C = JOY_BUTTON_5,
JOY_WII_Z = JOY_BUTTON_6,
JOY_WII_MINUS = JOY_BUTTON_10,
JOY_WII_PLUS = JOY_BUTTON_11,
JOY_VR_GRIP = JOY_BUTTON_2,
JOY_VR_PAD = JOY_BUTTON_14,
JOY_VR_TRIGGER = JOY_BUTTON_15,
JOY_OCULUS_AX = JOY_BUTTON_7,
JOY_OCULUS_BY = JOY_BUTTON_1,
JOY_OCULUS_MENU = JOY_BUTTON_3,
JOY_OPENVR_MENU = JOY_BUTTON_1,
// end of history
JOY_AXIS_0 = 0,
JOY_AXIS_1 = 1,
JOY_AXIS_2 = 2,
JOY_AXIS_3 = 3,
JOY_AXIS_4 = 4,
JOY_AXIS_5 = 5,
JOY_AXIS_6 = 6,
JOY_AXIS_7 = 7,
JOY_AXIS_8 = 8,
JOY_AXIS_9 = 9,
JOY_AXIS_MAX = 10,
JOY_ANALOG_LX = JOY_AXIS_0,
JOY_ANALOG_LY = JOY_AXIS_1,
JOY_ANALOG_RX = JOY_AXIS_2,
JOY_ANALOG_RY = JOY_AXIS_3,
JOY_ANALOG_L2 = JOY_AXIS_6,
JOY_ANALOG_R2 = JOY_AXIS_7,
JOY_VR_ANALOG_TRIGGER = JOY_AXIS_2,
JOY_VR_ANALOG_GRIP = JOY_AXIS_4,
JOY_OPENVR_TOUCHPADX = JOY_AXIS_0,
JOY_OPENVR_TOUCHPADY = JOY_AXIS_1,
};
enum MidiMessageList {
MIDI_MESSAGE_NOTE_OFF = 0x8,
MIDI_MESSAGE_NOTE_ON = 0x9,
MIDI_MESSAGE_AFTERTOUCH = 0xA,
MIDI_MESSAGE_CONTROL_CHANGE = 0xB,
MIDI_MESSAGE_PROGRAM_CHANGE = 0xC,
MIDI_MESSAGE_CHANNEL_PRESSURE = 0xD,
MIDI_MESSAGE_PITCH_BEND = 0xE,
};
/**
* Input Modifier Status
* for keyboard/mouse events.
*/
class InputEvent : public Resource {
GDCLASS(InputEvent, Resource);
int device;
protected:
static void _bind_methods();
public:
static const int DEVICE_ID_TOUCH_MOUSE;
static const int DEVICE_ID_INTERNAL;
void set_device(int p_device);
int get_device() const;
bool is_action(const StringName &p_action, bool p_exact_match = false) const;
bool is_action_pressed(const StringName &p_action, bool p_allow_echo = false, bool p_exact_match = false) const;
bool is_action_released(const StringName &p_action, bool p_exact_match = false) const;
float get_action_strength(const StringName &p_action, bool p_exact_match = false) const;
float get_action_raw_strength(const StringName &p_action, bool p_exact_match = false) const;
// To be removed someday, since they do not make sense for all events
virtual bool is_pressed() const;
virtual bool is_echo() const;
// ...-.
virtual String as_text() const;
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
virtual bool is_action_type() const;
virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
InputEvent();
};
class InputEventWithModifiers : public InputEvent {
GDCLASS(InputEventWithModifiers, InputEvent);
bool shift;
bool alt;
#ifdef APPLE_STYLE_KEYS
union {
bool command;
bool meta; //< windows/mac key
};
bool control;
#else
union {
bool command; //< windows/mac key
bool control;
};
bool meta; //< windows/mac key
#endif
protected:
static void _bind_methods();
public:
void set_shift(bool p_enabled);
bool get_shift() const;
void set_alt(bool p_enabled);
bool get_alt() const;
void set_control(bool p_enabled);
bool get_control() const;
void set_metakey(bool p_enabled);
bool get_metakey() const;
void set_command(bool p_enabled);
bool get_command() const;
void set_modifiers_from_event(const InputEventWithModifiers *event);
uint32_t get_modifiers_mask() const;
InputEventWithModifiers();
};
class InputEventKey : public InputEventWithModifiers {
GDCLASS(InputEventKey, InputEventWithModifiers);
bool pressed; /// otherwise release
uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks
uint32_t physical_scancode;
uint32_t unicode; ///unicode
bool echo; /// true if this is an echo key
protected:
static void _bind_methods();
public:
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_physical_scancode(uint32_t p_scancode);
uint32_t get_physical_scancode() 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_physical_scancode_with_modifiers() const;
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
virtual bool is_action_type() const { return true; }
virtual String as_text() const;
InputEventKey();
};
class InputEventMouse : public InputEventWithModifiers {
GDCLASS(InputEventMouse, InputEventWithModifiers);
int button_mask;
Vector2 pos;
Vector2 global_pos;
protected:
static void _bind_methods();
public:
void set_button_mask(int p_mask);
int get_button_mask() const;
void set_position(const Vector2 &p_pos);
Vector2 get_position() const;
void set_global_position(const Vector2 &p_global_pos);
Vector2 get_global_position() const;
InputEventMouse();
};
class InputEventMouseButton : public InputEventMouse {
GDCLASS(InputEventMouseButton, InputEventMouse);
float factor;
int button_index;
bool pressed; //otherwise released
bool doubleclick; //last even less than doubleclick time
protected:
static void _bind_methods();
public:
void set_factor(float p_factor);
float get_factor() const;
void set_button_index(int p_index);
int get_button_index() const;
void set_pressed(bool p_pressed);
virtual bool is_pressed() const;
void set_doubleclick(bool p_doubleclick);
bool is_doubleclick() const;
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
virtual bool is_action_type() const { return true; }
virtual String as_text() const;
InputEventMouseButton();
};
class InputEventMouseMotion : public InputEventMouse {
GDCLASS(InputEventMouseMotion, InputEventMouse);
Vector2 tilt;
float pressure;
Vector2 relative;
Vector2 speed;
protected:
static void _bind_methods();
public:
void set_tilt(const Vector2 &p_tilt);
Vector2 get_tilt() const;
void set_pressure(float p_pressure);
float get_pressure() const;
void set_relative(const Vector2 &p_relative);
Vector2 get_relative() const;
void set_speed(const Vector2 &p_speed);
Vector2 get_speed() const;
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
virtual String as_text() const;
virtual bool accumulate(const Ref<InputEvent> &p_event);
InputEventMouseMotion();
};
class InputEventJoypadMotion : public InputEvent {
GDCLASS(InputEventJoypadMotion, InputEvent);
int axis; ///< Joypad axis
float axis_value; ///< -1 to 1
protected:
static void _bind_methods();
public:
void set_axis(int p_axis);
int get_axis() const;
void set_axis_value(float p_value);
float get_axis_value() const;
virtual bool is_pressed() const;
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
virtual bool is_action_type() const { return true; }
virtual String as_text() const;
InputEventJoypadMotion();
};
class InputEventJoypadButton : public InputEvent {
GDCLASS(InputEventJoypadButton, InputEvent);
int button_index;
bool pressed;
float pressure; //0 to 1
protected:
static void _bind_methods();
public:
void set_button_index(int p_index);
int get_button_index() const;
void set_pressed(bool p_pressed);
virtual bool is_pressed() const;
void set_pressure(float p_pressure);
float get_pressure() const;
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
virtual bool is_action_type() const { return true; }
virtual String as_text() const;
InputEventJoypadButton();
};
class InputEventScreenTouch : public InputEvent {
GDCLASS(InputEventScreenTouch, InputEvent);
int index;
Vector2 pos;
bool pressed;
protected:
static void _bind_methods();
public:
void set_index(int p_index);
int get_index() const;
void set_position(const Vector2 &p_pos);
Vector2 get_position() const;
void set_pressed(bool p_pressed);
virtual bool is_pressed() const;
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
virtual String as_text() const;
InputEventScreenTouch();
};
class InputEventScreenDrag : public InputEvent {
GDCLASS(InputEventScreenDrag, InputEvent);
int index;
Vector2 pos;
Vector2 relative;
Vector2 speed;
protected:
static void _bind_methods();
public:
void set_index(int p_index);
int get_index() const;
void set_position(const Vector2 &p_pos);
Vector2 get_position() const;
void set_relative(const Vector2 &p_relative);
Vector2 get_relative() const;
void set_speed(const Vector2 &p_speed);
Vector2 get_speed() const;
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
virtual String as_text() const;
virtual bool accumulate(const Ref<InputEvent> &p_event);
InputEventScreenDrag();
};
class InputEventAction : public InputEvent {
GDCLASS(InputEventAction, InputEvent);
StringName action;
bool pressed;
float strength;
protected:
static void _bind_methods();
public:
void set_action(const StringName &p_action);
StringName get_action() const;
void set_pressed(bool p_pressed);
virtual bool is_pressed() const;
void set_strength(float p_strength);
float get_strength() const;
virtual bool is_action(const StringName &p_action) const;
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
virtual bool is_action_type() const { return true; }
virtual String as_text() const;
InputEventAction();
};
class InputEventGesture : public InputEventWithModifiers {
GDCLASS(InputEventGesture, InputEventWithModifiers);
Vector2 pos;
protected:
static void _bind_methods();
public:
void set_position(const Vector2 &p_pos);
Vector2 get_position() const;
};
class InputEventMagnifyGesture : public InputEventGesture {
GDCLASS(InputEventMagnifyGesture, InputEventGesture);
real_t factor;
protected:
static void _bind_methods();
public:
void set_factor(real_t p_factor);
real_t get_factor() const;
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
virtual String as_text() const;
InputEventMagnifyGesture();
};
class InputEventPanGesture : public InputEventGesture {
GDCLASS(InputEventPanGesture, InputEventGesture);
Vector2 delta;
protected:
static void _bind_methods();
public:
void set_delta(const Vector2 &p_delta);
Vector2 get_delta() const;
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
virtual String as_text() const;
InputEventPanGesture();
};
class InputEventMIDI : public InputEvent {
GDCLASS(InputEventMIDI, InputEvent);
int channel;
int message;
int pitch;
int velocity;
int instrument;
int pressure;
int controller_number;
int controller_value;
protected:
static void _bind_methods();
public:
void set_channel(const int p_channel);
int get_channel() const;
void set_message(const int p_message);
int get_message() const;
void set_pitch(const int p_pitch);
int get_pitch() const;
void set_velocity(const int p_velocity);
int get_velocity() const;
void set_instrument(const int p_instrument);
int get_instrument() const;
void set_pressure(const int p_pressure);
int get_pressure() const;
void set_controller_number(const int p_controller_number);
int get_controller_number() const;
void set_controller_value(const int p_controller_value);
int get_controller_value() const;
virtual String as_text() const;
InputEventMIDI();
};
#endif

473
core/input/keyboard.cpp Normal file
View File

@ -0,0 +1,473 @@
/*************************************************************************/
/* keyboard.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "keyboard.h"
#include "core/os/os.h"
struct _KeyCodeText {
int code;
const char *text;
};
static const _KeyCodeText _keycodes[] = {
/* clang-format off */
{KEY_ESCAPE ,"Escape"},
{KEY_TAB ,"Tab"},
{KEY_BACKTAB ,"BackTab"},
{KEY_BACKSPACE ,"BackSpace"},
{KEY_ENTER ,"Enter"},
{KEY_KP_ENTER ,"Kp Enter"},
{KEY_INSERT ,"Insert"},
{KEY_DELETE ,"Delete"},
{KEY_PAUSE ,"Pause"},
{KEY_PRINT ,"Print"},
{KEY_SYSREQ ,"SysReq"},
{KEY_CLEAR ,"Clear"},
{KEY_HOME ,"Home"},
{KEY_END ,"End"},
{KEY_LEFT ,"Left"},
{KEY_UP ,"Up"},
{KEY_RIGHT ,"Right"},
{KEY_DOWN ,"Down"},
{KEY_PAGEUP ,"PageUp"},
{KEY_PAGEDOWN ,"PageDown"},
{KEY_SHIFT ,"Shift"},
{KEY_CONTROL ,"Control"},
#ifdef OSX_ENABLED
{KEY_META ,"Command"},
#else
{KEY_META ,"Meta"},
#endif
{KEY_ALT ,"Alt"},
{KEY_CAPSLOCK ,"CapsLock"},
{KEY_NUMLOCK ,"NumLock"},
{KEY_SCROLLLOCK ,"ScrollLock"},
{KEY_F1 ,"F1"},
{KEY_F2 ,"F2"},
{KEY_F3 ,"F3"},
{KEY_F4 ,"F4"},
{KEY_F5 ,"F5"},
{KEY_F6 ,"F6"},
{KEY_F7 ,"F7"},
{KEY_F8 ,"F8"},
{KEY_F9 ,"F9"},
{KEY_F10 ,"F10"},
{KEY_F11 ,"F11"},
{KEY_F12 ,"F12"},
{KEY_F13 ,"F13"},
{KEY_F14 ,"F14"},
{KEY_F15 ,"F15"},
{KEY_F16 ,"F16"},
{KEY_KP_MULTIPLY ,"Kp Multiply"},
{KEY_KP_DIVIDE ,"Kp Divide"},
{KEY_KP_SUBTRACT ,"Kp Subtract"},
{KEY_KP_PERIOD ,"Kp Period"},
{KEY_KP_ADD ,"Kp Add"},
{KEY_KP_0 ,"Kp 0"},
{KEY_KP_1 ,"Kp 1"},
{KEY_KP_2 ,"Kp 2"},
{KEY_KP_3 ,"Kp 3"},
{KEY_KP_4 ,"Kp 4"},
{KEY_KP_5 ,"Kp 5"},
{KEY_KP_6 ,"Kp 6"},
{KEY_KP_7 ,"Kp 7"},
{KEY_KP_8 ,"Kp 8"},
{KEY_KP_9 ,"Kp 9"},
{KEY_SUPER_L ,"Super L"},
{KEY_SUPER_R ,"Super R"},
{KEY_MENU ,"Menu"},
{KEY_HYPER_L ,"Hyper L"},
{KEY_HYPER_R ,"Hyper R"},
{KEY_HELP ,"Help"},
{KEY_DIRECTION_L ,"Direction L"},
{KEY_DIRECTION_R ,"Direction R"},
{KEY_BACK ,"Back"},
{KEY_FORWARD ,"Forward"},
{KEY_STOP ,"Stop"},
{KEY_REFRESH ,"Refresh"},
{KEY_VOLUMEDOWN ,"VolumeDown"},
{KEY_VOLUMEMUTE ,"VolumeMute"},
{KEY_VOLUMEUP ,"VolumeUp"},
{KEY_BASSBOOST ,"BassBoost"},
{KEY_BASSUP ,"BassUp"},
{KEY_BASSDOWN ,"BassDown"},
{KEY_TREBLEUP ,"TrebleUp"},
{KEY_TREBLEDOWN ,"TrebleDown"},
{KEY_MEDIAPLAY ,"MediaPlay"},
{KEY_MEDIASTOP ,"MediaStop"},
{KEY_MEDIAPREVIOUS ,"MediaPrevious"},
{KEY_MEDIANEXT ,"MediaNext"},
{KEY_MEDIARECORD ,"MediaRecord"},
{KEY_HOMEPAGE ,"HomePage"},
{KEY_FAVORITES ,"Favorites"},
{KEY_SEARCH ,"Search"},
{KEY_STANDBY ,"StandBy"},
{KEY_LAUNCHMAIL ,"LaunchMail"},
{KEY_LAUNCHMEDIA ,"LaunchMedia"},
{KEY_LAUNCH0 ,"Launch0"},
{KEY_LAUNCH1 ,"Launch1"},
{KEY_LAUNCH2 ,"Launch2"},
{KEY_LAUNCH3 ,"Launch3"},
{KEY_LAUNCH4 ,"Launch4"},
{KEY_LAUNCH5 ,"Launch5"},
{KEY_LAUNCH6 ,"Launch6"},
{KEY_LAUNCH7 ,"Launch7"},
{KEY_LAUNCH8 ,"Launch8"},
{KEY_LAUNCH9 ,"Launch9"},
{KEY_LAUNCHA ,"LaunchA"},
{KEY_LAUNCHB ,"LaunchB"},
{KEY_LAUNCHC ,"LaunchC"},
{KEY_LAUNCHD ,"LaunchD"},
{KEY_LAUNCHE ,"LaunchE"},
{KEY_LAUNCHF ,"LaunchF"},
{KEY_UNKNOWN ,"Unknown"},
{KEY_SPACE ,"Space"},
{KEY_EXCLAM ,"Exclam"},
{KEY_QUOTEDBL ,"QuoteDbl"},
{KEY_NUMBERSIGN ,"NumberSign"},
{KEY_DOLLAR ,"Dollar"},
{KEY_PERCENT ,"Percent"},
{KEY_AMPERSAND ,"Ampersand"},
{KEY_APOSTROPHE ,"Apostrophe"},
{KEY_PARENLEFT ,"ParenLeft"},
{KEY_PARENRIGHT ,"ParenRight"},
{KEY_ASTERISK ,"Asterisk"},
{KEY_PLUS ,"Plus"},
{KEY_COMMA ,"Comma"},
{KEY_MINUS ,"Minus"},
{KEY_PERIOD ,"Period"},
{KEY_SLASH ,"Slash"},
{KEY_0 ,"0"},
{KEY_1 ,"1"},
{KEY_2 ,"2"},
{KEY_3 ,"3"},
{KEY_4 ,"4"},
{KEY_5 ,"5"},
{KEY_6 ,"6"},
{KEY_7 ,"7"},
{KEY_8 ,"8"},
{KEY_9 ,"9"},
{KEY_COLON ,"Colon"},
{KEY_SEMICOLON ,"Semicolon"},
{KEY_LESS ,"Less"},
{KEY_EQUAL ,"Equal"},
{KEY_GREATER ,"Greater"},
{KEY_QUESTION ,"Question"},
{KEY_AT ,"At"},
{KEY_A ,"A"},
{KEY_B ,"B"},
{KEY_C ,"C"},
{KEY_D ,"D"},
{KEY_E ,"E"},
{KEY_F ,"F"},
{KEY_G ,"G"},
{KEY_H ,"H"},
{KEY_I ,"I"},
{KEY_J ,"J"},
{KEY_K ,"K"},
{KEY_L ,"L"},
{KEY_M ,"M"},
{KEY_N ,"N"},
{KEY_O ,"O"},
{KEY_P ,"P"},
{KEY_Q ,"Q"},
{KEY_R ,"R"},
{KEY_S ,"S"},
{KEY_T ,"T"},
{KEY_U ,"U"},
{KEY_V ,"V"},
{KEY_W ,"W"},
{KEY_X ,"X"},
{KEY_Y ,"Y"},
{KEY_Z ,"Z"},
{KEY_BRACKETLEFT ,"BracketLeft"},
{KEY_BACKSLASH ,"BackSlash"},
{KEY_BRACKETRIGHT ,"BracketRight"},
{KEY_ASCIICIRCUM ,"AsciiCircum"},
{KEY_UNDERSCORE ,"UnderScore"},
{KEY_QUOTELEFT ,"QuoteLeft"},
{KEY_BRACELEFT ,"BraceLeft"},
{KEY_BAR ,"Bar"},
{KEY_BRACERIGHT ,"BraceRight"},
{KEY_ASCIITILDE ,"AsciiTilde"},
{KEY_NOBREAKSPACE ,"NoBreakSpace"},
{KEY_EXCLAMDOWN ,"ExclamDown"},
{KEY_CENT ,"Cent"},
{KEY_STERLING ,"Sterling"},
{KEY_CURRENCY ,"Currency"},
{KEY_YEN ,"Yen"},
{KEY_BROKENBAR ,"BrokenBar"},
{KEY_SECTION ,"Section"},
{KEY_DIAERESIS ,"Diaeresis"},
{KEY_COPYRIGHT ,"Copyright"},
{KEY_ORDFEMININE ,"Ordfeminine"},
{KEY_GUILLEMOTLEFT ,"GuillemotLeft"},
{KEY_NOTSIGN ,"NotSign"},
{KEY_HYPHEN ,"Hyphen"},
{KEY_REGISTERED ,"Registered"},
{KEY_MACRON ,"Macron"},
{KEY_DEGREE ,"Degree"},
{KEY_PLUSMINUS ,"PlusMinus"},
{KEY_TWOSUPERIOR ,"TwoSuperior"},
{KEY_THREESUPERIOR ,"ThreeSuperior"},
{KEY_ACUTE ,"Acute"},
{KEY_MU ,"Mu"},
{KEY_PARAGRAPH ,"Paragraph"},
{KEY_PERIODCENTERED ,"PeriodCentered"},
{KEY_CEDILLA ,"Cedilla"},
{KEY_ONESUPERIOR ,"OneSuperior"},
{KEY_MASCULINE ,"Masculine"},
{KEY_GUILLEMOTRIGHT ,"GuillemotRight"},
{KEY_ONEQUARTER ,"OneQuarter"},
{KEY_ONEHALF ,"OneHalf"},
{KEY_THREEQUARTERS ,"ThreeQuarters"},
{KEY_QUESTIONDOWN ,"QuestionDown"},
{KEY_AGRAVE ,"Agrave"},
{KEY_AACUTE ,"Aacute"},
{KEY_ACIRCUMFLEX ,"AcircumFlex"},
{KEY_ATILDE ,"Atilde"},
{KEY_ADIAERESIS ,"Adiaeresis"},
{KEY_ARING ,"Aring"},
{KEY_AE ,"Ae"},
{KEY_CCEDILLA ,"Ccedilla"},
{KEY_EGRAVE ,"Egrave"},
{KEY_EACUTE ,"Eacute"},
{KEY_ECIRCUMFLEX ,"Ecircumflex"},
{KEY_EDIAERESIS ,"Ediaeresis"},
{KEY_IGRAVE ,"Igrave"},
{KEY_IACUTE ,"Iacute"},
{KEY_ICIRCUMFLEX ,"Icircumflex"},
{KEY_IDIAERESIS ,"Idiaeresis"},
{KEY_ETH ,"Eth"},
{KEY_NTILDE ,"Ntilde"},
{KEY_OGRAVE ,"Ograve"},
{KEY_OACUTE ,"Oacute"},
{KEY_OCIRCUMFLEX ,"Ocircumflex"},
{KEY_OTILDE ,"Otilde"},
{KEY_ODIAERESIS ,"Odiaeresis"},
{KEY_MULTIPLY ,"Multiply"},
{KEY_OOBLIQUE ,"Ooblique"},
{KEY_UGRAVE ,"Ugrave"},
{KEY_UACUTE ,"Uacute"},
{KEY_UCIRCUMFLEX ,"Ucircumflex"},
{KEY_UDIAERESIS ,"Udiaeresis"},
{KEY_YACUTE ,"Yacute"},
{KEY_THORN ,"Thorn"},
{KEY_SSHARP ,"Ssharp"},
{KEY_DIVISION ,"Division"},
{KEY_YDIAERESIS ,"Ydiaeresis"},
{0 ,nullptr}
/* clang-format on */
};
bool keycode_has_unicode(uint32_t p_keycode) {
switch (p_keycode) {
case KEY_ESCAPE:
case KEY_TAB:
case KEY_BACKTAB:
case KEY_BACKSPACE:
case KEY_ENTER:
case KEY_KP_ENTER:
case KEY_INSERT:
case KEY_DELETE:
case KEY_PAUSE:
case KEY_PRINT:
case KEY_SYSREQ:
case KEY_CLEAR:
case KEY_HOME:
case KEY_END:
case KEY_LEFT:
case KEY_UP:
case KEY_RIGHT:
case KEY_DOWN:
case KEY_PAGEUP:
case KEY_PAGEDOWN:
case KEY_SHIFT:
case KEY_CONTROL:
case KEY_META:
case KEY_ALT:
case KEY_CAPSLOCK:
case KEY_NUMLOCK:
case KEY_SCROLLLOCK:
case KEY_F1:
case KEY_F2:
case KEY_F3:
case KEY_F4:
case KEY_F5:
case KEY_F6:
case KEY_F7:
case KEY_F8:
case KEY_F9:
case KEY_F10:
case KEY_F11:
case KEY_F12:
case KEY_F13:
case KEY_F14:
case KEY_F15:
case KEY_F16:
case KEY_SUPER_L:
case KEY_SUPER_R:
case KEY_MENU:
case KEY_HYPER_L:
case KEY_HYPER_R:
case KEY_HELP:
case KEY_DIRECTION_L:
case KEY_DIRECTION_R:
case KEY_BACK:
case KEY_FORWARD:
case KEY_STOP:
case KEY_REFRESH:
case KEY_VOLUMEDOWN:
case KEY_VOLUMEMUTE:
case KEY_VOLUMEUP:
case KEY_BASSBOOST:
case KEY_BASSUP:
case KEY_BASSDOWN:
case KEY_TREBLEUP:
case KEY_TREBLEDOWN:
case KEY_MEDIAPLAY:
case KEY_MEDIASTOP:
case KEY_MEDIAPREVIOUS:
case KEY_MEDIANEXT:
case KEY_MEDIARECORD:
case KEY_HOMEPAGE:
case KEY_FAVORITES:
case KEY_SEARCH:
case KEY_STANDBY:
case KEY_OPENURL:
case KEY_LAUNCHMAIL:
case KEY_LAUNCHMEDIA:
case KEY_LAUNCH0:
case KEY_LAUNCH1:
case KEY_LAUNCH2:
case KEY_LAUNCH3:
case KEY_LAUNCH4:
case KEY_LAUNCH5:
case KEY_LAUNCH6:
case KEY_LAUNCH7:
case KEY_LAUNCH8:
case KEY_LAUNCH9:
case KEY_LAUNCHA:
case KEY_LAUNCHB:
case KEY_LAUNCHC:
case KEY_LAUNCHD:
case KEY_LAUNCHE:
case KEY_LAUNCHF:
return false;
}
return true;
}
String keycode_get_string(uint32_t p_code) {
String codestr;
if (p_code & KEY_MASK_SHIFT) {
codestr += find_keycode_name(KEY_SHIFT);
codestr += "+";
}
if (p_code & KEY_MASK_ALT) {
codestr += find_keycode_name(KEY_ALT);
codestr += "+";
}
if (p_code & KEY_MASK_CTRL) {
codestr += find_keycode_name(KEY_CONTROL);
codestr += "+";
}
if (p_code & KEY_MASK_META) {
codestr += find_keycode_name(KEY_META);
codestr += "+";
}
p_code &= KEY_CODE_MASK;
const _KeyCodeText *kct = &_keycodes[0];
while (kct->text) {
if (kct->code == (int)p_code) {
codestr += kct->text;
return codestr;
}
kct++;
}
codestr += String::chr(p_code);
return codestr;
}
int find_keycode(const String &p_code) {
const _KeyCodeText *kct = &_keycodes[0];
while (kct->text) {
if (p_code.nocasecmp_to(kct->text) == 0) {
return kct->code;
}
kct++;
}
return 0;
}
const char *find_keycode_name(int p_keycode) {
const _KeyCodeText *kct = &_keycodes[0];
while (kct->text) {
if (kct->code == p_keycode) {
return kct->text;
}
kct++;
}
return "";
}
int keycode_get_count() {
const _KeyCodeText *kct = &_keycodes[0];
int count = 0;
while (kct->text) {
count++;
kct++;
}
return count;
}
int keycode_get_value_by_index(int p_index) {
return _keycodes[p_index].code;
}
const char *keycode_get_name_by_index(int p_index) {
return _keycodes[p_index].text;
}

328
core/input/keyboard.h Normal file
View File

@ -0,0 +1,328 @@
/*************************************************************************/
/* keyboard.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include "core/ustring.h"
/*
Special Key:
The strategy here is similar to the one used by toolkits,
which consists in leaving the 24 bits unicode range for printable
characters, and use the upper 8 bits for special keys and
modifiers. This way everything (char/keycode) can fit nicely in one 32 bits unsigned integer.
*/
enum {
SPKEY = (1 << 24)
};
enum KeyList {
/* CURSOR/FUNCTION/BROWSER/MULTIMEDIA/MISC KEYS */
KEY_ESCAPE = SPKEY | 0x01,
KEY_TAB = SPKEY | 0x02,
KEY_BACKTAB = SPKEY | 0x03,
KEY_BACKSPACE = SPKEY | 0x04,
KEY_ENTER = SPKEY | 0x05,
KEY_KP_ENTER = SPKEY | 0x06,
KEY_INSERT = SPKEY | 0x07,
KEY_DELETE = SPKEY | 0x08,
KEY_PAUSE = SPKEY | 0x09,
KEY_PRINT = SPKEY | 0x0A,
KEY_SYSREQ = SPKEY | 0x0B,
KEY_CLEAR = SPKEY | 0x0C,
KEY_HOME = SPKEY | 0x0D,
KEY_END = SPKEY | 0x0E,
KEY_LEFT = SPKEY | 0x0F,
KEY_UP = SPKEY | 0x10,
KEY_RIGHT = SPKEY | 0x11,
KEY_DOWN = SPKEY | 0x12,
KEY_PAGEUP = SPKEY | 0x13,
KEY_PAGEDOWN = SPKEY | 0x14,
KEY_SHIFT = SPKEY | 0x15,
KEY_CONTROL = SPKEY | 0x16,
KEY_META = SPKEY | 0x17,
KEY_ALT = SPKEY | 0x18,
KEY_CAPSLOCK = SPKEY | 0x19,
KEY_NUMLOCK = SPKEY | 0x1A,
KEY_SCROLLLOCK = SPKEY | 0x1B,
KEY_F1 = SPKEY | 0x1C,
KEY_F2 = SPKEY | 0x1D,
KEY_F3 = SPKEY | 0x1E,
KEY_F4 = SPKEY | 0x1F,
KEY_F5 = SPKEY | 0x20,
KEY_F6 = SPKEY | 0x21,
KEY_F7 = SPKEY | 0x22,
KEY_F8 = SPKEY | 0x23,
KEY_F9 = SPKEY | 0x24,
KEY_F10 = SPKEY | 0x25,
KEY_F11 = SPKEY | 0x26,
KEY_F12 = SPKEY | 0x27,
KEY_F13 = SPKEY | 0x28,
KEY_F14 = SPKEY | 0x29,
KEY_F15 = SPKEY | 0x2A,
KEY_F16 = SPKEY | 0x2B,
KEY_KP_MULTIPLY = SPKEY | 0x81,
KEY_KP_DIVIDE = SPKEY | 0x82,
KEY_KP_SUBTRACT = SPKEY | 0x83,
KEY_KP_PERIOD = SPKEY | 0x84,
KEY_KP_ADD = SPKEY | 0x85,
KEY_KP_0 = SPKEY | 0x86,
KEY_KP_1 = SPKEY | 0x87,
KEY_KP_2 = SPKEY | 0x88,
KEY_KP_3 = SPKEY | 0x89,
KEY_KP_4 = SPKEY | 0x8A,
KEY_KP_5 = SPKEY | 0x8B,
KEY_KP_6 = SPKEY | 0x8C,
KEY_KP_7 = SPKEY | 0x8D,
KEY_KP_8 = SPKEY | 0x8E,
KEY_KP_9 = SPKEY | 0x8F,
KEY_SUPER_L = SPKEY | 0x2C,
KEY_SUPER_R = SPKEY | 0x2D,
KEY_MENU = SPKEY | 0x2E,
KEY_HYPER_L = SPKEY | 0x2F,
KEY_HYPER_R = SPKEY | 0x30,
KEY_HELP = SPKEY | 0x31,
KEY_DIRECTION_L = SPKEY | 0x32,
KEY_DIRECTION_R = SPKEY | 0x33,
KEY_BACK = SPKEY | 0x40,
KEY_FORWARD = SPKEY | 0x41,
KEY_STOP = SPKEY | 0x42,
KEY_REFRESH = SPKEY | 0x43,
KEY_VOLUMEDOWN = SPKEY | 0x44,
KEY_VOLUMEMUTE = SPKEY | 0x45,
KEY_VOLUMEUP = SPKEY | 0x46,
KEY_BASSBOOST = SPKEY | 0x47,
KEY_BASSUP = SPKEY | 0x48,
KEY_BASSDOWN = SPKEY | 0x49,
KEY_TREBLEUP = SPKEY | 0x4A,
KEY_TREBLEDOWN = SPKEY | 0x4B,
KEY_MEDIAPLAY = SPKEY | 0x4C,
KEY_MEDIASTOP = SPKEY | 0x4D,
KEY_MEDIAPREVIOUS = SPKEY | 0x4E,
KEY_MEDIANEXT = SPKEY | 0x4F,
KEY_MEDIARECORD = SPKEY | 0x50,
KEY_HOMEPAGE = SPKEY | 0x51,
KEY_FAVORITES = SPKEY | 0x52,
KEY_SEARCH = SPKEY | 0x53,
KEY_STANDBY = SPKEY | 0x54,
KEY_OPENURL = SPKEY | 0x55,
KEY_LAUNCHMAIL = SPKEY | 0x56,
KEY_LAUNCHMEDIA = SPKEY | 0x57,
KEY_LAUNCH0 = SPKEY | 0x58,
KEY_LAUNCH1 = SPKEY | 0x59,
KEY_LAUNCH2 = SPKEY | 0x5A,
KEY_LAUNCH3 = SPKEY | 0x5B,
KEY_LAUNCH4 = SPKEY | 0x5C,
KEY_LAUNCH5 = SPKEY | 0x5D,
KEY_LAUNCH6 = SPKEY | 0x5E,
KEY_LAUNCH7 = SPKEY | 0x5F,
KEY_LAUNCH8 = SPKEY | 0x60,
KEY_LAUNCH9 = SPKEY | 0x61,
KEY_LAUNCHA = SPKEY | 0x62,
KEY_LAUNCHB = SPKEY | 0x63,
KEY_LAUNCHC = SPKEY | 0x64,
KEY_LAUNCHD = SPKEY | 0x65,
KEY_LAUNCHE = SPKEY | 0x66,
KEY_LAUNCHF = SPKEY | 0x67,
KEY_UNKNOWN = SPKEY | 0xFFFFFF,
/* PRINTABLE LATIN 1 CODES */
KEY_SPACE = 0x0020,
KEY_EXCLAM = 0x0021,
KEY_QUOTEDBL = 0x0022,
KEY_NUMBERSIGN = 0x0023,
KEY_DOLLAR = 0x0024,
KEY_PERCENT = 0x0025,
KEY_AMPERSAND = 0x0026,
KEY_APOSTROPHE = 0x0027,
KEY_PARENLEFT = 0x0028,
KEY_PARENRIGHT = 0x0029,
KEY_ASTERISK = 0x002A,
KEY_PLUS = 0x002B,
KEY_COMMA = 0x002C,
KEY_MINUS = 0x002D,
KEY_PERIOD = 0x002E,
KEY_SLASH = 0x002F,
KEY_0 = 0x0030,
KEY_1 = 0x0031,
KEY_2 = 0x0032,
KEY_3 = 0x0033,
KEY_4 = 0x0034,
KEY_5 = 0x0035,
KEY_6 = 0x0036,
KEY_7 = 0x0037,
KEY_8 = 0x0038,
KEY_9 = 0x0039,
KEY_COLON = 0x003A,
KEY_SEMICOLON = 0x003B,
KEY_LESS = 0x003C,
KEY_EQUAL = 0x003D,
KEY_GREATER = 0x003E,
KEY_QUESTION = 0x003F,
KEY_AT = 0x0040,
KEY_A = 0x0041,
KEY_B = 0x0042,
KEY_C = 0x0043,
KEY_D = 0x0044,
KEY_E = 0x0045,
KEY_F = 0x0046,
KEY_G = 0x0047,
KEY_H = 0x0048,
KEY_I = 0x0049,
KEY_J = 0x004A,
KEY_K = 0x004B,
KEY_L = 0x004C,
KEY_M = 0x004D,
KEY_N = 0x004E,
KEY_O = 0x004F,
KEY_P = 0x0050,
KEY_Q = 0x0051,
KEY_R = 0x0052,
KEY_S = 0x0053,
KEY_T = 0x0054,
KEY_U = 0x0055,
KEY_V = 0x0056,
KEY_W = 0x0057,
KEY_X = 0x0058,
KEY_Y = 0x0059,
KEY_Z = 0x005A,
KEY_BRACKETLEFT = 0x005B,
KEY_BACKSLASH = 0x005C,
KEY_BRACKETRIGHT = 0x005D,
KEY_ASCIICIRCUM = 0x005E,
KEY_UNDERSCORE = 0x005F,
KEY_QUOTELEFT = 0x0060,
KEY_BRACELEFT = 0x007B,
KEY_BAR = 0x007C,
KEY_BRACERIGHT = 0x007D,
KEY_ASCIITILDE = 0x007E,
KEY_NOBREAKSPACE = 0x00A0,
KEY_EXCLAMDOWN = 0x00A1,
KEY_CENT = 0x00A2,
KEY_STERLING = 0x00A3,
KEY_CURRENCY = 0x00A4,
KEY_YEN = 0x00A5,
KEY_BROKENBAR = 0x00A6,
KEY_SECTION = 0x00A7,
KEY_DIAERESIS = 0x00A8,
KEY_COPYRIGHT = 0x00A9,
KEY_ORDFEMININE = 0x00AA,
KEY_GUILLEMOTLEFT = 0x00AB,
KEY_NOTSIGN = 0x00AC,
KEY_HYPHEN = 0x00AD,
KEY_REGISTERED = 0x00AE,
KEY_MACRON = 0x00AF,
KEY_DEGREE = 0x00B0,
KEY_PLUSMINUS = 0x00B1,
KEY_TWOSUPERIOR = 0x00B2,
KEY_THREESUPERIOR = 0x00B3,
KEY_ACUTE = 0x00B4,
KEY_MU = 0x00B5,
KEY_PARAGRAPH = 0x00B6,
KEY_PERIODCENTERED = 0x00B7,
KEY_CEDILLA = 0x00B8,
KEY_ONESUPERIOR = 0x00B9,
KEY_MASCULINE = 0x00BA,
KEY_GUILLEMOTRIGHT = 0x00BB,
KEY_ONEQUARTER = 0x00BC,
KEY_ONEHALF = 0x00BD,
KEY_THREEQUARTERS = 0x00BE,
KEY_QUESTIONDOWN = 0x00BF,
KEY_AGRAVE = 0x00C0,
KEY_AACUTE = 0x00C1,
KEY_ACIRCUMFLEX = 0x00C2,
KEY_ATILDE = 0x00C3,
KEY_ADIAERESIS = 0x00C4,
KEY_ARING = 0x00C5,
KEY_AE = 0x00C6,
KEY_CCEDILLA = 0x00C7,
KEY_EGRAVE = 0x00C8,
KEY_EACUTE = 0x00C9,
KEY_ECIRCUMFLEX = 0x00CA,
KEY_EDIAERESIS = 0x00CB,
KEY_IGRAVE = 0x00CC,
KEY_IACUTE = 0x00CD,
KEY_ICIRCUMFLEX = 0x00CE,
KEY_IDIAERESIS = 0x00CF,
KEY_ETH = 0x00D0,
KEY_NTILDE = 0x00D1,
KEY_OGRAVE = 0x00D2,
KEY_OACUTE = 0x00D3,
KEY_OCIRCUMFLEX = 0x00D4,
KEY_OTILDE = 0x00D5,
KEY_ODIAERESIS = 0x00D6,
KEY_MULTIPLY = 0x00D7,
KEY_OOBLIQUE = 0x00D8,
KEY_UGRAVE = 0x00D9,
KEY_UACUTE = 0x00DA,
KEY_UCIRCUMFLEX = 0x00DB,
KEY_UDIAERESIS = 0x00DC,
KEY_YACUTE = 0x00DD,
KEY_THORN = 0x00DE,
KEY_SSHARP = 0x00DF,
KEY_DIVISION = 0x00F7,
KEY_YDIAERESIS = 0x00FF,
};
enum KeyModifierMask {
KEY_CODE_MASK = ((1 << 25) - 1), ///< Apply this mask to any keycode to remove modifiers.
KEY_MODIFIER_MASK = (0xFF << 24), ///< Apply this mask to isolate modifiers.
KEY_MASK_SHIFT = (1 << 25),
KEY_MASK_ALT = (1 << 26),
KEY_MASK_META = (1 << 27),
KEY_MASK_CTRL = (1 << 28),
#ifdef APPLE_STYLE_KEYS
KEY_MASK_CMD = KEY_MASK_META,
#else
KEY_MASK_CMD = KEY_MASK_CTRL,
#endif
KEY_MASK_KPAD = (1 << 29),
KEY_MASK_GROUP_SWITCH = (1 << 30)
// bit 31 can't be used because variant uses regular 32 bits int as datatype
};
String keycode_get_string(uint32_t p_code);
bool keycode_has_unicode(uint32_t p_keycode);
int find_keycode(const String &p_code);
const char *find_keycode_name(int p_keycode);
int keycode_get_count();
int keycode_get_value_by_index(int p_index);
const char *keycode_get_name_by_index(int p_index);
#endif