Keyboard-winrt.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. * Portions Copyright (c) Microsoft Open Technologies, Inc.
  5. * All Rights Reserved
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "platform/winrt/Keyboard-winrt.h"
  24. #include "base/CCEventKeyboard.h"
  25. #include "platform/winrt/CCGLViewImpl-winrt.h"
  26. #include "base/CCIMEDispatcher.h"
  27. #include "base/CCDirector.h"
  28. #include "base/CCEventDispatcher.h"
  29. using namespace cocos2d;
  30. using namespace Platform;
  31. using namespace Windows::System;
  32. using namespace Windows::System::Threading;
  33. using namespace Windows::UI::Core;
  34. using namespace Windows::UI::Input;
  35. using namespace Windows::UI::Xaml;
  36. using namespace Windows::UI::Xaml::Controls;
  37. using namespace Windows::UI::Xaml::Input;
  38. NS_CC_BEGIN
  39. struct keyCodeItem
  40. {
  41. int key;
  42. EventKeyboard::KeyCode keyCode;
  43. };
  44. static std::map<int, EventKeyboard::KeyCode> g_keyCodeMap;
  45. // http://www.kbdedit.com/manual/low_level_vk_list.html
  46. // https://msdn.microsoft.com/library/windows/apps/windows.system.virtualkey.aspx
  47. static keyCodeItem g_keyCodeStructArray [] = {
  48. /* The unknown key */
  49. { (int) VirtualKey::None, EventKeyboard::KeyCode::KEY_NONE },
  50. /* Printable keys */
  51. { (int) VirtualKey::Space, EventKeyboard::KeyCode::KEY_SPACE },
  52. { (int) VK_OEM_7, EventKeyboard::KeyCode::KEY_APOSTROPHE },
  53. { (int) VK_OEM_COMMA, EventKeyboard::KeyCode::KEY_COMMA },
  54. { (int) VK_OEM_MINUS, EventKeyboard::KeyCode::KEY_MINUS },
  55. { (int) VK_OEM_PERIOD, EventKeyboard::KeyCode::KEY_PERIOD },
  56. { (int) VK_OEM_2, EventKeyboard::KeyCode::KEY_SLASH },
  57. { (int) VK_OEM_3, EventKeyboard::KeyCode::KEY_TILDE },
  58. { (int) VirtualKey::Number0, EventKeyboard::KeyCode::KEY_0 },
  59. { (int) VirtualKey::Number1, EventKeyboard::KeyCode::KEY_1 },
  60. { (int) VirtualKey::Number2, EventKeyboard::KeyCode::KEY_2 },
  61. { (int) VirtualKey::Number3, EventKeyboard::KeyCode::KEY_3 },
  62. { (int) VirtualKey::Number4, EventKeyboard::KeyCode::KEY_4 },
  63. { (int) VirtualKey::Number5, EventKeyboard::KeyCode::KEY_5 },
  64. { (int) VirtualKey::Number6, EventKeyboard::KeyCode::KEY_6 },
  65. { (int) VirtualKey::Number7, EventKeyboard::KeyCode::KEY_7 },
  66. { (int) VirtualKey::Number8, EventKeyboard::KeyCode::KEY_8 },
  67. { (int) VirtualKey::Number9, EventKeyboard::KeyCode::KEY_9 },
  68. { (int) VK_OEM_1, EventKeyboard::KeyCode::KEY_SEMICOLON },
  69. { (int) VK_OEM_PLUS, EventKeyboard::KeyCode::KEY_EQUAL },
  70. { (int) VirtualKey::A, EventKeyboard::KeyCode::KEY_A },
  71. { (int) VirtualKey::B, EventKeyboard::KeyCode::KEY_B },
  72. { (int) VirtualKey::C, EventKeyboard::KeyCode::KEY_C },
  73. { (int) VirtualKey::D, EventKeyboard::KeyCode::KEY_D },
  74. { (int) VirtualKey::E, EventKeyboard::KeyCode::KEY_E },
  75. { (int) VirtualKey::F, EventKeyboard::KeyCode::KEY_F },
  76. { (int) VirtualKey::G, EventKeyboard::KeyCode::KEY_G },
  77. { (int) VirtualKey::H, EventKeyboard::KeyCode::KEY_H },
  78. { (int) VirtualKey::I, EventKeyboard::KeyCode::KEY_I },
  79. { (int) VirtualKey::J, EventKeyboard::KeyCode::KEY_J },
  80. { (int) VirtualKey::K, EventKeyboard::KeyCode::KEY_K },
  81. { (int) VirtualKey::L, EventKeyboard::KeyCode::KEY_L },
  82. { (int) VirtualKey::M, EventKeyboard::KeyCode::KEY_M },
  83. { (int) VirtualKey::N, EventKeyboard::KeyCode::KEY_N },
  84. { (int) VirtualKey::O, EventKeyboard::KeyCode::KEY_O },
  85. { (int) VirtualKey::P, EventKeyboard::KeyCode::KEY_P },
  86. { (int) VirtualKey::Q, EventKeyboard::KeyCode::KEY_Q },
  87. { (int) VirtualKey::R, EventKeyboard::KeyCode::KEY_R },
  88. { (int) VirtualKey::S, EventKeyboard::KeyCode::KEY_S },
  89. { (int) VirtualKey::T, EventKeyboard::KeyCode::KEY_T },
  90. { (int) VirtualKey::U, EventKeyboard::KeyCode::KEY_U },
  91. { (int) VirtualKey::V, EventKeyboard::KeyCode::KEY_V },
  92. { (int) VirtualKey::W, EventKeyboard::KeyCode::KEY_W },
  93. { (int) VirtualKey::X, EventKeyboard::KeyCode::KEY_X },
  94. { (int) VirtualKey::Y, EventKeyboard::KeyCode::KEY_Y },
  95. { (int) VirtualKey::Z, EventKeyboard::KeyCode::KEY_Z },
  96. { VK_OEM_4, EventKeyboard::KeyCode::KEY_LEFT_BRACKET },
  97. { VK_OEM_5, EventKeyboard::KeyCode::KEY_BACK_SLASH },
  98. { VK_OEM_6, EventKeyboard::KeyCode::KEY_RIGHT_BRACKET },
  99. // { GLFW_KEY_GRAVE_ACCENT , EventKeyboard::KeyCode::KEY_GRAVE },
  100. /* Function keys */
  101. { (int) VirtualKey::Escape, EventKeyboard::KeyCode::KEY_ESCAPE },
  102. { (int) VirtualKey::Enter, EventKeyboard::KeyCode::KEY_ENTER },
  103. { (int) VirtualKey::Tab, EventKeyboard::KeyCode::KEY_TAB },
  104. { (int) VirtualKey::Back, EventKeyboard::KeyCode::KEY_BACKSPACE },
  105. { (int) VirtualKey::Insert, EventKeyboard::KeyCode::KEY_INSERT },
  106. { (int) VirtualKey::Delete, EventKeyboard::KeyCode::KEY_DELETE },
  107. { (int) VirtualKey::Right, EventKeyboard::KeyCode::KEY_RIGHT_ARROW },
  108. { (int) VirtualKey::Left, EventKeyboard::KeyCode::KEY_LEFT_ARROW },
  109. { (int) VirtualKey::Down, EventKeyboard::KeyCode::KEY_DOWN_ARROW },
  110. { (int) VirtualKey::Up, EventKeyboard::KeyCode::KEY_UP_ARROW },
  111. { VK_PRIOR, EventKeyboard::KeyCode::KEY_PG_UP },
  112. { VK_NEXT, EventKeyboard::KeyCode::KEY_PG_DOWN },
  113. { VK_HOME, EventKeyboard::KeyCode::KEY_HOME },
  114. { VK_END, EventKeyboard::KeyCode::KEY_END },
  115. { VK_CAPITAL, EventKeyboard::KeyCode::KEY_CAPS_LOCK },
  116. { VK_SCROLL, EventKeyboard::KeyCode::KEY_SCROLL_LOCK },
  117. { VK_NUMLOCK, EventKeyboard::KeyCode::KEY_NUM_LOCK },
  118. { VK_SNAPSHOT, EventKeyboard::KeyCode::KEY_PRINT },
  119. { VK_PAUSE, EventKeyboard::KeyCode::KEY_PAUSE },
  120. { (int) VirtualKey::F1, EventKeyboard::KeyCode::KEY_F1 },
  121. { (int) VirtualKey::F2, EventKeyboard::KeyCode::KEY_F2 },
  122. { (int) VirtualKey::F3, EventKeyboard::KeyCode::KEY_F3 },
  123. { (int) VirtualKey::F4, EventKeyboard::KeyCode::KEY_F4 },
  124. { (int) VirtualKey::F5, EventKeyboard::KeyCode::KEY_F5 },
  125. { (int) VirtualKey::F6, EventKeyboard::KeyCode::KEY_F6 },
  126. { (int) VirtualKey::F7, EventKeyboard::KeyCode::KEY_F7 },
  127. { (int) VirtualKey::F8, EventKeyboard::KeyCode::KEY_F8 },
  128. { (int) VirtualKey::F9, EventKeyboard::KeyCode::KEY_F9 },
  129. { (int) VirtualKey::F10, EventKeyboard::KeyCode::KEY_F10 },
  130. { (int) VirtualKey::F11, EventKeyboard::KeyCode::KEY_F11 },
  131. { (int) VirtualKey::F12, EventKeyboard::KeyCode::KEY_F12 },
  132. { (int) VirtualKey::F13, EventKeyboard::KeyCode::KEY_NONE },
  133. { (int) VirtualKey::F14, EventKeyboard::KeyCode::KEY_NONE },
  134. { (int) VirtualKey::F15, EventKeyboard::KeyCode::KEY_NONE },
  135. { (int) VirtualKey::F16, EventKeyboard::KeyCode::KEY_NONE },
  136. { (int) VirtualKey::F17, EventKeyboard::KeyCode::KEY_NONE },
  137. { (int) VirtualKey::F18, EventKeyboard::KeyCode::KEY_NONE },
  138. { (int) VirtualKey::F19, EventKeyboard::KeyCode::KEY_NONE },
  139. { (int) VirtualKey::F20, EventKeyboard::KeyCode::KEY_NONE },
  140. { (int) VirtualKey::F21, EventKeyboard::KeyCode::KEY_NONE },
  141. { (int) VirtualKey::F22, EventKeyboard::KeyCode::KEY_NONE },
  142. { (int) VirtualKey::F23, EventKeyboard::KeyCode::KEY_NONE },
  143. { (int) VirtualKey::F24, EventKeyboard::KeyCode::KEY_NONE },
  144. { (int) VirtualKey::NumberPad0, EventKeyboard::KeyCode::KEY_0 },
  145. { (int) VirtualKey::NumberPad1, EventKeyboard::KeyCode::KEY_1 },
  146. { (int) VirtualKey::NumberPad2, EventKeyboard::KeyCode::KEY_2 },
  147. { (int) VirtualKey::NumberPad3, EventKeyboard::KeyCode::KEY_3 },
  148. { (int) VirtualKey::NumberPad4, EventKeyboard::KeyCode::KEY_4 },
  149. { (int) VirtualKey::NumberPad5, EventKeyboard::KeyCode::KEY_5 },
  150. { (int) VirtualKey::NumberPad6, EventKeyboard::KeyCode::KEY_6 },
  151. { (int) VirtualKey::NumberPad7, EventKeyboard::KeyCode::KEY_7 },
  152. { (int) VirtualKey::NumberPad8, EventKeyboard::KeyCode::KEY_8 },
  153. { (int) VirtualKey::NumberPad9, EventKeyboard::KeyCode::KEY_9 },
  154. #if 0
  155. { GLFW_KEY_KP_1, EventKeyboard::KeyCode::KEY_1 },
  156. { GLFW_KEY_KP_2, EventKeyboard::KeyCode::KEY_2 },
  157. { GLFW_KEY_KP_3, EventKeyboard::KeyCode::KEY_3 },
  158. { GLFW_KEY_KP_4, EventKeyboard::KeyCode::KEY_4 },
  159. { GLFW_KEY_KP_5, EventKeyboard::KeyCode::KEY_5 },
  160. { GLFW_KEY_KP_6, EventKeyboard::KeyCode::KEY_6 },
  161. { GLFW_KEY_KP_7, EventKeyboard::KeyCode::KEY_7 },
  162. { GLFW_KEY_KP_8, EventKeyboard::KeyCode::KEY_8 },
  163. { GLFW_KEY_KP_9, EventKeyboard::KeyCode::KEY_9 },
  164. #endif
  165. { (int) VirtualKey::Decimal, EventKeyboard::KeyCode::KEY_PERIOD },
  166. { (int) VirtualKey::Divide, EventKeyboard::KeyCode::KEY_KP_DIVIDE },
  167. { (int) VirtualKey::Multiply, EventKeyboard::KeyCode::KEY_KP_MULTIPLY },
  168. { (int) VirtualKey::Subtract, EventKeyboard::KeyCode::KEY_KP_MINUS },
  169. { (int) VirtualKey::Add, EventKeyboard::KeyCode::KEY_KP_PLUS },
  170. //{ GLFW_KEY_KP_ENTER , EventKeyboard::KeyCode::KEY_KP_ENTER },
  171. //{ GLFW_KEY_KP_EQUAL , EventKeyboard::KeyCode::KEY_EQUAL },
  172. { (int) VirtualKey::Shift, EventKeyboard::KeyCode::KEY_LEFT_SHIFT },
  173. { (int) VirtualKey::Control, EventKeyboard::KeyCode::KEY_LEFT_CTRL },
  174. { VK_LMENU, EventKeyboard::KeyCode::KEY_LEFT_ALT },
  175. { (int) VirtualKey::LeftWindows, EventKeyboard::KeyCode::KEY_HYPER },
  176. { (int) VirtualKey::RightShift, EventKeyboard::KeyCode::KEY_RIGHT_SHIFT },
  177. { (int) VirtualKey::RightControl, EventKeyboard::KeyCode::KEY_RIGHT_CTRL },
  178. { VK_RMENU, EventKeyboard::KeyCode::KEY_RIGHT_ALT },
  179. { (int) VirtualKey::RightWindows, EventKeyboard::KeyCode::KEY_HYPER },
  180. { (int) VirtualKey::Menu, EventKeyboard::KeyCode::KEY_MENU },
  181. { (int) VirtualKey::LeftMenu, EventKeyboard::KeyCode::KEY_MENU },
  182. { (int) VirtualKey::RightMenu, EventKeyboard::KeyCode::KEY_MENU }
  183. };
  184. KeyBoardWinRT::KeyBoardWinRT()
  185. {
  186. g_keyCodeMap.clear();
  187. for (auto& item : g_keyCodeStructArray)
  188. {
  189. g_keyCodeMap[item.key] = item.keyCode;
  190. }
  191. }
  192. KeyBoardWinRT::~KeyBoardWinRT()
  193. {
  194. }
  195. void KeyBoardWinRT::ShowKeyboard(Platform::String^ text)
  196. {
  197. auto panel = cocos2d::GLViewImpl::sharedOpenGLView()->getPanel();
  198. auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
  199. if (dispatcher && panel)
  200. {
  201. // run on main UI thread
  202. dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, text, panel]()
  203. {
  204. if (m_textBox == nullptr)
  205. {
  206. m_textBox = ref new TextBox();
  207. m_textBox->Opacity = 0.0;
  208. m_textBox->Width = 1;
  209. m_textBox->Height = 1;
  210. m_textBox->TextChanged += ref new TextChangedEventHandler(this, &KeyBoardWinRT::OnTextChanged);
  211. #if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
  212. // Need to use InputScopeNameValue::Search to prevent auto-capitalize
  213. m_textBox->InputScope = ref new InputScope();
  214. auto n = m_textBox->InputScope->Names;
  215. n->Append(ref new InputScopeName(InputScopeNameValue::Search));
  216. #endif
  217. panel->Children->Append(m_textBox);
  218. }
  219. m_textBox->SelectionLength = 0;
  220. m_textBox->SelectionStart = 32768;
  221. m_textBox->Focus(FocusState::Programmatic);
  222. }));
  223. }
  224. }
  225. void KeyBoardWinRT::HideKeyboard(Platform::String^ text)
  226. {
  227. auto panel = cocos2d::GLViewImpl::sharedOpenGLView()->getPanel();
  228. auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
  229. if (dispatcher && panel)
  230. {
  231. // run on main UI thread
  232. dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, text, panel]()
  233. {
  234. if (m_textBox != nullptr)
  235. {
  236. unsigned int index;
  237. if (panel->Children->IndexOf(m_textBox, &index))
  238. {
  239. panel->Children->RemoveAt(index);
  240. }
  241. }
  242. m_textBox = nullptr;
  243. }));
  244. }
  245. }
  246. void KeyBoardWinRT::OnWinRTKeyboardEvent(WinRTKeyboardEventType type, KeyEventArgs^ args)
  247. {
  248. bool pressed = (type == WinRTKeyboardEventType::KeyPressed);
  249. // Is key repeats
  250. bool repeat = pressed && args->KeyStatus.WasKeyDown;
  251. int key = static_cast<int>(args->VirtualKey);
  252. auto it = g_keyCodeMap.find(key);
  253. if (it != g_keyCodeMap.end())
  254. {
  255. EventKeyboard::KeyCode keyCode = it->second;
  256. EventKeyboard event(keyCode, pressed);
  257. if (!repeat)
  258. {
  259. auto dispatcher = Director::getInstance()->getEventDispatcher();
  260. dispatcher->dispatchEvent(&event);
  261. if (keyCode == EventKeyboard::KeyCode::KEY_ENTER)
  262. {
  263. IMEDispatcher::sharedDispatcher()->dispatchInsertText("\n", 1);
  264. }
  265. }
  266. if (pressed && !event.isStopped())
  267. {
  268. switch (keyCode)
  269. {
  270. case EventKeyboard::KeyCode::KEY_BACKSPACE:
  271. IMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
  272. break;
  273. case EventKeyboard::KeyCode::KEY_HOME:
  274. case EventKeyboard::KeyCode::KEY_KP_HOME:
  275. case EventKeyboard::KeyCode::KEY_DELETE:
  276. case EventKeyboard::KeyCode::KEY_KP_DELETE:
  277. case EventKeyboard::KeyCode::KEY_END:
  278. case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
  279. case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
  280. case EventKeyboard::KeyCode::KEY_ESCAPE:
  281. IMEDispatcher::sharedDispatcher()->dispatchControlKey(keyCode);
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. }
  288. else
  289. {
  290. log("GLViewImpl::OnWinRTKeyboardEvent Virtual Key Code %d not supported", key);
  291. }
  292. }
  293. void KeyBoardWinRT::OnTextChanged(Platform::Object^ sender, TextChangedEventArgs^ args)
  294. {
  295. auto text = m_textBox->Text;
  296. if (text)
  297. {
  298. std::shared_ptr<cocos2d::InputEvent> e(new cocos2d::KeyboardEvent(Cocos2dKeyEvent::Text, text));
  299. cocos2d::GLViewImpl::sharedOpenGLView()->QueueEvent(e);
  300. m_textBox->Text = L"";
  301. }
  302. }
  303. NS_CC_END