Mouse event handling

  1. why is SDL_MOUSEBUTTONDOWN = 5 and SDL_MOUSEBUTTONUP = 6 ? So one
    cannot do a clean ‘or’ with those two to check if something just
    happened with the button.

Event types are not bitmasks.

  1. Everytime i press a mousebutton (without releasing it) i get two (!)
    mouseevents, after releasing the button i get another two events, why?
    Can i just ignore the second mouseevent (which would make my code look
    ugly)?

A simple test doesn’t show that happening on my platform, but looking at
your code, the first thing I see that might be wrong is that:

       ( (inp_mtable[i].type & event.button.type) != 0) &&
       ( (inp_mtable[i].button & event.button.button) != 0) )

… event types, and mouse buttons, are not bitmasks.

There exist bitmask versions of these constants (e.g. SDL_BUTTON_LMASK
and SDL_MOUSEBUTTONDOWNMASK), but those are used in other functions and
do not show up directly in the event structure values AFAIK. There are
macros SDL_EVENTMASK and SDL_BUTTON in the headers that turn single
event-type or mouse-button values into their bitmasks (by using bitwise
left shift), but I can’t find these in the documentation, so they’re
probably considered internal.

—> Drake WilsonOn Mon, May 24, 2004 at 10:01:09PM +0200, Michael Sauer wrote:

Hi there,
i got two points with mouse handling:

  1. why is SDL_MOUSEBUTTONDOWN = 5 and SDL_MOUSEBUTTONUP = 6 ? So one
    cannot do a clean ‘or’ with those two to check if something just
    happened with the button. Ok, the or’ed value would give 11, but
    nevertheless, the easy programming like that
    if ( (SDL_MOUSEBUTTONDOWN | SDL_MOUSEBUTTONUP | SDL_JOYBUTTONDOWN) &
    event.type != 0) { }
    would not work (not very nice imo). Will that be changed in feature
    versions of SDL ?

  2. Everytime i press a mousebutton (without releasing it) i get two (!)
    mouseevents, after releasing the button i get another two events, why?
    Can i just ignore the second mouseevent (which would make my code look
    ugly)?

Code in charge:

SDL_PollEvent(&event);

if ( (event.type == SDL_MOUSEBUTTONDOWN) ||
(event.type == SDL_MOUSEBUTTONUP) )
for (i = 0; i < EOTG_MAX_MOUSECALLBACKS; i++)
if (inp_mtable[i].callback != NULL)
if ( (inp_mtable[i].x < event.button.x) &&
(inp_mtable[i].y < event.button.y) &&
( (inp_mtable[i].x+inp_mtable[i].w) > event.button.x) &&
( (inp_mtable[i].y+inp_mtable[i].h) > event.button.y) &&
( (inp_mtable[i].type & event.button.type) != 0) &&
( (inp_mtable[i].button & event.button.button) != 0) )
inp_mtable[i].callback(inp_mtable[i].id);

mIc

  1. Everytime i press a mousebutton (without releasing it) i get two (!)
    mouseevents, after releasing the button i get another two events, why?
    Can i just ignore the second mouseevent (which would make my code look
    ugly)?

Args, forget about that one, i forgot to remove the event flag (to be
clear, forgot the event.type = SDL_NOEVENT) …

mIc

Michael Sauer wrote:

Hi there,
i got two points with mouse handling:

  1. why is SDL_MOUSEBUTTONDOWN = 5 and SDL_MOUSEBUTTONUP = 6 ? So one
    cannot do a clean ‘or’ with those two to check if something just
    happened with the button. Ok, the or’ed value would give 11, but
    nevertheless, the easy programming like that
    if ( (SDL_MOUSEBUTTONDOWN | SDL_MOUSEBUTTONUP | SDL_JOYBUTTONDOWN) &
    event.type != 0) { }
    would not work (not very nice imo). Will that be changed in feature
    versions of SDL ?

I haven’t done any mouse stuff with SDL yet, but, um… Wouldn’t you use:
if((SDL_MOUSEBUTTONDOWN || SDL_MOUSEBUTTONUP ||
SDL_JOYBUTTONDOWN) && event.type !=0) {}
using the comparison “||” rather than the mathematical “|”? Seems to me
to be pretty logical to make SDL_MOUSEBUTTON* values whatever they
want, because their event code is distanced from the code they are
forced to use in order to ACTUALLY get the events. I imagine that they
have maybe the quit event = 0, keydown/up = 1 and 2, maybe mouse
movement = 3 and 4 (mouse move x/y), and then mouse buttons = 5 and 6.
Throw some joystick code somewhere in there, and you can have a real
nice wrapper for whatever system-specific code may be necessary to
ACTUALLY read the state of things. Kinda’ convenient if you ask me.

–Scott

I haven’t done any mouse stuff with SDL yet, but, um… Wouldn’t you use:
if((SDL_MOUSEBUTTONDOWN || SDL_MOUSEBUTTONUP ||
SDL_JOYBUTTONDOWN) && event.type !=0) {}
using the comparison “||” rather than the mathematical “|”?

Na, i won’t. I want to keep a list of mouse actions in memory, each one
bound to rectangular part of the screen. Some of those actions need a
specifig MOUSEBUTTONDOWN, some a specifig MOUSEBUTTONUP and some don’t
care about MOUSEBUTTONDOWN / MOUSEBUTTONUP and need both events.
Some other problems arise with MOUSE_BUTTON_LEFT / MOUSE_BUTTON_RIGHT /
MOUSE_BUTTON_MIDDLE, which cannot be or’ed too. That way i cannot add an
action that is triggered by all three buttons, i have to create 3
actions …

mIc

Michael Sauer wrote:

Hi there,
i got two points with mouse handling:

  1. why is SDL_MOUSEBUTTONDOWN = 5 and SDL_MOUSEBUTTONUP = 6 ? So one
    cannot do a clean ‘or’ with those two to check if something just
    happened with the button. Ok, the or’ed value would give 11

Nope, that’d be 7

, but
nevertheless, the easy programming like that
if ( (SDL_MOUSEBUTTONDOWN | SDL_MOUSEBUTTONUP | SDL_JOYBUTTONDOWN) &
event.type != 0) { }
would not work (not very nice imo). Will that be changed in feature
versions of SDL ?

Well, that’s useless since an event can’t be mouse up and mouse down at
the same time. More generally, an event can’t be multiple things at
once. If you want to “factor” events, you can still use the switch/case
statement with fallthroughs :
switch (event.type)
{
case SDL_KEYDOWN:
case SDL_MOUSEMOTION:
/* these two do the same thing … */

break;

 case SDL_MOUSEBUTTONDOWN:  
 /* handle this one differently */

}

Stephnae

Hi there,
i got two points with mouse handling:

  1. why is SDL_MOUSEBUTTONDOWN = 5 and SDL_MOUSEBUTTONUP = 6 ? So one
    cannot do a clean ‘or’ with those two to check if something just
    happened with the button. Ok, the or’ed value would give 11, but
    nevertheless, the easy programming like that
    if ( (SDL_MOUSEBUTTONDOWN | SDL_MOUSEBUTTONUP | SDL_JOYBUTTONDOWN) &
    event.type != 0) { }
    would not work (not very nice imo). Will that be changed in feature
    versions of SDL ?

It is so they work well with the switch() statement or as indexes into a
vector of pointers to function. They are most commonly used in exactly
that way so having them be sequential numbers works best.

To get the effect that you want use code something like:

switch(event.type
{
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_JOYBUTTONDOWN:
}

  1. Everytime i press a mousebutton (without releasing it) i get two (!)
    mouseevents, after releasing the button i get another two events, why?
    Can i just ignore the second mouseevent (which would make my code look
    ugly)?

Never seen that, are you sure you are testing to see if you actually got
a new event? If the queue is empty the event structure will not be
changed.

Code in charge:

SDL_PollEvent(&event);

if ( (event.type == SDL_MOUSEBUTTONDOWN) ||
(event.type == SDL_MOUSEBUTTONUP) )
for (i = 0; i < EOTG_MAX_MOUSECALLBACKS; i++)
if (inp_mtable[i].callback != NULL)
if ( (inp_mtable[i].x < event.button.x) &&
(inp_mtable[i].y < event.button.y) &&
( (inp_mtable[i].x+inp_mtable[i].w) > event.button.x) &&
( (inp_mtable[i].y+inp_mtable[i].h) > event.button.y) &&
( (inp_mtable[i].type & event.button.type) != 0) &&
( (inp_mtable[i].button & event.button.button) != 0) )
inp_mtable[i].callback(inp_mtable[i].id);

Bob PendletonOn Mon, 2004-05-24 at 15:01, Michael Sauer wrote:

mIc


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±-------------------------------------+

  1. Everytime i press a mousebutton (without releasing it) i get two (!)
    mouseevents, after releasing the button i get another two events, why?
    Can i just ignore the second mouseevent (which would make my code look
    ugly)?

Args, forget about that one, i forgot to remove the event flag (to be
clear, forgot the event.type = SDL_NOEVENT) …

Why do you need that? Are you forgetting to check to see if you got an
event when you called SDL_PollEvent()?

	Bob PendletonOn Mon, 2004-05-24 at 15:08, Michael Sauer wrote:

mIc


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±-------------------------------------+

Hi,

I have a simple question about SDL_MOUSEBUTTONDOWN (UP). I couldnt find anything on how you can use that command on an image such as a button. Lets say I made an update button and placed it in the middle of the screen using the command apply_surface (280, 350, update, screen);. How would I make it so when someone clicks there it does something like update a bunch files from a specific location? Thx in advance.

~ Jonathan---------------------------------
How low will we go? Check out Yahoo! Messenger?s low PC-to-Phone call rates.

if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (event.button.x >= 280 && event.button.x < 280 + ButtonImage->w &&
event.button.y >= 350 && event.button.y < 350 + ButtonImage->h)
{
update_a_bunch_of_files_from_a_specific_location();
/* You’re weclome ;^) */
}
}

-bill!On Sun, Sep 03, 2006 at 05:51:03PM -0700, jonathan racioppo wrote:

Hi,

I have a simple question about SDL_MOUSEBUTTONDOWN (UP). I couldnt find anything on how you can use that command on an image such as a button. Lets say I made an update button and placed it in the middle of the screen using the command apply_surface (280, 350, update, screen);. How would I make it so when someone clicks there it does something like update a bunch files from a specific location? Thx in advance.

All,
I need to multplex a video feed and it’s related audio feed (acheived
via SDL)in order to transmit across network via UDP/RTP - does anyone
know of a good multiplexing API I could use? The video is encoded using
H.323 and the audio is AMR.
Many thanks,
-Chris Dobbs

Hello Bill,

Tuesday, September 5, 2006, 6:06:15 PM, you wrote:> On Sun, Sep 03, 2006 at 05:51:03PM -0700, jonathan racioppo wrote:

Hi,

I have a simple question about SDL_MOUSEBUTTONDOWN (UP). I couldnt find anything on how you can use that command on an image such as a button. Lets say I made an update button and placed it in the middle of the screen using the command apply_surface (280, 350, update, screen);. How would I make it so when someone clicks there it does something like update a bunch files from a specific location? Thx in advance.

if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (event.button.x >= 280 && event.button.x < 280 + ButtonImage->w &&
event.button.y >= 350 && event.button.y < 350 + ButtonImage->h)
{
update_a_bunch_of_files_from_a_specific_location();
/* You’re weclome ;^) */
}
}

Simple and effective :slight_smile:

The only thing I would suggest is that you look for a mouse button up,
with the cursor in the same region THEN do the processing.


Best regards,
Peter mailto:@Peter_Mulholland