1 /***************************************************************************
2 * Copyright (C) 2005 by Dimitris Saougos & Filippos Papadopoulos *
3 * <psybases@gmail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
26 const int LEFT_BUTTON = SDL_BUTTON_LEFT;
27 const int MIDDLE_BUTTON = SDL_BUTTON_MIDDLE;
28 const int RIGHT_BUTTON = SDL_BUTTON_RIGHT;
30 const int KEY_ESC = SDLK_ESCAPE;
31 const int KEY_ESCAPE = SDLK_ESCAPE;
33 const int KEY_MINUS = SDLK_MINUS;
34 const int KEY_EQUALS = SDLK_EQUALS;
36 const int KEY_BACKSPACE = SDLK_BACKSPACE;
38 const int KEY_TAB = SDLK_TAB;
40 const int KEY_LBRACKET = SDLK_LEFTBRACKET;
41 const int KEY_RBRACKET = SDLK_RIGHTBRACKET;
43 const int KEY_ENTER = SDLK_RETURN;
44 const int KEY_RETURN = SDLK_RETURN;
46 const int KEY_CTRL = SDLK_LCTRL;
47 const int KEY_LCTRL = SDLK_LCTRL;
48 const int KEY_RCTRL = SDLK_RCTRL;
50 const int KEY_SHIFT = SDLK_LSHIFT;
51 const int KEY_LSHIFT = SDLK_LSHIFT;
52 const int KEY_RSHIFT = SDLK_RSHIFT;
54 const int KEY_ALT = SDLK_LSUPER;
55 const int KEY_LALT = SDLK_LSUPER;
56 const int KEY_RALT = SDLK_RSUPER;
58 const int KEY_0 = SDLK_0;
59 const int KEY_1 = SDLK_1;
60 const int KEY_2 = SDLK_2;
61 const int KEY_3 = SDLK_3;
62 const int KEY_4 = SDLK_4;
63 const int KEY_5 = SDLK_5;
64 const int KEY_6 = SDLK_6;
65 const int KEY_7 = SDLK_7;
66 const int KEY_8 = SDLK_8;
67 const int KEY_9 = SDLK_9;
69 const int KEY_A = SDLK_a;
70 const int KEY_B = SDLK_b;
71 const int KEY_C = SDLK_c;
72 const int KEY_D = SDLK_d;
73 const int KEY_E = SDLK_e;
74 const int KEY_F = SDLK_f;
75 const int KEY_G = SDLK_g;
76 const int KEY_H = SDLK_h;
77 const int KEY_I = SDLK_i;
78 const int KEY_J = SDLK_j;
79 const int KEY_K = SDLK_k;
80 const int KEY_L = SDLK_l;
81 const int KEY_M = SDLK_m;
82 const int KEY_N = SDLK_n;
83 const int KEY_O = SDLK_o;
84 const int KEY_P = SDLK_p;
85 const int KEY_Q = SDLK_q;
86 const int KEY_R = SDLK_r;
87 const int KEY_S = SDLK_s;
88 const int KEY_T = SDLK_t;
89 const int KEY_U = SDLK_u;
90 const int KEY_V = SDLK_v;
91 const int KEY_W = SDLK_w;
92 const int KEY_X = SDLK_x;
93 const int KEY_Y = SDLK_y;
94 const int KEY_Z = SDLK_z;
96 const int KEY_SEMICOLON = SDLK_SEMICOLON;
97 const int KEY_APOSTROPHE = SDLK_QUOTE;
99 const int KEY_TILDE = SDLK_UNKNOWN;
100 const int KEY_GRAVE = SDLK_BACKQUOTE;
102 const int KEY_BACKSLASH = SDLK_BACKSLASH;
104 const int KEY_COMMA = SDLK_COMMA;
105 const int KEY_PERIOD = SDLK_PERIOD;
106 const int KEY_FORWARDSLASH = SDLK_SLASH;
107 const int KEY_SLASH = SDLK_SLASH;
109 const int KEY_SPACE = SDLK_SPACE;
111 const int KEY_CAPSLOCK = SDLK_CAPSLOCK;
112 const int KEY_CAPITAL = SDLK_CAPSLOCK;
114 const int KEY_F1 = SDLK_F1;
115 const int KEY_F2 = SDLK_F2;
116 const int KEY_F3 = SDLK_F3;
117 const int KEY_F4 = SDLK_F4;
118 const int KEY_F5 = SDLK_F5;
119 const int KEY_F6 = SDLK_F6;
120 const int KEY_F7 = SDLK_F7;
121 const int KEY_F8 = SDLK_F8;
122 const int KEY_F9 = SDLK_F9;
123 const int KEY_F10 = SDLK_F10;
124 const int KEY_F11 = SDLK_F11;
125 const int KEY_F12 = SDLK_F12;
127 const int KEY_SYSRQ = SDLK_SYSREQ;
128 const int KEY_SCROLLLOCK = SDLK_SCROLLOCK;
129 const int KEY_PAUSE = SDLK_PAUSE;
131 const int KEY_NUMLOCK = SDLK_NUMLOCK;
132 const int KEY_NUMPAD0 = SDLK_KP0;
133 const int KEY_NUMPAD1 = SDLK_KP1;
134 const int KEY_NUMPAD2 = SDLK_KP2;
135 const int KEY_NUMPAD3 = SDLK_KP3;
136 const int KEY_NUMPAD4 = SDLK_KP4;
137 const int KEY_NUMPAD5 = SDLK_KP5;
138 const int KEY_NUMPAD6 = SDLK_KP6;
139 const int KEY_NUMPAD7 = SDLK_KP7;
140 const int KEY_NUMPAD8 = SDLK_KP8;
141 const int KEY_NUMPAD9 = SDLK_KP9;
142 const int KEY_ADD = SDLK_PLUS;
143 const int KEY_SUBTRACT = SDLK_MINUS;
144 const int KEY_DIVIDE = SDLK_SLASH;
145 const int KEY_MULTIPLY = SDLK_ASTERISK;
146 const int KEY_DECIMAL = SDLK_PERIOD;
147 const int KEY_NUMPADENTER = SDLK_UNKNOWN;
149 const int KEY_INSERT = SDLK_INSERT;
150 const int KEY_DELETE = SDLK_DELETE;
151 const int KEY_HOME = SDLK_HOME;
152 const int KEY_END = SDLK_END;
153 const int KEY_PAGEUP = SDLK_PAGEUP;
154 const int KEY_PAGEDOWN = SDLK_PAGEDOWN;
156 const int KEY_UP = SDLK_UP;
157 const int KEY_DOWN = SDLK_DOWN;
158 const int KEY_LEFT = SDLK_LEFT;
159 const int KEY_RIGHT = SDLK_RIGHT;
161 const int KEY_LWIN = SDLK_LSUPER;
162 const int KEY_RWIN = SDLK_RSUPER;
163 const int KEY_APPS = SDLK_MENU;
169 Input::Input(bool keyb,bool mous)
175 mouseLockBuffer[i]=-1;
199 bool Input::keyboardGetKeyState(int key)
202 static long tm = getTime();
203 bool retValue = false;
207 SDLMod mod = SDL_GetModState();
209 if(key == KEY_CTRL && (mod & KMOD_CTRL))
212 if(key == KEY_LCTRL && (mod & KMOD_LCTRL))
215 if(key == KEY_RCTRL && (mod & KMOD_RCTRL))
218 if(key == KEY_SHIFT && (mod & KMOD_SHIFT))
221 if(key == KEY_LSHIFT && (mod & KMOD_LSHIFT))
224 if(key == KEY_RSHIFT && (mod & KMOD_RSHIFT))
227 if(key == KEY_ALT && (mod & KMOD_ALT))
230 if(key == KEY_LALT && (mod & KMOD_LALT))
233 if(key == KEY_RALT && (mod & KMOD_RALT))
236 if((key == KEY_CAPSLOCK || key == KEY_CAPITAL ) && (mod & KMOD_CAPS))
239 if(key == KEY_NUMLOCK && (mod & KMOD_NUM))
243 Uint8 *keystate = SDL_GetKeyState(NULL);
257 bool Input::keyboardGetKeyState(int key)
259 bool retValue = false;
261 if (getTime() - lockBuffer[key] > keyLockDuration)
264 // printf("key = %d\n", key);
265 if(lockBuffer[key]==-1)
267 if(key_press && key_val == key)
276 void Input::mousePrepare()
279 SDL_GetRelativeMouseState(&mx, &my);
283 long Input::mouseGetYDelta(void)
289 long Input::mouseGetXDelta(void)
295 bool Input::mouseGetButtonState(int button)
297 bool retValue = false;
299 if (getTime()-mouseLockBuffer[button]>buttonLockDuration)
300 mouseLockBuffer[button]=-1;
303 while(SDL_PollEvent(&event))
305 if(event.type == SDL_MOUSEBUTTONDOWN)
307 if((&event.button)->button == button)
311 if(SDL_PushEvent(&event) == -1)
312 std::cerr << "push event failed\n";
318 if(event.type == SDL_MOUSEBUTTONUP)
321 if(event.type == SDL_KEYDOWN || event.type == SDL_MOUSEMOTION)
323 if(SDL_PushEvent(&event) == -1)
324 std::cerr << "push event failed\n";
332 if (mouseLockBuffer[button]==-1)
335 if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(button))
343 void Input::lockKey(int key, int duration)
345 if (key<0 || key>255)
347 keyLockDuration=duration;
351 void Input::lockButton(int button, int duration)
353 if (button<0 || button>3)
356 //if (mouseLockBuffer[button]!=-1) return ;
358 mouseLockBuffer[button]=(int)(getTime());
360 buttonLockDuration=duration;
366 char Input::getChar(void)
369 static int oldKey = 0;
370 static long delay = getTime();
371 static long repeat = getTime();
374 if(key_val == oldKey)
377 if(getTime() - delay < D)
385 if(getTime() - repeat < R)
400 SDLMod mod = SDL_GetModState();
402 if((mod & KMOD_CAPS) && !(mod & KMOD_SHIFT))
403 key_val = toupper(key_val);
405 if((mod & KMOD_SHIFT))
408 return symbol[key_val - 48];
430 if(!(mod & KMOD_CAPS))
431 return toupper(key_val);
440 if(keyboardGetKeyState(i))