SDL_2 button.clicks field?

Hello,

I am having some issues handling double clicking in SDL_2. I have tried and tried different methods, but to no avail. When I output the return values of ev->button->clicks it seems to return random characters and numbers, and nothing upon a button click. If anyone could provide example code for handling double clicking, that would be great (or direct me to some). I have searched far and wide and can't seem to find an example that helps. I have checked SDL's documentation and can't seem to find any either. Thanks again.

-Daniel
You need to post code.

For what it's worth, you're most likely not handling the event instance correctly if you're getting garbage. An SDL event is actually a union type of various events. To make sure you have a mouse button event, check ev->type for the values "SDL_MOUSEBUTTONDOWN" and "SDL_MOUSEBUTTONUP".

Documentation is here: https://wiki.libsdl.org/SDL_Event

Sucks they don't have examples... would be really simple to add one.
Just looked, they do have examples... they just forgot to remove the "insert example here" section for some reason...
Last edited on
I want to say your right. I usually don't have a problem handling SDL_Events, but this one particularly is giving me issues. Below is what I've tried recently. I've tried a similar "if statement" approach that didn't work either.

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
    while (ev->type != SDL_QUIT )
    {
        SDL_PollEvent(ev);
        SDL_RenderClear(renderer);
        
        switch (ev->type)
        {
            case SDL_MOUSEBUTTONDOWN:
                switch (ev->button.clicks)
            {
                    case 1: std::cout << "Single click" << std::endl;
                        break;
                    case 2: std::cout << "Double Click" << std::endl;
                    break;
                    
                    default:
                        break;
            }
                break;
                
            default:
                break;
        }
        
        //Outputs random values
        std::cout << ev->button.clicks << std::endl;
        
        SDL_RenderPresent(renderer);
    }
Note that ev->button.clicks is an Uint8 which is the same as unsigned char so cout will output as if it was a character. To output the numerical value you should convert it to one of the bigger integer types before printing it.

 
std::cout << static_cast<int>(ev->button.clicks) << std::endl;
Last edited on
I've just added the static cast. The output still shows a bunch or random numbers, usually 38, 41, 40, 112 something like that (when the mousebutton is pressed). Otherwise returns zero.
Make sure ev->type == SDL_MOUSEBUTTONDOWN before printing the clicks. Also make sure you use SDL version 2.0.3 or later.
Last edited on
In that example, youll fetch mouse data from the event instance even though its outside of the block of code that checks whether or not its actually a mouse button event.
Right, I have now tried encompassing the output message in an if statement like so:

1
2
3
4
        if (ev->type == SDL_MOUSEBUTTONDOWN)
        {
            std::cout << static_cast<int>( ev->button.clicks ) << std::endl;
        }


However, the output still seems to be returning random integers. Is there anyway I can check my current version of SDL?
1
2
3
SDL_version version;
SDL_GetVersion(&version);
std::printf("SDL version %d.%d.%d.\n", version.major, version.minor, version.patch);

That's my problem. It seems I am running 2.0.1, thanks Peter! I thought I had updated, but looks like I was wrong.
How does that even work...? If it's an out of date library that doesn't include the feature, why doesn't the compiler say something about it... i.e. shouldn't clicks be missing?
Honestly I was wondering the same thing, but the clicks field is still present in the code. I checked all the headers to insure that, but the output from what Peter gave me returned 2.0.1. Which is really odd. I am currently trying to update to 2.0.3, but now I am getting system crashes at compile time.
Fixed that crashing error, it required manual compilation, man I was behind on my SDL updates! Thanks for everyones help! The clicks field seems to be returning the correct values now!
Topic archived. No new replies allowed.