From 3a9e8850c0b47e4058cefb648b9b48d672461bd5 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 26 Oct 2012 14:09:09 +0200 Subject: [PATCH] Added Keyboard::GetKeyName (experimental) Former-commit-id: a937b98ceeed3cb7c64bf6c1f544bb7021ad01bd --- include/Nazara/Utility/Keyboard.hpp | 2 + src/Nazara/Utility/Keyboard.cpp | 5 ++ src/Nazara/Utility/Win32/InputImpl.cpp | 113 ++++++++++++++++++------- src/Nazara/Utility/Win32/InputImpl.hpp | 2 + 4 files changed, 91 insertions(+), 31 deletions(-) diff --git a/include/Nazara/Utility/Keyboard.hpp b/include/Nazara/Utility/Keyboard.hpp index a7bb39f3a..2d783f80f 100644 --- a/include/Nazara/Utility/Keyboard.hpp +++ b/include/Nazara/Utility/Keyboard.hpp @@ -10,6 +10,7 @@ #define NAZARA_KEYBOARD_HPP #include +#include class NAZARA_API NzKeyboard { @@ -159,6 +160,7 @@ class NAZARA_API NzKeyboard Count }; + static NzString GetKeyName(Key key); static bool IsKeyPressed(Key key); }; diff --git a/src/Nazara/Utility/Keyboard.cpp b/src/Nazara/Utility/Keyboard.cpp index d463c6cb2..86b810bb3 100644 --- a/src/Nazara/Utility/Keyboard.cpp +++ b/src/Nazara/Utility/Keyboard.cpp @@ -14,6 +14,11 @@ #include +NzString NzKeyboard::GetKeyName(Key key) +{ + return NzEventImpl::GetKeyName(key); +} + bool NzKeyboard::IsKeyPressed(Key key) { return NzEventImpl::IsKeyPressed(key); diff --git a/src/Nazara/Utility/Win32/InputImpl.cpp b/src/Nazara/Utility/Win32/InputImpl.cpp index 0bc96168d..1ffc06d99 100644 --- a/src/Nazara/Utility/Win32/InputImpl.cpp +++ b/src/Nazara/Utility/Win32/InputImpl.cpp @@ -8,37 +8,9 @@ #include #include -NzVector2i NzEventImpl::GetMousePosition() +namespace { - POINT pos; - GetCursorPos(&pos); - - return NzVector2i(pos.x, pos.y); -} - -NzVector2i NzEventImpl::GetMousePosition(const NzWindow& relativeTo) -{ - HWND handle = reinterpret_cast(relativeTo.GetHandle()); - if (handle) - { - POINT pos; - GetCursorPos(&pos); - ScreenToClient(handle, &pos); - - return NzVector2i(pos.x, pos.y); - } - else - { - NazaraError("Window's handle is invalid"); - - // Attention que (-1, -1) est une position tout à fait valide et ne doit pas être utilisée pour tester l'erreur - return NzVector2i(-1, -1); - } -} - -bool NzEventImpl::IsKeyPressed(NzKeyboard::Key key) -{ - static int vKeys[NzKeyboard::Count] = { + int vKeys[NzKeyboard::Count] = { // Lettres 0x41, // Key::A 0x42, // Key::B @@ -161,7 +133,7 @@ bool NzEventImpl::IsKeyPressed(NzKeyboard::Key key) VK_BROWSER_SEARCH, // Key::Browser_Search VK_BROWSER_STOP, // Key::Browser_Stop - // Touches de contr + // Touches de contrôle VK_MEDIA_NEXT_TRACK, // Key::Media_Next, VK_MEDIA_PLAY_PAUSE, // Key::Media_PlayPause, VK_MEDIA_PREV_TRACK, // Key::Media_Previous, @@ -177,7 +149,86 @@ bool NzEventImpl::IsKeyPressed(NzKeyboard::Key key) VK_NUMLOCK, // Key::NumLock VK_SCROLL // Key::ScrollLock }; +} +NzString NzEventImpl::GetKeyName(NzKeyboard::Key key) +{ + // http://www.ffuts.org/blog/mapvirtualkey-getkeynametext-and-a-story-of-how-to/ + int vk = vKeys[key]; + unsigned int code = MapVirtualKeyW(vk, 0) << 16; + + ///FIXME: Liste complète ? + switch (vk) + { + case VK_ATTN: + case VK_DOWN: + case VK_DELETE: + case VK_DIVIDE: + case VK_END: + case VK_HOME: + case VK_INSERT: + case VK_LEFT: + case VK_LWIN: + case VK_OEM_1: + case VK_OEM_2: + case VK_OEM_3: + case VK_OEM_4: + case VK_OEM_5: + case VK_OEM_6: + case VK_OEM_7: + case VK_OEM_CLEAR: + case VK_OEM_COMMA: + case VK_OEM_MINUS: + case VK_OEM_PERIOD: + case VK_OEM_PLUS: + case VK_PAUSE: + case VK_NEXT: + case VK_NUMLOCK: + case VK_PRIOR: + case VK_RIGHT: + case VK_RWIN: + case VK_UP: + code |= 0x1000000; // 24ème bit pour l'extension + break; + } + + wchar_t keyName[20]; // Je ne pense pas que ça dépassera 20 caractères + if (!GetKeyNameTextW(code, &keyName[0], 20)) + return "Unknown"; + + return NzString::Unicode(keyName); +} + +NzVector2i NzEventImpl::GetMousePosition() +{ + POINT pos; + GetCursorPos(&pos); + + return NzVector2i(pos.x, pos.y); +} + +NzVector2i NzEventImpl::GetMousePosition(const NzWindow& relativeTo) +{ + HWND handle = reinterpret_cast(relativeTo.GetHandle()); + if (handle) + { + POINT pos; + GetCursorPos(&pos); + ScreenToClient(handle, &pos); + + return NzVector2i(pos.x, pos.y); + } + else + { + NazaraError("Window's handle is invalid"); + + // Attention que (-1, -1) est une position tout à fait valide et ne doit pas être utilisée pour tester l'erreur + return NzVector2i(-1, -1); + } +} + +bool NzEventImpl::IsKeyPressed(NzKeyboard::Key key) +{ switch (key) { case NzKeyboard::CapsLock: diff --git a/src/Nazara/Utility/Win32/InputImpl.hpp b/src/Nazara/Utility/Win32/InputImpl.hpp index 41961312e..07bb61526 100644 --- a/src/Nazara/Utility/Win32/InputImpl.hpp +++ b/src/Nazara/Utility/Win32/InputImpl.hpp @@ -8,12 +8,14 @@ #define NAZARA_INPUTIMPL_HPP #include +#include #include #include class NzEventImpl { public: + static NzString GetKeyName(NzKeyboard::Key key); static NzVector2i GetMousePosition(); static NzVector2i GetMousePosition(const NzWindow& relativeTo); static bool IsKeyPressed(NzKeyboard::Key key);