File: getSDLKey.cpp

package info (click to toggle)
fceux 2.3.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 8,904 kB
  • sloc: cpp: 108,328; ansic: 10,341; sh: 3,386; python: 424; pascal: 391; perl: 82; makefile: 70; lisp: 60
file content (90 lines) | stat: -rw-r--r-- 2,437 bytes parent folder | download | duplicates (3)
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
/////////////////////////////////////////////////////////////////
// getSDLKey.cpp
// Lukas Sabota - October, 2011
//
// Prints the corresponding SDL keysym for a key pressed
//  into a SDL window.  Useful for remapping hotkeys in they
//  ~/.fceux/fceux.cfg config file, but may be useful for other
//  applications as well.
//
/////////////////////////////////////////////////////////////////

#include<SDL/SDL.h>

void DisplayState(SDL_KeyboardEvent *key)
{
    if (key->type == SDL_KEYUP)
        printf("RELEASED: ");
    else
        printf("PRESSED: ");
 
}

void DisplayModifiers(SDL_KeyboardEvent *key)
{
    SDLMod modifier = key->keysym.mod;
    if( modifier & KMOD_NUM ) printf( "NUMLOCK " );
    if( modifier & KMOD_CAPS ) printf( "CAPSLOCK " );
    if( modifier & KMOD_MODE ) printf( "MODE " );
    if( modifier & KMOD_LCTRL ) printf( "LCTRL " );
    if( modifier & KMOD_RCTRL ) printf( "RCTRL " );
    if( modifier & KMOD_LSHIFT ) printf( "LSHIFT " );
    if( modifier & KMOD_RSHIFT ) printf( "RSHIFT " );
    if( modifier & KMOD_LALT ) printf( "LALT " );
    if( modifier & KMOD_RALT ) printf( "RALT " );
    if( modifier & KMOD_LMETA ) printf( "LMETA " );
    if( modifier & KMOD_RMETA ) printf( "RMETA " );
}

void DisplayKey(SDL_KeyboardEvent *key)
{
    printf( "%s\n", SDL_GetKeyName(key->keysym.sym));
}

void DisplayKeysym(SDL_KeyboardEvent *key)
{
    printf ("%d\n", key->keysym.sym);
}


int main(int argc, char** argv)
{
    SDL_Surface *screen;
 
    if (SDL_Init(SDL_INIT_VIDEO) != 0) 
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }
 
    atexit(SDL_Quit);
 
    screen = SDL_SetVideoMode(320, 240, 0, SDL_ANYFORMAT);
    SDL_WM_SetCaption("Get SDL Keysyms! Now 50\% off!", NULL);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }
    SDL_Event event;
    int running = 1;
 
    while(running) {
        while(SDL_PollEvent(&event)) {
            switch(event.type){
                case SDL_KEYDOWN:
                case SDL_KEYUP:
                    DisplayState(&event.key);
                    DisplayModifiers(&event.key);
                    DisplayKey(&event.key);
                    DisplayKeysym(&event.key);
                    break;
                case SDL_QUIT:
                    running = 0;
                    break;
            }
        }
    }
    
    return 0;
}