How to generate a SDL_MOUSEMOTION event

I would like to generate a SDL_MOUSEMOTION event.
So tried to do like I sucessfully did for SDL_MOUSEBUTTONDOWN:

event.type = SDL_MOUSEMOTION;
event.motion.x = myX;
event.motion.y = myY;

But unfortunately my event loop doesn’t receive anything so I guess that the event is not well initialized.

How to do please?


I just tested this and it looks like you have to set windowID, too.

mouseTest.type = SDL_MOUSEMOTION;
mouseTest.motion.windowID = 1;
mouseTest.motion.x = 1;
mouseTest.motion.y = 1;

The user event type is probably more appropriate than sending fake mouse events, but this should answer your question about how to form the event. (I looked at what Visual Studio’s local output was for the object and then tested it.)

Hello Sam,
Very strange that it doesn’t work for me.

I have the following function:

void WID_widget_sendLeftBtnEvent(struct WID_widget *widget) {
	SDL_Event event = {};

	event.type = SDL_MOUSEMOTION;
	event.motion.windowID = 1;
	event.motion.x = widget->x;
	event.motion.y = widget->y;

	event.button.which = SDL_BUTTON_LEFT;
	event.button.x = widget->x;
	event.button.y = widget->y;

	event.type = SDL_MOUSEBUTTONUP;

And I still only receive the SDL_MOUSEBUTTONDOWN and SDL_MOUSEBUTTONUP events only.

When I physically touch the screen, I receive:

SDL_MOUSEMOTION 1024, x  450, y  615
SDL_MOUSEBUTTONDOWN 1025, x  450, y  615
SDL_MOUSEBUTTONUP 1026, x  454, y  601

Hi Alain!

Actually, I don’t think windowID is necessary; looking at my event system, it looks like I was pushing those events out to the correct windows, so without windowID, I wasn’t catching the event.

Is this on Android or iOS? This is working fine for me on Windows with SDL 2.0.10, but I don’t have an Android project right now that I can test mouse events on, and I don’t have an iPhone.

Could it be related to SDL_HINT_MOUSE_TOUCH_EVENTS? And if you push a touch event instead, does that work? I don’t have much experience pushing SDL events, so you’ll probably need to wait for somebody else to help you with this.

Are you positive it isn’t the way you’re reading SDL’s events? Like how I made that mistake with windowID. I’m polling events basically like this (minus a switch where I’m actually handling each event):

while (SDL_PollEvent(&sdlEvent) == HAS_EVENTS && !shouldExit) {
  if (sdlEvent.type == SDL_MOUSEMOTION) {
    // output the x and y data from sdlEvent.motion

I suspect SDL is pushing the event correctly into its event list, so it could be how you’re polling it.

Hi Sam,

I’m working on Linux.
As I said, if I touch the screen with the finger, I successfully receive the corresponding event.
So, I think I read the events correctly.
So, it seems that some other information is missing here to really trig the event.
It’s strange anyway that it works in your case.
I tried on 2.0.8 and 2.0.9 (Linux) and same. I don’t receive the event.
Maybe something fixed in 2.0.10 :-(.
Unfortunately, I cannot use 2.0.10 at the moment…


I’m not an expert on how SDL manages messages, but I remember when I used to write Windows games if there were several WM_TIMER messages in the message queue Windows would just eat all but one to try and help you out. Is there a chance that (maybe on only certain OSes) the mouse motion message is being eaten by the OS as it thinks there’s no point you needing that as there’s a mouse up/down in the queue anyway? Or the mouse pointer is already at the x,y point you’re sending so again the OS just thinks that’s an unnecessary message to bother putting on the queue?

I don’t think it come from such kind of things.
According to what Sam reported, I guess this event may have changed in 2.0.10.

Sorry for the late reply. Which platform are you encountering this problem on? And does it work correctly on Windows? This works for me on Windows with SDL 2.0.10, but I haven’t had a chance to try on Android yet. If it works on one platform but not another, we should at least document it.

From my understanding of it, SDL_PushEvent() (and the event list itself) shouldn’t care about the platform. Does pushing a UserEvent work correctly? What about a TouchFingerEvent? There could be a filter related to SDL_HINT_MOUSE_TOUCH_EVENTS that’s interfering with pushing this event, but that probably isn’t meant to prevent SDL from pushing the event.

I’m working on Ubuntu 18.04
I better think it’s a difference between 2.0.8 and 2.0.10.
Real touch works fine.
I will try to update to 2.0.10 asap but it’s not so simple as I cannot update the package. I have to rebuild the library and make install it.
I hope it will works fine…
Kind regards,