2
* Custom keyboard shortcuts configuration
3
* Copyright (C) 2007 Joshua Langley <joshlangley@optusnet.com.au>
4
* Copyright (C) 2009-2010 The Mana Developers
5
* Copyright (C) 2011-2013 The ManaPlus Developers
7
* This file is part of The ManaPlus Client.
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include "keyboardconfig.h"
25
#include "configuration.h"
26
#include "inputmanager.h"
29
#include "utils/gettext.h"
31
#include <SDL_events.h>
35
extern volatile int tick_time;
37
KeyboardConfig::KeyboardConfig() :
40
mActiveKeys2(nullptr),
48
void KeyboardConfig::init()
52
mActiveKeys2 = new uint8_t[500];
53
mRepeatTime = config.getIntValue("repeateInterval2") / 10;
56
void KeyboardConfig::deinit()
58
delete [] mActiveKeys2;
59
mActiveKeys2 = nullptr;
62
int KeyboardConfig::getKeyValueFromEvent(const SDL_Event &event) const
65
return event.key.keysym.scancode;
67
if (event.key.keysym.sym)
68
return event.key.keysym.sym;
69
else if (event.key.keysym.scancode > 1)
70
return -event.key.keysym.scancode;
75
int KeyboardConfig::getKeyIndex(const SDL_Event &event, const int grp) const
77
const int keyValue = getKeyValueFromEvent(event);
78
return inputManager.getKeyIndex(keyValue, grp, INPUT_KEYBOARD);
81
void KeyboardConfig::refreshActiveKeys()
83
mActiveKeys = SDL_GetKeyState(nullptr);
86
std::string KeyboardConfig::getKeyName(const int key)
88
if (key == Input::KEY_NO_VALUE)
93
return SDL_GetKeyName(SDL_GetKeyFromScancode(
94
static_cast<SDL_Scancode>(key)));
96
return SDL_GetKeyName(static_cast<SDLKey>(key));
100
// TRANSLATORS: long key name, should be short
101
return strprintf(_("key_%d"), key);
104
std::string KeyboardConfig::getKeyShortString(const std::string &key)
106
if (key == "backspace")
108
else if (key == "numlock")
110
else if (key == "caps lock")
112
else if (key == "scroll lock")
114
else if (key == "right shift")
116
else if (key == "left shift")
118
else if (key == "right ctrl")
120
else if (key == "left ctrl")
122
else if (key == "right alt")
124
else if (key == "left alt")
126
else if (key == "right meta")
128
else if (key == "left meta")
130
else if (key == "right super")
132
else if (key == "left super")
134
else if (key == "print screen")
135
return "print screen";
136
else if (key == "page up")
138
else if (key == "page down")
141
if (key == "unknown key")
143
// TRANSLATORS: Unknown key short string.
144
// TRANSLATORS: This string must be maximum 5 chars
150
SDLKey KeyboardConfig::getKeyFromEvent(const SDL_Event &event)
153
return event.key.keysym.scancode;
155
return event.key.keysym.sym;
159
KeysVector *KeyboardConfig::getActionVector(const SDL_Event &event)
161
const int i = getKeyValueFromEvent(event);
162
// logger->log("key triggerAction: %d", i);
163
if (i != 0 && i < SDLK_LAST && mKeyToAction.find(i) != mKeyToAction.end())
164
return &mKeyToAction[i];
168
KeysVector *KeyboardConfig::getActionVectorByKey(const int i)
170
// logger->log("key triggerAction: %d", i);
171
if (i != 0 && i < SDLK_LAST && mKeyToAction.find(i) != mKeyToAction.end())
172
return &mKeyToAction[i];
176
int KeyboardConfig::getActionId(const SDL_Event &event)
178
const int i = getKeyValueFromEvent(event);
179
// logger->log("getActionId: %d", i);
180
if (i != 0 && i < SDLK_LAST && mKeyToId.find(i) != mKeyToId.end())
185
bool KeyboardConfig::isActionActive(const int index) const
190
const KeyFunction &key = inputManager.getKey(index);
191
for (size_t i = 0; i < KeyFunctionSize; i ++)
193
const KeyItem &val = key.values[i];
194
if (val.type != INPUT_KEYBOARD)
197
const int value = val.value;
200
if (mActiveKeys[value])
203
else if (value < -1 && value > -500)
205
if (mActiveKeys2[-value])
212
void KeyboardConfig::update()
214
inputManager.updateKeyActionMap(mKeyToAction, mKeyToId,
215
mKeyTimeMap, INPUT_KEYBOARD);
218
void KeyboardConfig::handleActicateKey(const SDL_Event &event)
220
const int key = getKeyValueFromEvent(event);
221
if (key < -1 && key > -500)
222
mActiveKeys2[-key] = 1;
226
void KeyboardConfig::handleActicateKey(const int key)
228
if (key < -1 && key > -500)
229
mActiveKeys2[-key] = 1;
233
void KeyboardConfig::handleDeActicateKey(const SDL_Event &event)
235
const int key = getKeyValueFromEvent(event);
236
if (key < -1 && key > -500)
237
mActiveKeys2[-key] = 0;
241
void KeyboardConfig::handleDeActicateKey(const int key)
243
if (key < -1 && key > -500)
244
mActiveKeys2[-key] = 0;
248
void KeyboardConfig::handleRepeat(const int time)
250
FOR_EACH (KeyTimeMapIter, it, mKeyTimeMap)
253
const int key = (*it).first;
254
int &keyTime = (*it).second;
257
if (mActiveKeys && mActiveKeys[key])
260
else if (key < -1 && key > -500)
262
if (mActiveKeys2 && mActiveKeys2[-key])
267
if (time > keyTime && abs(time - keyTime)
268
> static_cast<signed>(mRepeatTime))
271
inputManager.triggerAction(getActionVectorByKey(key));
277
void KeyboardConfig::resetRepeat(const int key)
279
const KeyTimeMapIter it = mKeyTimeMap.find(key);
280
if (it != mKeyTimeMap.end())
281
(*it).second = tick_time;