PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
sdl/PlatformKeyboard.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/platforms/sdl/Keyboard.cpp
4// Purpose: Contains SDL-specific interface for the keyboard handler
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27#include "PlatformKeyboard.h"
28#include "../../devices/PKeyboard.h"
29#include "../../devices/DeviceState.h"
30
31
32#include "PlatformTimer.h"
33
34
35#include "SDL.h"
36#include <stdio.h>
37
38// cout removed - use cerr for debug output
39using std::cerr;
40using std::endl;
41using std::flush;
42using std::list;
43using std::ostream;
44
45
46
53
54
57
60 // mCDT(CDT_KEYBOARD)
61{
62
63 //EMSCRIPTEN uses sdl 2, which replaces getkeystat with getkeyboardstate
64#if defined PEBL_EMSCRIPTEN
65 mKeyboardState = SDL_GetKeyboardState(NULL);
66 SDL_PumpEvents();
67#else
68 mKeyboardState = SDL_GetKeyboardState(NULL);
69 SDL_PumpEvents();
70#endif
71}
72
73
79
80
81//We can distinguish between scancodes and keycodes!
82//scancode is an absolute keyboard position.
83
86{
87 if(key == PEBL_KEYCODE_ANYKEY)
88 {
89
90
91 return IsAnyKeyDown();
92 }
93 else
94 {
95
96
97 SDL_PumpEvents();
98 if( mKeyboardState[key] == 1)
99 return key;
100 else
102
103 }
104}
105
106
107
112
114{
115
116 //cout << "Testing for iskeyup: " << key << "|" << PEBL_KEYCODE_ANYKEY << endl;
117
118 //If the key is 'anykey', what you are really asking is if there
119 //are any keys that are not up. This is a semantic distortion, because
120 //logically, you should be asking whether ALL keys are pressed, which
121 //is pretty ridiculous.
122 //
123 if(key == PEBL_KEYCODE_ANYKEY)
124 {
125 PEBL_Keycode key = IsAnyKeyDown();
126
127 return key;
128 }
129 else
130 {
131 //SDL_PumpEvents();
132 //cout << "Getting scancode name from key: " << key << endl;
133 int code = SDL_GetScancodeFromKey(key);
134 //cout << code << "---------" << endl;
135 if(mKeyboardState[code]==1)
136 return key;
137 else
138 return PEBL_KEYCODE_NOTHING; //==0==false
139 }
140}
141
142
144PEBL_Keycode PlatformKeyboard::IsAnyKeyDown() const
145{
146
147
148 int code = 0;
149
150 //Update the keyboard state from SDL function.
151 //SDL_PumpEvents();
152
153
154 // int keys;
155 // const Uint8 *state = SDL_GetKeyboardState(&keys);
156 // //this justdisplays the completekeyboardstate.
157 // for(int i = 0;i<keys;i++)
158 // {
159 // cout <<"|"<< i<< ":" << state[i] << std::flush;
160 // }
161 // cout << endl;
162
163
164 //Skip uppercase letters: do standard letters
165 for(int i = 91; i<=127; i++)
166 {
167
168 if(mKeyboardState[i]) code=i;
169 }
170
171 //Go through first 64
172 for(int i = 0; i<=64; i++)
173 {
174
175 if(mKeyboardState[i]) code =i;
176 }
177
178
179 //Keypad, function keys, etc.
180 for(int i = 256; i<=296; i++)
181 {
182
183 if(mKeyboardState[i]) code = i;
184 }
185
186
187
188 //Modkeys/misc
189 for(int i = 300; i<=322; i++)
190 {
191
192 if(mKeyboardState[i]) code = i;
193 }
194
195
196 //International letters (out of order, but of lower priority)
197 for(int i = 160; i<=255; i++)
198 {
199
200 if(mKeyboardState[i]) code = i;
201 }
202
203
204 if(code==0)
206 else
207 return (PEBL_Keycode)SDL_GetKeyFromScancode((SDL_Scancode)code);
208
209}
210
211
212int PlatformKeyboard::GetState(int iface) const
213{
214
215 //This asks for the state of a particular key.
216
217
218 PEBL_Keycode key = (PEBL_Keycode)iface;
219 int ret = (int)IsKeyUp(key);
220
221 //cout << "keyboard Getstating: " << PEBL_KEYCODE_x <<":"<<iface <<"|" << ret << endl;
222 return ret;
223}
224
225
226
227
228
229
230// Inheritable function that is called by friend method << operator of PComplexData
231ostream & PlatformKeyboard::SendToStream(ostream& out) const
232{
233
234 out << "<SDL PlatformKeyboard>" << flush;
235 return out;
236}
237
238
#define NULL
Definition BinReloc.cpp:317
PEBL_Keycode
Definition PKeyboard.h:78
@ PEBL_KEYCODE_ANYKEY
Definition PKeyboard.h:329
@ PEBL_KEYCODE_NOTHING
Definition PKeyboard.h:331
virtual std::ostream & SendToStream(std::ostream &out) const
virtual ~PlatformKeyboard()
Standard Destructor.
virtual int GetState(int iface) const
virtual bool IsKeyUp(PEBL_Keycode key) const
PlatformKeyboard()
Standard Constructor.
virtual PEBL_Keycode IsKeyDown(PEBL_Keycode key) const
Primitive key event poller.