How to detect the right mouse button was clicked?

How to detect the right mouse button was clicked?
0

#1

Is it possible to detect when the right mouse button was clicked? I need to get the event and differentiate from left mouse button clicks, which I currently manage with ccTouchBegan.

Thanks!


#2

Good question. I would like to know that too!


#3

Go to cocos2dx/platform/XYZ/CCEGLView.cpp
You can modify void mouseButtonEventHandle(int iMouseID,int iMouseState) to forward iMouseID and iMouseState somewhere else (like notifiaction center, some singleton, etc.).


#4

https://github.com/cocos2d/cocos2d-x/blob/403deedd5fecc2e88456efce1936ef8376ffaaa2/cocos/platform/desktop/CCGLViewImpl-desktop.cpp#L727 is the function you want to update. Doesn’t seem like cocos2dx supports it out of the box yet, and like you said, you need to add an enum type for tracking left vs right click.

Actually, you could probably make the change to check for _RIGHT instead of just _LEFT, and make it touchID 2 instead of 1 in that case… edit: Nope, passing in a different id there always just results in touch->getTouchID being 0, so I’m not sure.


#5

I cannot speak to how this affects anything beyond touch->mouse_button_id being 0 or 1, but this seems to work so far. You can ignore the Antialiasing thing, I didn’t realize that was in the diff.

The touchID seems to be an index in an array of touches, so I just hacked in something on my own. Hope this helps. It won’t work for any platform other than win32, naturally.

index 51ef2f6..3fdaeb6 100644
--- a/cocos/base/CCTouch.h
+++ b/cocos/base/CCTouch.h
@@ -43,6 +43,11 @@ NS_CC_BEGIN
 class CC_DLL Touch : public Ref
 {
 public:
+
+    //my edit: check which mouse button this is
+    intptr_t mouse_button_id;
+
+
     /**
      * Dispatch mode, how the touches are dispatched.
      * @js NA
diff --git a/cocos/platform/CCGLView.cpp b/cocos/platform/CCGLView.cpp
index a4fd435..cf71e67 100644
--- a/cocos/platform/CCGLView.cpp
+++ b/cocos/platform/CCGLView.cpp
@@ -317,6 +317,10 @@ void GLView::handleTouchesBegin(int num, intptr_t ids[], float xs[], float ys[])
             Touch* touch = g_touches[unusedIndex] = new (std::nothrow) Touch();
             touch->setTouchInfo(unusedIndex, (x - _viewPortRect.origin.x) / _scaleX,
                                      (y - _viewPortRect.origin.y) / _scaleY);
+
+            //my edit to add in the mouse button
+            touch->mouse_button_id = ids[0];
+

             CCLOGINFO("x = %f y = %f", touch->getLocationInView().x, touch->getLocationInView().y);

diff --git a/cocos/platform/desktop/CCGLViewImpl-desktop.cpp b/cocos/platform/desktop/CCGLViewImpl-desktop.cpp
index fc1ceb8..d8918b2 100644
--- a/cocos/platform/desktop/CCGLViewImpl-desktop.cpp
+++ b/cocos/platform/desktop/CCGLViewImpl-desktop.cpp
@@ -343,6 +343,9 @@ bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float fram
     glfwWindowHint(GLFW_DEPTH_BITS,_glContextAttrs.depthBits);
     glfwWindowHint(GLFW_STENCIL_BITS,_glContextAttrs.stencilBits);

+    //my edit to add AA
+    //glfwWindowHint(GLFW_SAMPLES,4);
+
     int neededWidth = rect.size.width * _frameZoomFactor;
     int neededHeight = rect.size.height * _frameZoomFactor;

@@ -750,14 +753,14 @@ void GLViewImpl::onGLFWError(int errorID, const char* errorDesc)

 void GLViewImpl::onGLFWMouseCallBack(GLFWwindow* /*window*/, int button, int action, int /*modify*/)
 {
-    if(GLFW_MOUSE_BUTTON_LEFT == button)
+    if(GLFW_MOUSE_BUTTON_LEFT == button || GLFW_MOUSE_BUTTON_RIGHT == button)
     {
+        intptr_t id /*mouse_button_id, my edit */ = GLFW_MOUSE_BUTTON_LEFT == button ? 0 : 1;
         if(GLFW_PRESS == action)
         {
             _captured = true;
             if (this->getViewPortRect().equals(Rect::ZERO) || this->getViewPortRect().containsPoint(Vec2(_mouseX,_mouseY)))
             {
-                intptr_t id = 0;
                 this->handleTouchesBegin(1, &id, &_mouseX, &_mouseY);
             }
         }
@@ -766,7 +769,6 @@ void GLViewImpl::onGLFWMouseCallBack(GLFWwindow* /*window*/, int button, int act
             if (_captured)
             {
                 _captured = false;
-                intptr_t id = 0;
                 this->handleTouchesEnd(1, &id, &_mouseX, &_mouseY);
             }
         }

#6

cocos2d-x supports this.

	auto mouseListener = EventListenerMouse::create();
    mouseListener->onMouseMove = CC_CALLBACK_1(DesktopPointer::onMouseMove, this);
    mouseListener->onMouseDown = [=](EventMouse *event){
        if(event->getMouseButton() == EventMouse::MouseButton::BUTTON_LEFT){
      
        }
    };
    mouseListener->onMouseUp = [=](EventMouse *event){
        
        
        if(event->getMouseButton() == EventMouse::MouseButton::BUTTON_LEFT){
           
            
        }


    };
    _eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
#endif

#7

That’s totally true, I forgot about the standard event listener. I was mostly concerned with cocos2d::ui::Widget, since they all have special overloads for touch ended, like if you want a bind a right click to a ui::Button, you need to mess with its source, as far as I know.


#8

mark it as solution so it helps others as well. :smiley:


#9

I’m not OP haha


#10

lol … my bad.