如何获取相对和绝对鼠标指针位置?

4

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

我需要:

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

8

这将获取光标位置相对于当前显示器

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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接