If there are errors in the code, terminology, etc. in the text, please correct me
Article directory
foreword
-
of this program
From 019, it is possible to detect whether a key is pressed, whether the mouse is clicked, the position of the mouse, etc. anywhere in the program .
But if you want to press a certain button (A), you need to use the key value defined by the GLFW macro
if (Hazel::Input::IsKeyPressed(GLFW_KEY_A)) { HZ_TRACE("A键按下"); }
GLFW_KEY_A is defined in the #include <GLFW/glfw3.h> file, so as long as GLFW_KEY_A is used, this header file needs to be included. This is not good, and the key value should be customized to get rid of the close connection with GLFW.
if (Hazel::Input::IsKeyPressed(HZ_KEY_A)) { HZ_TRACE("A键按下"); }
-
Why do you want to customize the key value?
Because the engine wants to use glfw on mac and linux in the future, and use win32 windows on windows, and the key values of win32 windows are inconsistent with the key values of glfw windows , so it is necessary to customize and take into account the common two window libraries.
-
How to take into account the method analysis of the two window libraries (not implemented yet)
Cherno talked a lot, I'm a little dizzy, probably
-
According to the macro definition
For example: if it is a win32 window, the custom key value Tab=9, if it is a glfw window, the custom key value Tab=258
-
Mapping by function
Provide a function similar to HZKEYToWin32Key (HZ_KEY key), convert the key value of Win32 according to the input HZ_KEY
-
project related
the code
-
KeyCodes
#pragma once // From glfw3.h 参考glfw3.h中的键值 #define HZ_KEY_SPACE 32 #define HZ_KEY_APOSTROPHE 39 /* ' */ #define HZ_KEY_COMMA 44 /* , */ #define HZ_KEY_MINUS 45 /* - */ #define HZ_KEY_PERIOD 46 /* . */ #define HZ_KEY_SLASH 47 /* / */ #define HZ_KEY_0 48 #define HZ_KEY_1 49 #define HZ_KEY_2 50 #define HZ_KEY_3 51 #define HZ_KEY_4 52 #define HZ_KEY_5 53 #define HZ_KEY_6 54 #define HZ_KEY_7 55 #define HZ_KEY_8 56 #define HZ_KEY_9 57 #define HZ_KEY_SEMICOLON 59 /* ; */ #define HZ_KEY_EQUAL 61 /* = */ #define HZ_KEY_A 65 #define HZ_KEY_B 66 #define HZ_KEY_C 67 #define HZ_KEY_D 68 #define HZ_KEY_E 69 #define HZ_KEY_F 70 #define HZ_KEY_G 71 #define HZ_KEY_H 72 #define HZ_KEY_I 73 #define HZ_KEY_J 74 #define HZ_KEY_K 75 #define HZ_KEY_L 76 #define HZ_KEY_M 77 #define HZ_KEY_N 78 #define HZ_KEY_O 79 #define HZ_KEY_P 80 #define HZ_KEY_Q 81 #define HZ_KEY_R 82 #define HZ_KEY_S 83 #define HZ_KEY_T 84 #define HZ_KEY_U 85 #define HZ_KEY_V 86 #define HZ_KEY_W 87 #define HZ_KEY_X 88 #define HZ_KEY_Y 89 #define HZ_KEY_Z 90 #define HZ_KEY_LEFT_BRACKET 91 /* [ */ #define HZ_KEY_BACKSLASH 92 /* \ */ #define HZ_KEY_RIGHT_BRACKET 93 /* ] */ #define HZ_KEY_GRAVE_ACCENT 96 /* ` */ #define HZ_KEY_WORLD_1 161 /* non-US #1 */ #define HZ_KEY_WORLD_2 162 /* non-US #2 */ /* Function keys */ #define HZ_KEY_ESCAPE 256 #define HZ_KEY_ENTER 257 #define HZ_KEY_TAB 258 #define HZ_KEY_BACKSPACE 259 #define HZ_KEY_INSERT 260 #define HZ_KEY_DELETE 261 #define HZ_KEY_RIGHT 262 #define HZ_KEY_LEFT 263 #define HZ_KEY_DOWN 264 #define HZ_KEY_UP 265 #define HZ_KEY_PAGE_UP 266 #define HZ_KEY_PAGE_DOWN 267 #define HZ_KEY_HOME 268 #define HZ_KEY_END 269 #define HZ_KEY_CAPS_LOCK 280 #define HZ_KEY_SCROLL_LOCK 281 #define HZ_KEY_NUM_LOCK 282 #define HZ_KEY_PRINT_SCREEN 283 #define HZ_KEY_PAUSE 284 #define HZ_KEY_F1 290 #define HZ_KEY_F2 291 #define HZ_KEY_F3 292 #define HZ_KEY_F4 293 #define HZ_KEY_F5 294 #define HZ_KEY_F6 295 #define HZ_KEY_F7 296 #define HZ_KEY_F8 297 #define HZ_KEY_F9 298 #define HZ_KEY_F10 299 #define HZ_KEY_F11 300 #define HZ_KEY_F12 301 #define HZ_KEY_F13 302 #define HZ_KEY_F14 303 #define HZ_KEY_F15 304 #define HZ_KEY_F16 305 #define HZ_KEY_F17 306 #define HZ_KEY_F18 307 #define HZ_KEY_F19 308 #define HZ_KEY_F20 309 #define HZ_KEY_F21 310 #define HZ_KEY_F22 311 #define HZ_KEY_F23 312 #define HZ_KEY_F24 313 #define HZ_KEY_F25 314 #define HZ_KEY_KP_0 320 #define HZ_KEY_KP_1 321 #define HZ_KEY_KP_2 322 #define HZ_KEY_KP_3 323 #define HZ_KEY_KP_4 324 #define HZ_KEY_KP_5 325 #define HZ_KEY_KP_6 326 #define HZ_KEY_KP_7 327 #define HZ_KEY_KP_8 328 #define HZ_KEY_KP_9 329 #define HZ_KEY_KP_DECIMAL 330 #define HZ_KEY_KP_DIVIDE 331 #define HZ_KEY_KP_MULTIPLY 332 #define HZ_KEY_KP_SUBTRACT 333 #define HZ_KEY_KP_ADD 334 #define HZ_KEY_KP_ENTER 335 #define HZ_KEY_KP_EQUAL 336 #define HZ_KEY_LEFT_SHIFT 340 #define HZ_KEY_LEFT_CONTROL 341 #define HZ_KEY_LEFT_ALT 342 #define HZ_KEY_LEFT_SUPER 343 #define HZ_KEY_RIGHT_SHIFT 344 #define HZ_KEY_RIGHT_CONTROL 345 #define HZ_KEY_RIGHT_ALT 346 #define HZ_KEY_RIGHT_SUPER 347 #define HZ_KEY_MENU 348
-
MouseButtonCodes
#pragma once // From glfw3.h #define HZ_MOUSE_BUTTON_1 0 #define HZ_MOUSE_BUTTON_2 1 #define HZ_MOUSE_BUTTON_3 2 #define HZ_MOUSE_BUTTON_4 3 #define HZ_MOUSE_BUTTON_5 4 #define HZ_MOUSE_BUTTON_6 5 #define HZ_MOUSE_BUTTON_7 6 #define HZ_MOUSE_BUTTON_8 7 #define HZ_MOUSE_BUTTON_LAST HZ_MOUSE_BUTTON_8 #define HZ_MOUSE_BUTTON_LEFT HZ_MOUSE_BUTTON_1 #define HZ_MOUSE_BUTTON_RIGHT HZ_MOUSE_BUTTON_2 #define HZ_MOUSE_BUTTON_MIDDLE HZ_MOUSE_BUTTON_3
The macro definition values of buttons and mouse are all macro definitions from GLFW.
Test effect
-
Sandbox App
class ExampleLayer : public Hazel::Layer { public: ExampleLayer() : Layer("Example"){ } // 输入轮询 void OnUpdate() override{ //HZ_INFO("ExampleLayer::Update"); if (Hazel::Input::IsKeyPressed(HZ_KEY_A)) { // 使用键值,检测A键是否按下 HZ_TRACE("A键按下(poll)"); } } // 事件系统 void OnEvent(Hazel::Event& event) override{ //HZ_TRACE("examplayer:{0}", event); if (event.GetEventType() == Hazel::EventType::KeyPressed) { Hazel::KeyPressedEvent& e = (Hazel::KeyPressedEvent&)event; if (e.GetKeyCode() == HZ_KEY_A) { // 使用键值,检测A键是否按下 HZ_TRACE("A键按下(event)"); } HZ_TRACE("{0}",(char)e.GetKeyCode()); } } };
It can be seen that both input polling and event systems can use custom key values to determine whether a key is pressed