I need to write a KeyUpEvent event KeyUpEvent - KeyUpHandler . This handler will be tied to a panel with so many widgets (input fields, text areas, checkboxes, etc.). In this panel, some kind of action will be performed on Enter . But if the focus is in the text area ( <textarea> , class TextArea ) and it is not read-only , then the action should not be performed, because in this case only a line break should occur and that's all. When the focus is on any other element attached to the panel, the action should be performed.

Question: how to determine in the event handler of the KeyUpEvent event whether focus is currently in textarea ?

  • I'm not good at gwt, but it seems to me that there should be something like isFocused or isActive, etc. - Alex Kapustin
  • There is no such thing in GWT. There are only FocusHandler and BlurHandler , but then they will have to be hung on all the textarea and add a field responsible for the textarea . - angry
  • can then, before performing the action, check where it is in focus - Gorets

2 answers 2

In theory, KeyUpHandler accepts a KeyUpEvent , which is the successor to GwtEvent. And GwtEvent, in turn, has getSource . It can usually be used to identify the source.

But ... why don't you just set this handler for the specified field instead of frightening some poor if?

  • Because there can be different controls on the panel (unknown in advance). And event handling should be just for the panel as a whole. If you put a textarea on the panel and put focus into it, then in the handler, the getSource method getSource return the panel, not the textarea . - angry
  • Well, when you form these controls, can you not expose handlers? - cy6erGn0m
  • The handler is added to the panel. In that place we do not know in advance what we will have on this panel. And what is added to the panel does not know that the data is sent to the server by enter . - angry
  • He does not need to know. He just needs to inform this handler or factory of controls. So the answer above is fail because it is a clumsy crutch. - cy6erGn0m

The solution was:

  if (e.getNativeKeyCode() == KeyCodes.KEY_ENTER) { /* Здесь getEventTarget возвращает тот элемент, который сгенерировал событие, getCurrentEventTarget() и getRelatedEventTarget() возвращают общую панель т.е. тот элемент, на который прикреплен данный обработчик (как и метод getSource) */ EventTarget et = event.getNativeEvent().getEventTarget(); Element e = Element.as(et); if ("TEXTAREA".equalsIgnoreCase(e.getTagName())) { TextAreaElement area = e.<TextAreaElement>cast(); if (!(area.isReadOnly())) { return; } } submit(); }