Daniel Hári
Daniel Hári

Reputation: 7774

Capture pasted text in GWT Textbox

I want to handle paste event and catching pasted text of a TextBox in GWT. As I checked there are no standard handlers for paste events in TextBox.

Upvotes: 3

Views: 1220

Answers (1)

Daniel Hári
Daniel Hári

Reputation: 7774

I found out can do it with a little javascript native code added:

public class PasteAwareTextBox extends TextBox {

    public PasteAwareTextBox() {
        super();
        sinkEvents(Event.ONPASTE);
    }

    @Override
    public void onBrowserEvent(Event event) {
        super.onBrowserEvent(event);
        switch (event.getTypeInt()) {
        case Event.ONPASTE:
            onPasted(getClipboardData(event));
            break;
        }

    }

    private void onPasted(String clipboardData) {
        System.out.println("Pasted:" + clipboardData);
    }

    private static native String getClipboardData(Event event) /*-{
        return event.clipboardData.getData('text/plain'); 
    }-*/;

}

1) Subscribing for paste events.

2) Call event.clipboardData.getData('text/plain'); through JSNI.

Upvotes: 3

Related Questions