user195488
user195488

Reputation:

Disable Key press event temporarily

Is it possible, in GWT, to temporarily suspend a certain key press until it is desired to allow that key press again? Like a global suspension. I have an issue where when I open the date picker on a RelativeDateItem in SmartGWT, that it causes the entire page to scroll out of view until the user hits the UP arrow again.

Upvotes: 0

Views: 2051

Answers (3)

Tamias
Tamias

Reputation: 311

This is working for me for a JEditorPane:

    @Override
    public void keyTyped(KeyEvent evt) {
    if (inTag() > -1) {
        evt.consume();
    }

The javadoc shows:

void java.awt.event.InputEvent.consume()

consume

public void consume()

Consumes this event so that it will not be processed in the default manner by the source which originated it.

The JEditorPane is in a JFrame which implements KeyListener and the JEditorPane does:

jEditorPaneSource.addKeyListener(this);

Upvotes: 1

Daniel Faro
Daniel Faro

Reputation: 327

I believe you can block it with this.

         Event.addNativePreviewHandler(new NativePreviewHandler(){
                @Override
                public void onPreviewNativeEvent(NativePreviewEvent event) {
               EventTarget eventTarget = event.getNativeEvent().getEventTarget();
            Element el = Element.as(eventTarget);
                       switch (event.getTypeInt()){
                       case Event.ONKEYPRESS:
                       case Event.ONKEYUP:
                       case Event.ONKEYDOWN:
                         if (el.getNodeName().equalsIgnoreCase("NODE FROM DATEPICKER")) {
                             if (event.getNativeEvent().getKeyCode() == "yourKeyCodeToBlock"
                                    event.cancel();
                             }
                         }
                         break;
                       }                   
                }
         });

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

datepicker.addKeyPressHandler(new com.smartgwt.client.widgets.events.KeyPressHandler() {
public void onKeyPress(
    com.smartgwt.client.widgets.events.KeyPressEvent event) {
        event.preventDefault();
    }
});

Upvotes: 0

Related Questions