diff options
author | Andrei Karas <akaras@inbox.ru> | 2012-03-09 17:13:34 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-03-09 17:13:34 +0300 |
commit | e6bb33fad96878b66ecb32c104d06cd6e5ea9e77 (patch) | |
tree | 9294304f6480140a1cb0383df395893ae7009db7 | |
parent | 443813146a327504a07a507f43117d92296ca8a7 (diff) | |
download | manaverse-e6bb33fad96878b66ecb32c104d06cd6e5ea9e77.tar.gz manaverse-e6bb33fad96878b66ecb32c104d06cd6e5ea9e77.tar.bz2 manaverse-e6bb33fad96878b66ecb32c104d06cd6e5ea9e77.tar.xz manaverse-e6bb33fad96878b66ecb32c104d06cd6e5ea9e77.zip |
Add limited support for keys unknown for SDL.
This keys can be assigned to simple actions and cant be modifier keys.
-rw-r--r-- | src/gui/setup_keyboard.cpp | 8 | ||||
-rw-r--r-- | src/keyboardconfig.cpp | 58 | ||||
-rw-r--r-- | src/keyboardconfig.h | 5 |
3 files changed, 56 insertions, 15 deletions
diff --git a/src/gui/setup_keyboard.cpp b/src/gui/setup_keyboard.cpp index aae1cd183..e9ce1b704 100644 --- a/src/gui/setup_keyboard.cpp +++ b/src/gui/setup_keyboard.cpp @@ -201,11 +201,9 @@ void Setup_Keyboard::refreshAssignedKey(int index) } else { - std::string caption; - char *temp = SDL_GetKeyName( - static_cast<SDLKey>(keyboard.getKeyValue(index))); - caption = keyboard.getKeyCaption(index) + ": " + toString(temp); - mKeyListModel->setElementAt(index, caption); + mKeyListModel->setElementAt(index, strprintf("%s: %s", + keyboard.getKeyCaption(index).c_str(), + keyboard.getKeyStringLong(index).c_str())); } } diff --git a/src/keyboardconfig.cpp b/src/keyboardconfig.cpp index eac641b5a..cf620148e 100644 --- a/src/keyboardconfig.cpp +++ b/src/keyboardconfig.cpp @@ -456,9 +456,18 @@ void KeyboardConfig::callbackNewKey() mSetupKey->newKeyCallback(mNewKeyIndex); } +int KeyboardConfig::getKeyValueFromEvent(const SDL_Event &event) const +{ + if (event.key.keysym.sym) + return event.key.keysym.sym; + else if (event.key.keysym.scancode > 1) + return -event.key.keysym.scancode; + return 0; +} + int KeyboardConfig::getKeyIndex(const SDL_Event &event, int grp) const { - const int keyValue = event.key.keysym.sym; + const int keyValue = getKeyValueFromEvent(event); for (int i = 0; i < KEY_TOTAL; i++) { if (keyValue == mKey[i].value && @@ -473,7 +482,7 @@ int KeyboardConfig::getKeyIndex(const SDL_Event &event, int grp) const int KeyboardConfig::getKeyEmoteOffset(const SDL_Event &event) const { - const int keyValue = event.key.keysym.sym; + const int keyValue = getKeyValueFromEvent(event); for (int i = KEY_EMOTE_1; i <= KEY_EMOTE_48; i++) { if (keyValue == mKey[i].value) @@ -486,7 +495,11 @@ bool KeyboardConfig::isActionActive(int index) const { if (!mActiveKeys) return false; - return mActiveKeys[mKey[index].value]; + const int value = mKey[index].value; + if (value >= 0) + return mActiveKeys[value]; + else + return false; // scan codes active state now not implimented } void KeyboardConfig::refreshActiveKeys() @@ -494,20 +507,47 @@ void KeyboardConfig::refreshActiveKeys() mActiveKeys = SDL_GetKeyState(nullptr); } -std::string KeyboardConfig::getKeyValueString(int index) const +std::string KeyboardConfig::getKeyStringLong(int index) const { - std::string key = SDL_GetKeyName( - static_cast<SDLKey>(getKeyValue(index))); + const int keyValue = getKeyValue(index); + if (keyValue >= 0) + return SDL_GetKeyName(static_cast<SDLKey>(keyValue)); + else if (keyValue < -1) + return strprintf(_("key_%d"), -keyValue); + else + return _("unknown key"); +} - return getKeyShortString(key); +std::string KeyboardConfig::getKeyValueString(int index) const +{ + const int keyValue = getKeyValue(index); + if (keyValue >= 0) + { + std::string key = SDL_GetKeyName(static_cast<SDLKey>(keyValue)); + return getKeyShortString(key); + } + else if (keyValue < -1) + { + return strprintf("#%d", -keyValue); + } + else + { + // TRANSLATORS: Unknown key short string. This string must be maximum 5 chars + return _("u key"); + } } std::string KeyboardConfig::getKeyShortString(const std::string &key) const { if (key == "backspace") + { return "bksp"; + } else if (key == "unknown key") - return "u key"; + { + // TRANSLATORS: Unknown key short string. This string must be maximum 5 chars + return _("u key"); + } return key; } @@ -518,7 +558,7 @@ SDLKey KeyboardConfig::getKeyFromEvent(const SDL_Event &event) const void KeyboardConfig::setNewKey(const SDL_Event &event) { - mKey[mNewKeyIndex].value = event.key.keysym.sym; + mKey[mNewKeyIndex].value = getKeyValueFromEvent(event); } void KeyboardConfig::unassignKey() diff --git a/src/keyboardconfig.h b/src/keyboardconfig.h index 55e795e61..009ede631 100644 --- a/src/keyboardconfig.h +++ b/src/keyboardconfig.h @@ -28,7 +28,6 @@ #include <string> -//enum SDLKey; union SDL_Event; /** @@ -152,11 +151,15 @@ class KeyboardConfig std::string getKeyShortString(const std::string &key) const; + std::string getKeyStringLong(int index) const; + const std::string &getBindError() const { return mBindError; } SDLKey getKeyFromEvent(const SDL_Event &event) const; + int getKeyValueFromEvent(const SDL_Event &event) const; + void unassignKey(); /** |