[haiku-3rdparty-dev] Re: How to ascertain with which mouse Button is BPictureButton clicked (left or right)

  • From: Stephan Aßmus <superstippi@xxxxxx>
  • To: haiku-3rdparty-dev@xxxxxxxxxxxxx
  • Date: Wed, 09 Mar 2011 07:47:11 +0100

Am 09.03.2011 01:00, schrieb Rene Gollent:
On Tue, Mar 8, 2011 at 5:49 PM, Robert Stiehler
<r.stiehler85@xxxxxxxxxxxxxx>  wrote:
is there a way to get knowledge about with which mouse button a
BPictureButton was clicked (left or right mouse button (maybe event by
keyboard))?

In order to detect that, you'll need to get at the message which
triggered the mouse down event. i.e. by overriding BPictureButton's
MouseDown and doing something like the following:

void
CustomPictureButton::MouseDown(BPoint point)
{
   BMessage *message = CurrentMessage();
   int32 buttons = 0;
   if (message->FindInt32("buttons",&buttons) == B_OK)
   {
     switch(buttons)
     {
       case B_PRIMARY_MOUSE_BUTTON:
       {
         // left click
       }
       break;

       case B_SECONDARY_MOUSE_BUTTON:
       {
         // right click
       }
       break;

       case B_TERTIARY_MOUSE_BUTTON:
       {
         // middle click
       }
       break;
     }
   }

   BPictureButton::MouseDown(point);
}

The buttons int32 is a bit mask. Therefor the code is more future proof like this:

    BMessage* message = CurrentMessage();
    int32 buttons = 0;
    if (message->FindInt32("buttons", &buttons) == B_OK) {
        if ((buttons & B_PRIMARY_MOUSE_BUTTON) != 0) {
            // ...
        }
        if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0) {
            // ...
        }
        // ...
    }

At the moment, the input_server or app_server (don't remember which) will, to comply with BeOS behavior, not generate another mouse down event when the user presses additional buttons while already holding one button. However, the additional buttons will appear in the MouseMoved() message buttons field. So depending on where such a switch block appears in an application's code, it might not work as expected.

Best regards,
-Stephan

Other related posts: