diff options
author | crupest <crupest@outlook.com> | 2020-03-29 23:10:00 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-03-29 23:10:00 +0800 |
commit | 85322feaa9f646bf9e6f35f57f6d08244b298a46 (patch) | |
tree | d41211c4f2913921b550e798f3ac05c45b3f4c1a | |
parent | 671860a02aa1ecc41eac1cf1e0f28c504bf9c37c (diff) | |
download | cru-85322feaa9f646bf9e6f35f57f6d08244b298a46.tar.gz cru-85322feaa9f646bf9e6f35f57f6d08244b298a46.tar.bz2 cru-85322feaa9f646bf9e6f35f57f6d08244b298a46.zip |
...
-rw-r--r-- | include/cru/platform/native/base.hpp | 7 | ||||
-rw-r--r-- | include/cru/platform/native/key_code.hpp | 89 | ||||
-rw-r--r-- | include/cru/platform/native/keyboard.hpp | 120 | ||||
-rw-r--r-- | include/cru/platform/native/window.hpp | 4 | ||||
-rw-r--r-- | include/cru/win/native/keyboard.hpp | 8 | ||||
-rw-r--r-- | include/cru/win/native/window.hpp | 24 | ||||
-rw-r--r-- | src/platform/native/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/win/native/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/win/native/keyboard.cpp | 66 |
9 files changed, 222 insertions, 100 deletions
diff --git a/include/cru/platform/native/base.hpp b/include/cru/platform/native/base.hpp index d62dc56b..5ebd98d6 100644 --- a/include/cru/platform/native/base.hpp +++ b/include/cru/platform/native/base.hpp @@ -2,6 +2,7 @@ #include "cru/common/base.hpp" #include "cru/common/bitmask.hpp" #include "cru/platform/graph/base.hpp" +#include "keyboard.hpp" namespace cru::platform::native { struct ICursor; @@ -35,6 +36,12 @@ enum class SystemCursorType { struct NativeMouseButtonEventArgs { MouseButton button; Point point; + KeyModifier modifier; +}; + +struct NativeKeyEventArgs { + KeyCode key; + KeyModifier modifier; }; enum class FocusChangeType { Gain, Lost }; diff --git a/include/cru/platform/native/key_code.hpp b/include/cru/platform/native/key_code.hpp deleted file mode 100644 index d4e1ee8f..00000000 --- a/include/cru/platform/native/key_code.hpp +++ /dev/null @@ -1,89 +0,0 @@ -#pragma once - -namespace cru::platform::native { -enum class KeyCode { - LeftButton, - MiddleButton, - RightButton, - Escape, - F1, - F2, - F3, - F4, - F5, - F6, - F7, - F8, - F9, - F10, - F11, - F12, - N1, - N2, - N3, - N4, - N5, - N6, - N7, - N8, - N9, - N0, - A, - B, - C, - D, - E, - F, - G, - H, - I, - J, - K, - L, - M, - N, - O, - P, - Q, - R, - S, - T, - U, - V, - W, - X, - Y, - Z, - GraveAccent, - Tab, - CapsLock, - LeftShift, - LeftControl, - Win, - LeftAlt, - Minus, - Equal, - Backspace, - LeftSquareBracket, - RightSquareBracket, - BackSlash, - Semicolon, - Quote, - Comma, - Point, - Slash, - RightShift, - RightAlt, - RightCtrl, - Insert, - Delete, - Home, - End, - PageUp, - PageDown, - Up, - Left, - Down, - Right -}; -} diff --git a/include/cru/platform/native/keyboard.hpp b/include/cru/platform/native/keyboard.hpp new file mode 100644 index 00000000..8b5e6162 --- /dev/null +++ b/include/cru/platform/native/keyboard.hpp @@ -0,0 +1,120 @@ +#pragma once +#include "cru/common/bitmask.hpp" + +namespace cru::platform::native { +// Because of the complexity of keyboard layout, I only add code in US keyboard +// layout, the most widely used layout in China. We should try to make it easy +// to add new keyboard layout. +enum class KeyCode { + Unknown, + LeftButton, + MiddleButton, + RightButton, + Escape, + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + N0, + N1, + N2, + N3, + N4, + N5, + N6, + N7, + N8, + N9, + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + GraveAccent, + Tab, + CapsLock, + LeftShift, + LeftCtrl, + LeftSuper, + LeftAlt, + Minus, + Equal, + Backspace, + LeftSquareBracket, + RightSquareBracket, + BackSlash, + Semicolon, + Quote, + Comma, + Period, + Slash, + RightShift, + RightCtrl, + RightSuper, + RightAlt, + Insert, + Delete, + Home, + End, + PageUp, + PageDown, + Up, + Left, + Down, + Right, + PrintScreen, + ScrollLock, + Pause, + NumPad0, + NumPad1, + NumPad2, + NumPad3, + NumPad4, + NumPad5, + NumPad6, + NumPad7, + NumPad8, + NumPad9 +}; + +namespace details { +struct TagKeyModifier {}; +} // namespace details + +using KeyModifier = Bitmask<details::TagKeyModifier>; + +namespace key_modifiers { +constexpr KeyModifier shift{0b1}; +constexpr KeyModifier ctrl{0b10}; +constexpr KeyModifier alt{0b100}; +} // namespace key_modifiers +} // namespace cru::platform::native diff --git a/include/cru/platform/native/window.hpp b/include/cru/platform/native/window.hpp index 0d40702f..dcb9effa 100644 --- a/include/cru/platform/native/window.hpp +++ b/include/cru/platform/native/window.hpp @@ -55,8 +55,8 @@ struct INativeWindow : virtual INativeResource { virtual IEvent<Point>* MouseMoveEvent() = 0; virtual IEvent<NativeMouseButtonEventArgs>* MouseDownEvent() = 0; virtual IEvent<NativeMouseButtonEventArgs>* MouseUpEvent() = 0; - virtual IEvent<int>* KeyDownEvent() = 0; - virtual IEvent<int>* KeyUpEvent() = 0; + virtual IEvent<NativeKeyEventArgs>* KeyDownEvent() = 0; + virtual IEvent<NativeKeyEventArgs>* KeyUpEvent() = 0; virtual IEvent<std::string>* CharEvent() = 0; }; diff --git a/include/cru/win/native/keyboard.hpp b/include/cru/win/native/keyboard.hpp new file mode 100644 index 00000000..bf59e519 --- /dev/null +++ b/include/cru/win/native/keyboard.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/platform/native/keyboard.hpp" + +namespace cru::platform::native::win { +KeyCode VirtualKeyToKeyCode(int virtual_key); +} diff --git a/include/cru/win/native/window.hpp b/include/cru/win/native/window.hpp index d5f41e47..819fbfd9 100644 --- a/include/cru/win/native/window.hpp +++ b/include/cru/win/native/window.hpp @@ -73,8 +73,12 @@ class WinNativeWindow : public WinNativeResource, public virtual INativeWindow { override { return &mouse_up_event_; } - IEvent<int>* KeyDownEvent() override { return &key_down_event_; } - IEvent<int>* KeyUpEvent() override { return &key_up_event_; } + IEvent<platform::native::NativeKeyEventArgs>* KeyDownEvent() override { + return &key_down_event_; + } + IEvent<platform::native::NativeKeyEventArgs>* KeyUpEvent() override { + return &key_up_event_; + } IEvent<std::string>* CharEvent() override { return &char_event_; }; IEvent<WindowNativeMessageEventArgs&>* NativeMessageEvent() { @@ -106,12 +110,16 @@ class WinNativeWindow : public WinNativeResource, public virtual INativeWindow { void OnMouseMoveInternal(POINT point); void OnMouseLeaveInternal(); - void OnMouseDownInternal(platform::native::MouseButton button, POINT point); - void OnMouseUpInternal(platform::native::MouseButton button, POINT point); + void OnMouseDownInternal(platform::native::MouseButton button, POINT point, + platform::native::KeyModifier modifier); + void OnMouseUpInternal(platform::native::MouseButton button, POINT point, + platform::native::KeyModifier modifier); void OnMouseWheelInternal(short delta, POINT point); - void OnKeyDownInternal(int virtual_code); - void OnKeyUpInternal(int virtual_code); + void OnKeyDownInternal(int virtual_code, + platform::native::KeyModifier modifier); + void OnKeyUpInternal(int virtual_code, + platform::native::KeyModifier modifier); void OnCharInternal(wchar_t c); void OnActivatedInternal(); @@ -147,8 +155,8 @@ class WinNativeWindow : public WinNativeResource, public virtual INativeWindow { Event<Point> mouse_move_event_; Event<platform::native::NativeMouseButtonEventArgs> mouse_down_event_; Event<platform::native::NativeMouseButtonEventArgs> mouse_up_event_; - Event<int> key_down_event_; - Event<int> key_up_event_; + Event<platform::native::NativeKeyEventArgs> key_down_event_; + Event<platform::native::NativeKeyEventArgs> key_up_event_; Event<std::string> char_event_; Event<WindowNativeMessageEventArgs&> native_message_event_; diff --git a/src/platform/native/CMakeLists.txt b/src/platform/native/CMakeLists.txt index 5150415a..c280ddd0 100644 --- a/src/platform/native/CMakeLists.txt +++ b/src/platform/native/CMakeLists.txt @@ -5,7 +5,7 @@ add_library(cru_platform_native STATIC target_sources(cru_platform_native PUBLIC ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/base.hpp ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/cursor.hpp - ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/key_code.hpp + ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/keyboard.hpp ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/window.hpp ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/ui_application.hpp ) diff --git a/src/win/native/CMakeLists.txt b/src/win/native/CMakeLists.txt index 979b54df..82994d67 100644 --- a/src/win/native/CMakeLists.txt +++ b/src/win/native/CMakeLists.txt @@ -9,6 +9,7 @@ add_library(cru_win_native STATIC cursor.cpp god_window.cpp + keyboard.cpp timer.cpp ui_application.cpp window.cpp @@ -21,6 +22,7 @@ target_sources(cru_win_native PUBLIC ${CRU_WIN_NATIVE_INCLUDE_DIR}/cursor.hpp ${CRU_WIN_NATIVE_INCLUDE_DIR}/exception.hpp ${CRU_WIN_NATIVE_INCLUDE_DIR}/god_window.hpp + ${CRU_WIN_NATIVE_INCLUDE_DIR}/keyboard.hpp ${CRU_WIN_NATIVE_INCLUDE_DIR}/resource.hpp ${CRU_WIN_NATIVE_INCLUDE_DIR}/ui_application.hpp ${CRU_WIN_NATIVE_INCLUDE_DIR}/window.hpp diff --git a/src/win/native/keyboard.cpp b/src/win/native/keyboard.cpp new file mode 100644 index 00000000..b73d0ffb --- /dev/null +++ b/src/win/native/keyboard.cpp @@ -0,0 +1,66 @@ +#include "cru/win/native/keyboard.hpp" + +namespace cru::platform::native::win { +KeyCode VirtualKeyToKeyCode(int virtual_key) { + if (virtual_key >= 0x30 && virtual_key <= 0x39) { + return KeyCode{static_cast<int>(KeyCode::N0) + (virtual_key - 0x30)}; + } else if (virtual_key >= 0x41 && virtual_key <= 0x5a) { + return KeyCode{static_cast<int>(KeyCode::A) + (virtual_key - 0x41)}; + } else if (virtual_key >= VK_NUMPAD0 && virtual_key <= VK_NUMPAD9) { + return KeyCode{static_cast<int>(KeyCode::NumPad0) + + (virtual_key - VK_NUMPAD0)}; + } else if (virtual_key >= VK_F1 && virtual_key <= VK_F12) { + return KeyCode{static_cast<int>(KeyCode::F1) + (virtual_key - VK_F1)}; + } else { + switch (virtual_key) { +#define CRU_MAP_KEY(virtual_key, keycode) \ + case virtual_key: \ + return KeyCode::keycode; + + CRU_MAP_KEY(VK_LBUTTON, LeftButton) + CRU_MAP_KEY(VK_MBUTTON, MiddleButton) + CRU_MAP_KEY(VK_RBUTTON, RightButton) + CRU_MAP_KEY(VK_ESCAPE, Escape) + CRU_MAP_KEY(VK_OEM_3, GraveAccent) + CRU_MAP_KEY(VK_TAB, Tab) + CRU_MAP_KEY(VK_CAPITAL, CapsLock) + CRU_MAP_KEY(VK_LSHIFT, LeftShift) + CRU_MAP_KEY(VK_LCONTROL, LeftCtrl) + CRU_MAP_KEY(VK_LWIN, LeftSuper) + CRU_MAP_KEY(VK_LMENU, LeftAlt) + CRU_MAP_KEY(VK_OEM_MINUS, Minus) + CRU_MAP_KEY(VK_OEM_PLUS, Equal) + CRU_MAP_KEY(VK_BACK, Backspace) + CRU_MAP_KEY(VK_OEM_4, LeftSquareBracket) + CRU_MAP_KEY(VK_OEM_6, RightSquareBracket) + CRU_MAP_KEY(VK_OEM_5, BackSlash) + CRU_MAP_KEY(VK_OEM_1, Semicolon) + CRU_MAP_KEY(VK_OEM_7, Quote) + CRU_MAP_KEY(VK_OEM_COMMA, Comma) + CRU_MAP_KEY(VK_OEM_PERIOD, Period) + CRU_MAP_KEY(VK_OEM_2, Slash) + CRU_MAP_KEY(VK_RSHIFT, RightShift) + CRU_MAP_KEY(VK_RCONTROL, RightCtrl) + CRU_MAP_KEY(VK_RWIN, RightSuper) + CRU_MAP_KEY(VK_RMENU, RightAlt) + CRU_MAP_KEY(VK_INSERT, Insert) + CRU_MAP_KEY(VK_DELETE, Delete) + CRU_MAP_KEY(VK_HOME, Home) + CRU_MAP_KEY(VK_END, End) + CRU_MAP_KEY(VK_PRIOR, PageUp) + CRU_MAP_KEY(VK_NEXT, PageDown) + CRU_MAP_KEY(VK_UP, Up) + CRU_MAP_KEY(VK_LEFT, Left) + CRU_MAP_KEY(VK_DOWN, Down) + CRU_MAP_KEY(VK_RIGHT, Right) + CRU_MAP_KEY(VK_SNAPSHOT, PrintScreen) + CRU_MAP_KEY(VK_PAUSE, Pause) + +#undef CRU_MAP_KEY + + default: + return KeyCode::Unknown; + } + } +} +} // namespace cru::platform::native::win |