简体   繁体   English

我如何找出哪个GWT元素有焦点?

[英]How do i find out which GWT element has focus?

I would like to find out, in GWT, which element currently has focus. 我想在GWT中找出目前关注的元素。 Basically i was working on virtual keyboard in our application. 基本上我在我们的应用程序中使用虚拟键盘。 All keys are working fine except tab key. 除tab键外,所有键都正常工作。 If i get focused element then i can work out the tab key code. 如果我得到聚焦元素,那么我可以计算出tab键代码。

In javascript and jquery we can get this using document.activeElement . 在javascript和jquery中,我们可以使用document.activeElement来获取它。 Hope some body will put me in right way to achieve this. 希望有些机构会让我以正确的方式实现这一目标。

Help would be appreciated. 帮助将不胜感激。

The fact that it's not supported in "all browsers" is only important if your app is targeting all browsers. 只有当您的应用定位到所有浏览器时,“所有浏览器”都不支持这一事实。 activeElement is currently supported by quite a few browsers Why is there no isFocused() in GWT? 很多浏览器目前支持activeElement 为什么GWT中没有isFocused()? .

I needed something similar, I needed to know from inside a widget if it had focus. 我需要类似的东西,我需要从小部件内部知道它是否有焦点。 I did the following 我做了以下

protected native boolean hasFocus(Element element) /*-{
   return element.ownerDocument.activeElement == element;
}-*/; 

I needed to pass in the current element to get the proper document, just calling 我需要传入当前元素来获取正确的文档,只需调用即可

document.activeElement;

did not give me the document I needed. 没有给我我需要的文件。 You could likely do the same but pass in the a different element (RootPanel element maybe?) and return the in focus Element rather than a bool. 你可能会做同样的事情,但传入一个不同的元素(RootPanel元素可能?)并返回焦点元素而不是bool。

protected native Element elementInFocus(Element element) /*-{
   return element.ownerDocument.activeElement;
}-*/; 

document.activeElement doesn't work in all browsers so there's no support for that in GWT. document.activeElement不适用于所有浏览器,因此在GWT中不支持。 You could maybe use focus&blur handlers to keep track which element has it. 您可以使用焦点和模糊处理程序来跟踪哪个元素具有它。

Short template: 短模板:

public class IntBox extends com.google.gwt.user.client.ui.IntegerBox {

private boolean focused=false;

public IntBox(){

        addFocusHandler(new FocusHandler() {

            @Override
            public void onFocus(FocusEvent event) {
                focused=true;
            }
        });

        addBlurHandler(new BlurHandler() {

            @Override
            public void onBlur(BlurEvent event) {
                focused=false;
            }
        });

    }

    public boolean isFocused() {
        return focused;
    }

}

We can now use the Elemental library to accomplish this. 我们现在可以使用元素库来实现这一目标。

http://www.gwtproject.org/articles/elemental.html http://www.gwtproject.org/articles/elemental.html

the exact function would be 确切的功能是

elemental.client.Browser.getDocument().getActiveElement()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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