Why double output???

here is my code

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
#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>

int main ( int argc, char** argv )
{
    if(SDL_Init(SDL_INIT_EVERYTHING) != 0)  return 1;
    if(TTF_Init() != 0)                     return 11;

    int width = 640, height = 480;
    SDL_Window* win = SDL_CreateWindow("SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                                       width, height, SDL_WINDOW_SHOWN);

    if(win == NULL)                         return 2;


    const Uint8* state = SDL_GetKeyboardState(NULL);

    SDL_Event event;

    bool quit = false;
    while(quit == false)
    {
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)          quit = true;

            SDL_PumpEvents();
            if(state[SDL_SCANCODE_ESCAPE])      quit = true;
            if(state[SDL_SCANCODE_DOWN])        std::cout << "<DOWN>  was pressed" << std::endl;
            if(state[SDL_SCANCODE_LEFT])        std::cout << "<LEFT>  was pressed" << std::endl;
            if(state[SDL_SCANCODE_RIGHT])       std::cout << "<RIGHT> was pressed" << std::endl;
            if(state[SDL_SCANCODE_UP])          std::cout << "<UP>    was pressed" << std::endl;
            if(state[SDL_SCANCODE_Z])           std::cout << "<Z>     was pressed" << std::endl;
            if(state[SDL_SCANCODE_X])           std::cout << "<X>     was pressed" << std::endl;
            if(state[SDL_SCANCODE_C])           std::cout << "<C>     was pressed" << std::endl;
        }
    }
    SDL_DestroyWindow(win);
    SDL_Quit();

    return 0;
}


everything seems fine but you know what when i press
z, x or c it double outputs for example:

<Z>     was pressed
<Z>     was pressed


what is wrong with my code or i simply dont know a fact about SDL_GetKeyboardState()
Last edited on
Nothing helpful to say, but the title "What Da??" is not really attractive nor informative, try change the title to gain a little bit more attention towards this question.
ok, changed it
I have no experience with SDL, but I'll randomly guess that perhaps you are getting an event for both the press and release?
@Zhuge i tried that without releasing the key. and even before releasing the key it outputt twice
Last edited on
Try using a debugger or just printing out more information about the event and see what the two events are.
well sorry but i havent use any debugger after a research i leraned most of it but i stuck at using "watches"

i use C::B could you help a bit

hmm i found a video so everything seems to fine now
Last edited on
well sorry but i used debogger it seems fine. also i couldnt find how to take the output of the watches
Last edited on
1
2
3
4
5
6
            if(state[SDL_SCANCODE_1])           std::cout << "<1>       was pressed" << std::endl;
            if(state[SDL_SCANCODE_2])           std::cout << "<2>       was pressed" << std::endl;
            if(state[SDL_SCANCODE_3])           std::cout << "<3>       was pressed" << std::endl;
            if(state[SDL_SCANCODE_KP_1])        std::cout << "<Num[1]>  was pressed" << std::endl;
            if(state[SDL_SCANCODE_KP_2])        std::cout << "<Num[2]>  was pressed" << std::endl;
            if(state[SDL_SCANCODE_KP_3])        std::cout << "<Num[3]>  was pressed" << std::endl;


i also tried these. only my arrow keys are normal.
Why don't you add something to indicate what events you are receiving? Then you'll have more of an idea of what is going on. Or if you are using the debugger, just check the state of the "event" variable on the loops when it is outputting the line twice.
well i tried many things. even though i m new at using debugger, i believe i used it well. and i have no idea what to do. i m confused

EDIT: also checked it at another computer it still gives the same output

and when i added something more just to output it outputs this

<Z>     was pressed
this is bad
<Z>     was pressed
this is bad


so it works twice when i pressed something other than arrow keys
Last edited on
i couldnt find a way to fix it

maybe i should leave it for later.
Topic archived. No new replies allowed.