Detecting double click despite cursor warping

This isn’t a problem with SDL per se, but with the specific thing I’m trying to do with it. I made my own GUI system, and recently I decided that a dial (or knob) should have its value changed not just by clicking on it and dragging the mouse up or down, but also that when doing this SDL_SetRelativeMouseMode(1) should be used so that I can drag the mouse indefinitely in either direction, which is quite crucial for instances where the knob is near the bottom or top of the screen.

The problem is that I also chose to have a double click on the control reset the knob’s value, but since the first click turns on the relative mouse mode which moves the cursor to the center of the window then if the second click follows quickly enough then in those cases it is not recognised as a double click, event.button.clicks has a value of 1, presumably because SDL recognises the two clicks as being at very different coordinates in the window. Note that when releasing a click then SDL_WarpMouseInWindow() is called to move the cursor back to where it was at the start of the first click, so in most cases the second click takes place at the same coordinates and SDL sets event.button.clicks to 2 as desired, but it seems that when the second clicks follows quickly relative to the FPS the second click might be identified as occurring at the coordinates of the centre of the window, and therefore too far from the location of the first click to be identified as a double click.

So my question is how can I work around this problem and properly detect the double click despite my cursor warping?

When I implement dragging, I use a dead zone so if the mouse button is held AND moved a certain distance from the mouse down coords, then I start the dragging logic (i.e. set relative mode in your case).

1 Like

That’s a good idea, it would also solve the problem of having the cursor flash off and back on when double clicking. Thanks!

Edit: yep that did it quite elegantly, problem solved!