vsg: include/vsg/platform/win32/Win32_Window.h Source File

vsg 1.1.13 VulkanSceneGraph library

Loading...
Searching...
No Matches
Win32_Window.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission 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:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE 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.
12
13</editor-fold> */
14
15#define VK_USE_PLATFORM_WIN32_KHR
16
17#ifndef NOMINMAX
18# define NOMINMAX
19#endif
20
21#include <vsg/app/Window.h>
22#include <vsg/ui/KeyEvent.h>
23#include <vsg/ui/PointerEvent.h>
24
25#include <windows.h>
26#include <windowsx.h>
27
28#include <vulkan/vulkan_win32.h>
29
30namespace vsgWin32
31{
32 class VSG_DECLSPEC KeyboardMap : public vsg::Object
33 {
34 public:
35 KeyboardMap();
36
37 using VirtualKeyToKeySymbolMap = std::map<uint16_t, vsg::KeySymbol>;
38
39 bool getKeySymbol(WPARAM wParam, LPARAM lParam, vsg::KeySymbol& keySymbol, vsg::KeySymbol& modifiedKeySymbol, vsg::KeyModifier& keyModifier)
40 {
41 uint16_t modifierMask = 0;
42
43 // see https://learn.microsoft.com/en-us/windows/win32/inputdev/about-keyboard-input#keystroke-message-flags
44 WORD keyFlags = HIWORD(lParam);
45 WORD scanCode = LOBYTE(keyFlags); // scan code
46 BOOL isExtendedKey = (keyFlags & KF_EXTENDED) == KF_EXTENDED; // extended-key flag, 1 if scancode has 0xE0 prefix
47
48 if (isExtendedKey)
49 scanCode = MAKEWORD(scanCode, 0xE0);
50
51 uint32_t virtualKey = ::MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, ::GetKeyboardLayout(0));
52 auto itr = _vk2vsg.find(virtualKey);
53
54 if (itr == _vk2vsg.end())
55 {
56 // What ever the code was in lParam should translate to a Virtual Key that we know of in _vk2vsg
57 // If we cannot find it, we simply return.
58 return false;
59 }
60
61 // This is the base-key that was pressed. (i.e., the VSG enum of the physical key pressed).
62 keySymbol = itr->second;
63
64 // Look for any modifiers that may be active.
65 BYTE keyState[256];
66 if (virtualKey == 0 || !::GetKeyboardState(keyState))
67 {
68 // if virtualKey was undefined or we could not get the keyboard state, simply return.
69 return false;
70 }
71
72 // If any of the specific left/right modifier keys are active
73 // add the side-independent vsg modifier to the modifier Mask
74 switch (virtualKey)
75 {
76 case VK_LSHIFT:
77 case VK_RSHIFT:
78 modifierMask |= vsg::KeyModifier::MODKEY_Shift;
79 break;
80
81 case VK_LCONTROL:
82 case VK_RCONTROL:
83 modifierMask |= vsg::KeyModifier::MODKEY_Control;
84 break;
85
86 case VK_LMENU:
87 case VK_RMENU:
88 modifierMask |= vsg::KeyModifier::MODKEY_Alt;
89 break;
90
91 default:
92 virtualKey = static_cast<uint32_t>(wParam);
93 break;
94 }
95
96 // Check if caps lock or numlock is toggled.
97 if (keyState[VK_CAPITAL] & 0x01) modifierMask |= vsg::KeyModifier::MODKEY_CapsLock;
98 if (keyState[VK_NUMLOCK] & 0x01) modifierMask |= vsg::KeyModifier::MODKEY_NumLock;
99
100 // Check if the modifier keys are down (these are non-toggle keys, so the high-order bit is relevant!)
101 // again, vsg only has a side-independent modifier
102 if (keyState[VK_LSHIFT] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Shift;
103 if (keyState[VK_RSHIFT] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Shift;
104 if (keyState[VK_LCONTROL] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Control;
105 if (keyState[VK_RCONTROL] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Control;
106 if (keyState[VK_LMENU] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Alt;
107 if (keyState[VK_RMENU] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Alt;
108
109 // This is the final keyModifier
110 keyModifier = static_cast<vsg::KeyModifier>(modifierMask);
111
112 // The actual keystroke is what we get after the ::ToAscii call
113 char asciiKey[2];
114 int32_t numChars = ::ToAsciiEx(static_cast<UINT>(wParam), scanCode, keyState, reinterpret_cast<WORD*>(asciiKey), 0, ::GetKeyboardLayout(0));
115 if (numChars == 1)
116 {
117 // it is indeed an ascii character. 0-127
118 modifiedKeySymbol = static_cast<vsg::KeySymbol>(asciiKey[0]);
119 }
120 else
121 {
122 // otherwise treat the modifiedKeySymbol as the same as the keySymbol.
123 modifiedKeySymbol = keySymbol;
124 }
125
126 return true;
127 }
128
129 protected:
130 VirtualKeyToKeySymbolMap _vk2vsg;
131 };
132
133 inline vsg::ButtonMask getButtonMask(WPARAM wParam)
134 {
135 auto mask = (wParam & MK_LBUTTON ? vsg::ButtonMask::BUTTON_MASK_1 : 0) | (wParam & MK_MBUTTON ? vsg::ButtonMask::BUTTON_MASK_2 : 0) | (wParam & MK_RBUTTON ? vsg::ButtonMask::BUTTON_MASK_3 : 0) |
136 (wParam & MK_XBUTTON1 ? vsg::ButtonMask::BUTTON_MASK_4 : 0) | (wParam & MK_XBUTTON2 ? vsg::ButtonMask::BUTTON_MASK_5 : 0);
137 return static_cast<vsg::ButtonMask>(mask);
138 }
139
140 inline uint32_t getButtonDownEventDetail(UINT buttonMsg, WORD wParamHi)
141 {
142 switch (buttonMsg)
143 {
144 case WM_LBUTTONDBLCLK:
145 case WM_LBUTTONDOWN: return 1;
146 case WM_MBUTTONDBLCLK:
147 case WM_MBUTTONDOWN: return 2;
148 case WM_RBUTTONDBLCLK:
149 case WM_RBUTTONDOWN: return 3;
150 case WM_XBUTTONDBLCLK:
151 case WM_XBUTTONDOWN:
152 if (wParamHi == XBUTTON1)
153 return 4;
154 else if (wParamHi == XBUTTON2)
155 return 5;
156 else
157 return 0;
158 default:
159 return 0;
160 }
161 }
162
163 inline uint32_t getButtonUpEventDetail(UINT buttonMsg, WORD wParamHi)
164 {
165 switch (buttonMsg)
166 {
167 case WM_LBUTTONUP: return 1;
168 case WM_MBUTTONUP: return 2;
169 case WM_RBUTTONUP: return 3;
170 case WM_XBUTTONUP:
171 if (wParamHi == XBUTTON1)
172 return 4;
173 else if (wParamHi == XBUTTON2)
174 return 5;
175 else
176 return 0;
177 default:
178 return 0;
179 }
180 }
181
183 class VSG_DECLSPEC Win32_Window : public vsg::Inherit<vsg::Window, Win32_Window>
184 {
185 public:
186 Win32_Window(vsg::ref_ptr<vsg::WindowTraits> traits);
187 Win32_Window() = delete;
188 Win32_Window(const Win32_Window&) = delete;
189 Win32_Window operator=(const Win32_Window&) = delete;
190
191 const char* instanceExtensionSurfaceName() const override { return VK_KHR_WIN32_SURFACE_EXTENSION_NAME; }
192
193 bool valid() const override { return _window; }
194
195 bool visible() const override;
196
197 void releaseWindow() override;
198
199 bool pollEvents(vsg::UIEvents& events) override;
200
201 void resize() override;
202
203 operator HWND() const { return _window; }
204
206 virtual bool handleWin32Messages(UINT msg, WPARAM wParam, LPARAM lParam);
207
208 protected:
209 virtual ~Win32_Window();
210
211 void _initSurface() override;
212
213 HWND _window;
214 bool _windowMapped = false;
215
216 vsg::ref_ptr<KeyboardMap> _keyboard;
217 };
218
220 extern VSG_DECLSPEC vsg::Exception getLastErrorAsException(const std::string_view& prefix = {});
221
222} // namespace vsgWin32
223
224EVSG_type_name(vsgWin32::Win32_Window);
vsg::Inherit
Definition Inherit.h:28
vsg::Object
Definition Object.h:60
vsg::ref_ptr
Definition ref_ptr.h:22
vsgWin32::Win32_Window
Win32_Window implements Win32 specific window creation, event handling and vulkan Surface setup.
Definition Win32_Window.h:184
vsgWin32::Win32_Window::pollEvents
bool pollEvents(vsg::UIEvents &events) override
get the list of events since the last pollEvents() call by splicing bufferEvents with polled windowin...
vsgWin32::Win32_Window::handleWin32Messages
virtual bool handleWin32Messages(UINT msg, WPARAM wParam, LPARAM lParam)
handle Win32 event messages, return true if handled.
vsgWin32::Win32_Window::releaseWindow
void releaseWindow() override
vsg::Exception
Definition Exception.h:23