File: KeyboardLinux.cpp

package info (click to toggle)
between 6%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 3,532 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 99; perl: 67
file content (180 lines) | stat: -rw-r--r-- 3,366 bytes parent folder | download | duplicates (19)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Modification History
 *
 * 2000-December-7		Jason Rohrer
 * Created.
 *
 * 2000-December-8		Jason Rohrer
 * Changed so that key state functions take a string instead of
 * an integer vkey code.
 *
 * 2001-May-2   Jason Rohrer
 * Changed to use more standard SDL include location.
 *
 * 2006-June-26   Jason Rohrer
 * Added function to get events that are waiting in the queue.
 */
 
#include "minorGems/ui/Keyboard.h"

#include <SDL/SDL.h>

#include <string.h>

/**
 * Note:  Linux implementation:
 * Requires that a ScreenGraphics be constructed before accessing the keyboard.
 */

// prototypes:

/**
 * Maps an ascii string description of a key, such as "a", to an SDL keycode.
 *
 * @param inKeyDescription an ascii description of a key.
 *
 * @return the SDL keycode.
 */
int getKeyCode( const char *inKeyDescription );



/**
 * Maps a keycode to an ascii character.
 *
 * @param inSDLKeycode the keycode.
 *
 * @return the ascii character, or -1 if the keycode is not mappable to ascii.
 */
int getKeyASCII( int inSDLKeycode );




#define M_KEY 	SDLK_m
#define N_KEY	SDLK_n

#define S_KEY	SDLK_s

#define Q_KEY	SDLK_q

#define L_KEY	SDLK_l

#define R_KEY	SDLK_r

#define T_KEY	SDLK_t



//char Keyboard::getKeyDown( int vKeyCode ) {
char Keyboard::getKeyDown( const char *inKeyDescription ) {
	SDL_PumpEvents();
	Uint8 *keys;
	keys = SDL_GetKeyState( NULL );
	return keys[ getKeyCode( inKeyDescription ) ] == SDL_PRESSED;
	}



//char Keyboard::getKeyUp( int vKeyCode ) {
char Keyboard::getKeyUp( const char *inKeyDescription ) {
	SDL_PumpEvents();
	Uint8 *keys;
	keys = SDL_GetKeyState( NULL );
	return keys[ getKeyCode( inKeyDescription ) ] == SDL_RELEASED;
	}



int Keyboard::getKeyPressedEvent() {

    SDL_Event event;

    if( SDL_PollEvent( &event ) ) {
        switch( event.type ) {
            case SDL_KEYDOWN:
                return getKeyASCII( event.key.keysym.sym );
                break;
            }
        }
    else {
        return -1;
        }
    }



int getKeyCode( const char *inKeyDescription ) {
	
	// note that strcmp functions return 0 if strings match
	
	if( !strcasecmp( inKeyDescription, "m" ) ) {
		return SDLK_m;
		}
	else if( !strcasecmp( inKeyDescription, "n" ) ) {
		return SDLK_n;
		}
	else if( !strcasecmp( inKeyDescription, "s" ) ) {
		return SDLK_s;
		}	
	else if( !strcasecmp( inKeyDescription, "q" ) ) {
		return SDLK_q;
		}	
	else if( !strcasecmp( inKeyDescription, "l" ) ) {
		return SDLK_l;
		}
	else if( !strcasecmp( inKeyDescription, "r" ) ) {
		return SDLK_r;
		}
	else if( !strcasecmp( inKeyDescription, "t" ) ) {
		return SDLK_t;
		}
		 
	}



int getKeyASCII( int inSDLKeycode ) {
    switch( inSDLKeycode ) {
        case SDLK_m:
            return 'm';
            break;
        case SDLK_n:
            return 'n';
            break;
        case SDLK_s:
            return 's';
            break;
        case SDLK_q:
            return 'a';
            break;
        case SDLK_l:
            return 'l';
            break;
        case SDLK_r:
            return 'r';
            break;
        case SDLK_t:
            return 't';
            break;
        default:
            return -1;
            break;
        }
    }


/*	
#define M_KEY 	SDLK_m
#define N_KEY	SDLK_n

#define S_KEY	SDLK_s

#define Q_KEY	SDLK_q

#define L_KEY	SDLK_l

#define R_KEY	SDLK_r

#define T_KEY	SDLK_t
*/