How to check "is cursor visible"?

How to check "is cursor visible"?
closed account (E0p9LyTq)
The answer depends on the operating system.
Windows, my bad.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
typedef struct tagCURSORINFO {
  DWORD   cbSize;
  DWORD   flags;
  HCURSOR hCursor;
  POINT   ptScreenPos;
} CURSORINFO;
*/

CURSORINFO ci = {sizeof(CURSORINFO)};

if (GetCursorInfo(&ci))
{
    if (ci.flags == 0)
        ; // cursor is hidden
    else
        ; // cursor is showing (or possibly "suppressed")
}
else
{
    ; // GetCursorInfo function failed
}


https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getcursorinfo

https://docs.microsoft.com/en-us/windows/desktop/api/Winuser/ns-winuser-tagcursorinfo
It is not work. It's probably always see the cursor on x=0 and y=0 point, but user dont see this.
What do you mean by "cursor" (the mouse cursor? The text cursor?) and what do you mean by "visible" (e.g. do you consider that blinking the cursor affect its visibility?)?
I mean mouse cursor, if game window active, user dont see mouse cursor, but it's still on the game window center.
I don't understand what exactly isn't working, but the Win32 function to show/hide the cursor is ShowCursor.
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-showcursor

The function to check the state of a hidden/visible mouse cursor is through GetCursorInfo, as dutch mentioned.
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getcursorinfo
Last edited on
https://imgur.com/a/4FojzdX

Player dont see the cursor, but program still detect cursor.

On inventory cursor is visible (not in screenshot lol)
https://imgur.com/a/b9zAKfd

I want to stop clicking while in inventory, but

1
2
3
4
5
6
7
8
9
10
11
12
13
CURSORINFO ci = {sizeof(CURSORINFO)};

if (GetCursorInfo(&ci))
{
    if (ci.flags == 0)
        ; // cursor is hidden
    else
        ; // cursor is showing (or possibly "suppressed")
}
else
{
    ; // GetCursorInfo function failed
}

says cursor is always visible.
You should show your real code so we can see how your program "says cursor is always visible".

Did you check for the "suppressed" state?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
CURSORINFO ci = {sizeof(CURSORINFO)};

if (GetCursorInfo(&ci))
{
    if (ci.flags == 0)
        ; // cursor is hidden
    else if (ci.flags == 1)
        ; // cursor is visible
    else if (ci.flags == 2)
        ; // cursor is suppressed
    else
        ; // this shouldn't happen!
}
else
{
    ; // GetCursorInfo function failed
}

Last edited on
Everytime
1
2
    else if (ci.flags == 1)
        ; // cursor is visible 
Let's try to get on the same wavelength here.

Showing/hiding the cursor with ShowCursor([TRUE/FALSE]) will not prevent clicking events.

Do you have your own window, or are you trying to something with a window your program doesn't directly make/own?

If you want to make the cursor disappear, then do ShowCursor(false).

Here is a basic Win32 window example.
Press the Z button to toggle the mouse cursor being shown.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

//
// Modified from source: http://www.winprog.org/tutorial/simple_window.html
//

#include <iostream>
#include <windows.h>

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_LBUTTONDOWN:
        {
            std::cout << "Mouse click\n";
            break;
        }
        case WM_KEYDOWN:
        {
            if ( wParam == 'Z' )
            {
                static bool is_cursor_shown = true;
                is_cursor_shown = ! is_cursor_shown;
                ShowCursor(is_cursor_shown);
            }
            break;
        }
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    const char className[] = "myWindowClass";

    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = className;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        className,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}


Edit: dutch asked to see your real code, so we can get more of an idea of what you're doing. Are you just constantly polling the state of the mouse cursor? I don't know because I can't see the meat of your code.
Last edited on
Run this code concurrently with whatever action you're doing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <windows.h>

int main()
{
    while (true)
    {
        CURSORINFO ci = {sizeof(CURSORINFO)};

        if (GetCursorInfo(&ci))
        {
            if (ci.flags == 0)
                std::cout << "hidden\n"; // cursor is hidden
            else if (ci.flags == 1)
                std::cout << "visible\n"; // cursor is visible
            else if (ci.flags == 2)
                std::cout << "suppressed\n"; // cursor is suppressed
            else
                std::cout << "ERROR\n"; // this shouldn't happen!
        }
        else
        {
            std::cout << "Win32 error: " << GetLastError() << std::endl; // GetCursorInfo function failed
        }
    }
}


If the mouse state isn't changing from "hidden" to "visible" or vice-versa, it means that Minecraft or whatever program you're hovering over isn't actually hiding/showing the mouse at an OS level.
Last edited on
I just want to do program when user see cursor, the clicking get stopped. Clicking is done, but idk how to detect that user now see the cursor. Cursor show when player open inventory, chat, escape etc.

Edit: Copy - Paste upper code, and always spamming "visible".

So, how to disable my function, when player open inventory etc. without
1
2
3
4
if(inventorybuttonclicked)
{
...
}
Last edited on
To be clear: It spams "visible" even when you're active in the Minecraft window, with the cursor not physically being shown on your screen?

If this is the case: Then Java must be using some other method to hide the cursor, which Windows GetCursorInfo API is not aware of. This is just a guess. I don't know what Java GUI library Minecraft uses. It's possible it might be setting the mouse cursor to be invisible pixels (but technically "shown") instead of hiding it at an OS level. This is where I can't help anymore, since I don't know Java GUI stuff.

Edit:
So, how to disable my function, when player open inventory etc. without
1
2
3
4
if(inventorybuttonclicked)
{
...
}

Sorry, I really don't know what you mean. Seems to be a language disconnect. If you want to show a hidden cursor, call ShowCursor(true);
Last edited on
Yes, it spam "visible" when I dont see the physical cursor.

So how can I do "stop when menu or etc. shows"?

I want to disable clicking by opening inventory, chat etc. The easiest not amateur method in my opinion is stop clicking if cursor physically showed. For language, cursor is visible all the time, but for user, only when he open inventory, chat, menu etc.. So - stop clicking if player see physically cursor in Minecraft. This should prevent dropping items from inventory by clicker.

But if it's impossible, I can do in my program to a user can bind what button he want (for example: E - Minecraft inventory button) to stop clicking. Not professional method but :/
Last edited on
Topic archived. No new replies allowed.