File: KeyInput.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (71 lines) | stat: -rwxr-xr-x 1,690 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <cstring>
#include <SDL_keyboard.h>
#include <SDL_keysym.h>
#include <assert.h>

#include "KeyInput.h"

KeyInput* keyInput = NULL;

KeyInput* KeyInput::GetInstance() {
	if (keyInput == NULL) {
		keyInput = new KeyInput();
	}

	return keyInput;
}

void KeyInput::FreeInstance(KeyInput* keyInp) {
	delete keyInp; keyInput = NULL;
}



KeyInput::KeyInput() {
	keys.resize(SDLK_LAST, 0);

	// Initialize keyboard
	SDL_EnableUNICODE(1);
	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
	SDL_SetModState(KMOD_NONE);
}

/**
 * Tests SDL keystates and sets values in key array
 */
void KeyInput::Update(boost::uint16_t currKeyUnicodeChar, boost::int8_t fakeMetaKey)
{
	int numKeys = 0;

	const SDLMod keyMods = SDL_GetModState();
	const boost::uint8_t* keyStates = SDL_GetKeyState(&numKeys);

	assert(numKeys <= SDLK_LAST);
	memcpy(&keys[0], keyStates, sizeof(boost::uint8_t) * numKeys);

	keys[SDLK_LALT]   = (keyMods & KMOD_ALT)   ? 1 : 0;
	keys[SDLK_LCTRL]  = (keyMods & KMOD_CTRL)  ? 1 : 0;
	keys[SDLK_LMETA]  = (keyMods & KMOD_META)  ? 1 : 0;
	keys[SDLK_LSHIFT] = (keyMods & KMOD_SHIFT) ? 1 : 0;

	if (fakeMetaKey >= 0) {
		keys[SDLK_LMETA] |= keys[fakeMetaKey];
	}

	currentKeyUnicodeChar = currKeyUnicodeChar;
}

boost::uint16_t KeyInput::GetNormalizedKeySymbol(boost::uint16_t sym) const {

	if (sym <= SDLK_DELETE) {
		sym = tolower(sym);
	}
	else if (sym == SDLK_RSHIFT) { sym = SDLK_LSHIFT; }
	else if (sym == SDLK_RCTRL)  { sym = SDLK_LCTRL;  }
	else if (sym == SDLK_RMETA)  { sym = SDLK_LMETA;  }
	else if (sym == SDLK_RALT)   { sym = SDLK_LALT;   }

	return sym;
}