1 /*
2 * Modification History
3 *
4 * 2000-December-7 Jason Rohrer
5 * Created.
6 *
7 * 2000-December-8 Jason Rohrer
8 * Changed so that key state functions take a string instead of
9 * an integer vkey code.
10 *
11 * 2001-May-2 Jason Rohrer
12 * Changed to use more standard SDL include location.
13 *
14 * 2006-June-26 Jason Rohrer
15 * Added function to get events that are waiting in the queue.
16 */
17
18 #include "minorGems/ui/Keyboard.h"
19
20 #include <SDL/SDL.h>
21
22 #include <string.h>
23
24 /**
25 * Note: Linux implementation:
26 * Requires that a ScreenGraphics be constructed before accessing the keyboard.
27 */
28
29 // prototypes:
30
31 /**
32 * Maps an ascii string description of a key, such as "a", to an SDL keycode.
33 *
34 * @param inKeyDescription an ascii description of a key.
35 *
36 * @return the SDL keycode.
37 */
38 int getKeyCode( const char *inKeyDescription );
39
40
41
42 /**
43 * Maps a keycode to an ascii character.
44 *
45 * @param inSDLKeycode the keycode.
46 *
47 * @return the ascii character, or -1 if the keycode is not mappable to ascii.
48 */
49 int getKeyASCII( int inSDLKeycode );
50
51
52
53
54 #define M_KEY SDLK_m
55 #define N_KEY SDLK_n
56
57 #define S_KEY SDLK_s
58
59 #define Q_KEY SDLK_q
60
61 #define L_KEY SDLK_l
62
63 #define R_KEY SDLK_r
64
65 #define T_KEY SDLK_t
66
67
68
69 //char Keyboard::getKeyDown( int vKeyCode ) {
getKeyDown(const char * inKeyDescription)70 char Keyboard::getKeyDown( const char *inKeyDescription ) {
71 SDL_PumpEvents();
72 Uint8 *keys;
73 keys = SDL_GetKeyState( NULL );
74 return keys[ getKeyCode( inKeyDescription ) ] == SDL_PRESSED;
75 }
76
77
78
79 //char Keyboard::getKeyUp( int vKeyCode ) {
getKeyUp(const char * inKeyDescription)80 char Keyboard::getKeyUp( const char *inKeyDescription ) {
81 SDL_PumpEvents();
82 Uint8 *keys;
83 keys = SDL_GetKeyState( NULL );
84 return keys[ getKeyCode( inKeyDescription ) ] == SDL_RELEASED;
85 }
86
87
88
getKeyPressedEvent()89 int Keyboard::getKeyPressedEvent() {
90
91 SDL_Event event;
92
93 if( SDL_PollEvent( &event ) ) {
94 switch( event.type ) {
95 case SDL_KEYDOWN:
96 return getKeyASCII( event.key.keysym.sym );
97 break;
98 }
99 }
100 else {
101 return -1;
102 }
103 }
104
105
106
getKeyCode(const char * inKeyDescription)107 int getKeyCode( const char *inKeyDescription ) {
108
109 // note that strcmp functions return 0 if strings match
110
111 if( !strcasecmp( inKeyDescription, "m" ) ) {
112 return SDLK_m;
113 }
114 else if( !strcasecmp( inKeyDescription, "n" ) ) {
115 return SDLK_n;
116 }
117 else if( !strcasecmp( inKeyDescription, "s" ) ) {
118 return SDLK_s;
119 }
120 else if( !strcasecmp( inKeyDescription, "q" ) ) {
121 return SDLK_q;
122 }
123 else if( !strcasecmp( inKeyDescription, "l" ) ) {
124 return SDLK_l;
125 }
126 else if( !strcasecmp( inKeyDescription, "r" ) ) {
127 return SDLK_r;
128 }
129 else if( !strcasecmp( inKeyDescription, "t" ) ) {
130 return SDLK_t;
131 }
132
133 }
134
135
136
getKeyASCII(int inSDLKeycode)137 int getKeyASCII( int inSDLKeycode ) {
138 switch( inSDLKeycode ) {
139 case SDLK_m:
140 return 'm';
141 break;
142 case SDLK_n:
143 return 'n';
144 break;
145 case SDLK_s:
146 return 's';
147 break;
148 case SDLK_q:
149 return 'a';
150 break;
151 case SDLK_l:
152 return 'l';
153 break;
154 case SDLK_r:
155 return 'r';
156 break;
157 case SDLK_t:
158 return 't';
159 break;
160 default:
161 return -1;
162 break;
163 }
164 }
165
166
167 /*
168 #define M_KEY SDLK_m
169 #define N_KEY SDLK_n
170
171 #define S_KEY SDLK_s
172
173 #define Q_KEY SDLK_q
174
175 #define L_KEY SDLK_l
176
177 #define R_KEY SDLK_r
178
179 #define T_KEY SDLK_t
180 */
181