Java 如何获取光标的相对和绝对位置?

Java 如何获取光标的相对和绝对位置?,java,cursor,position,swt,Java,Cursor,Position,Swt,如何使用SWT获取光标的当前位置 我需要: 绝对位置(仅相对于当前显示) 相对于当前激活的控件的位置 这将获取光标相对于当前显示的位置: import org.eclipse.swt.widgets.Display; [...] Point cursorLocation = Display.getCurrent().getCursorLocation(); 要获得相对于聚焦控件的位置,您必须转换它: import org.eclipse.swt.widgets.Display; [...]

如何使用SWT获取光标的当前位置

我需要:

  • 绝对位置(仅相对于当前
    显示
  • 相对于当前激活的
    控件的位置

  • 这将获取光标相对于当前显示的位置:

    import org.eclipse.swt.widgets.Display;
    [...]
    Point cursorLocation = Display.getCurrent().getCursorLocation();
    

    要获得相对于聚焦控件的位置,您必须转换它:

    import org.eclipse.swt.widgets.Display;
    [...]
    Point cursorLocation = Display.getCurrent().getCursorLocation();
    Point relativeCursorLocation = Display.getCurrent().getFocusControl().toControl(cursorLocation);