How do i find out which GWT element has focus?

后端 未结 4 589
深忆病人
深忆病人 2021-01-13 07:58

I would like to find out, in GWT, which element currently has focus. Basically i was working on virtual keyboard in our application. All keys are working fine except tab key

4条回答
  •  心在旅途
    2021-01-13 08:39

    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?.

    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.

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

提交回复
热议问题