如何获得相对和绝对光标位置?

Kaa*_*zia 2 java swt position cursor

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

我需要:

  1. 绝对位置(仅相对于当前Display
  2. 对于当前活动的位置Control

Kaa*_*zia 5

这将获取 Cursor相对于当前 Display 的位置:

import org.eclipse.swt.widgets.Display;
[...]
Point cursorLocation = Display.getCurrent().getCursorLocation();
Run Code Online (Sandbox Code Playgroud)


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

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