Hiding the mouse pointer / cursor

I’m looking for a way to hide the mouse pointer in cocos2d-x. Has anyone looked in to doing this before and perhaps could give me a head start in my research. I know about glutSetCursor in opengl, is there a way to use the glview to hide the cursor in that case?

Once again, any help will be appreciated.

Cocos2d-x 3.0 uses GLFW. You can hide mouse cursor with
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);.
But you’ll need to modify the code of cocos2d-x, since there is no API to get the window handle from the client code.

@dotsquid Thanks a lot for the pointer, would this work on Android as well as Windows and Mac?

I have no idea.

Looks like there’s no way to change the mouse pointer on Android. :;angry Oh well, thanks again for your help.

It turns out that Android SDK does not provide an API for hiding the mouse pointer. That’s ridiculous.

In Cocos2d-x 3.2 you can get the window handle with Director::getInstance()->getOpenGLView->getWindow().
I don’t know if it’s available in earlier versions.

auto director = Director::getInstance();
auto glview = director->getOpenGLView();
glfwSetInputMode(glview->getWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);

On mac and cocos2d-x 3.3 I tried this, but am unable to get a handle on getWindow(). The only method I see is getCocoaWindow().

Has anyone figured out how to hide mouse cursor?

Thanks!

Just wondering if anyone has any ideas on how you could hide the mouse cursor on mac specifically but desktop in general. Id like to hide the cursor and then create my own cursor in cocos2dx.

Any ideas much appreciated :smile:

Currently using cocos2d-x 3.3 on mac

So I figured out how to hide the cursor on mac. Below is how I was able to achieve this. I welcome any suggestions on a better way.

I added the following functions to platform/mac/CCApplication-mac.h, & CCApplication-mac.mm

void Application::hideCursor()
{
    if (!cursorHidden)
    {
	cursorHidden = true;
        [NSCursor hide];
    }
}

void Application::unHideCursor()
{
    if(cursorHidden)
    {
	cursorHidden = false;
	[NSCursor unhide];
    }
}

Now in my onMouseMove event if the cursor is out of visible bounds of the screen I call my unHideCursor() function and if its in bounds I call hideCursor() function. I created a bool called cursorHidden as well. This was because every time you call [NSCursor hide] you have to call [NSCursor unhide] the same amount of times. This ovoids calling [NSCursor hide] or [NSCursor unhide] multiple times. As a matter of fact I guess I could have created a bool local to where my mouse event handlers are and not even call into Application if I don’t need to. Not sure theres really any difference. So either way now when my cursor is in bounds I just have a “cursor” sprite that follows my mouse position. Works like a charm.

It would be really nice if hiding the native cursor was an options and I didn’t have to modify these files to achieve this since my changes could get wiped out if I upgrade, but it works well enough for now!

I hope this helps someone :smile:

As I tried what is mentioned here, which is unnecessary as of now, I wanted to update with the current status of this feature in cocos2d-x, which is described here: https://github.com/Sheado/cocos2d-x/commit/741c2d67079cf035c0567509c142788b679a5bc7

Simply calling glview->setCursorVisible(false); works a charme.

2 Likes

Thats good to know! Thanks for the update!