简体   繁体   中英

How to get current mouse cursor type using windows API in java?

How can i get the current mouse cursor type using windows API in java ? (Cursor type means : pointer, text cursor , busy cursor and the rest of the categories)

I've found a link Here

where the api is given as

BOOL WINAPI GetCursorInfo(
  __inout  PCURSORINFO pci
);

But how to use the api in java

I think the closest you're going to get is thru:

  1. Find the component in your java app that is the focusOwner
  2. Call getCursor().getType();

You can use JNA - java native access. It provides access to native libraries, like DLL in windows. https://github.com/twall/jna#readme

This works quite well for me on Win7. The script is in Python but should be easy enough to translate into any other language. Of course, it only works if the respective application isn't using custom cursors:

from win32con import IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, \
    IDC_HELP, IDC_IBEAM, IDC_ICON, IDC_NO, IDC_SIZE, IDC_SIZEALL, \
    IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE, IDC_UPARROW, IDC_WAIT
from win32gui import LoadCursor, GetCursorInfo

def get_current_cursor():
    curr_cursor_handle = GetCursorInfo()[1]
    return Cursor.from_handle(curr_cursor_handle)

class Cursor(object):
    @classmethod
    def from_handle(cls, handle):
        for cursor in DEFAULT_CURSORS:
            if cursor.handle == handle:
                return cursor
        return cls(handle=handle)
    def __init__(self, cursor_type=None, handle=None):
        if handle is None:
            handle = LoadCursor(0, cursor_type)
        self.type = cursor_type
        self.handle = handle

DEFAULT_CURSORS \
    = APPSTARTING, ARROW, CROSS, HAND, HELP, IBEAM, ICON, NO, SIZE, SIZEALL, \
      SIZENESW, SIZENS, SIZENWSE, SIZEWE, UPARROW, WAIT \
    = Cursor(IDC_APPSTARTING), Cursor(IDC_ARROW), Cursor(IDC_CROSS), \
      Cursor(IDC_HAND), Cursor(IDC_HELP), Cursor(IDC_IBEAM), Cursor(IDC_ICON), \
      Cursor(IDC_NO), Cursor(IDC_SIZE), Cursor(IDC_SIZEALL), \
      Cursor(IDC_SIZENESW), Cursor(IDC_SIZENS), Cursor(IDC_SIZENWSE), \
      Cursor(IDC_SIZEWE), Cursor(IDC_UPARROW), Cursor(IDC_WAIT)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM