Intercepting a paste from the clipboard

915 views
Skip to first unread message

Catorcio

unread,
Jul 21, 2010, 11:08:12 AM7/21/10
to Google Web Toolkit
I have a TextArea widget and I need to be able to discern when the
user pastes text in it from the clipboard (instead of typing at the
keyboard). I would like to be able to block the paste action, get the
text to be pasted and simulate that the user has typed at the keyboard
that text (i.e. I would like to simulate KeyPressEvents because I have
already in place a KeyPressHandler that does the processing I need ).
Any idea? Thanks.

Jim Douglas

unread,
Jul 22, 2010, 11:28:55 AM7/22/10
to Google Web Toolkit
You can't do this in any reliable cross-browser way. The following
code works only in IE and WebKit. It fails in Firefox because there's
no reliable way to retrieve the pasted text in Firefox (there are
unreliable ways involving the Mozilla security manager). It fails in
Opera and Mobile Safari (iOS) because they don't fire the ONPASTE
event.

But if you want to give it a shot, the basic strategy would be:

(1) Subclass the TextArea.

(2) Do this:

sinkEvents(Event.ONPASTE);

(3) Add this:

public static native String getClipboardData(Event event)
/*-{
var text = "";

// This should eventually work in Firefox:
// https://bugzilla.mozilla.org/show_bug.cgi?id=407983
if (event.clipboardData) // WebKit (Chrome/Safari)
{
try
{
text = event.clipboardData.getData("Text");
return text;
}
catch (e)
{
// Hmm, that didn't work.
}
}

if ($wnd.clipboardData) // IE
{
try
{
text = $wnd.clipboardData.getData("Text");
return text;
}
catch (e)
{
// Hmm, that didn't work.
}
}

return text;
}-*/;

(4) Add this:

@Override
public void onBrowserEvent(Event event)
{
super.onBrowserEvent(event);
switch (event.getTypeInt())
{
case Event.ONPASTE:
{
event.preventDefault();
String text = getClipboardData(event);
for (int i = 0; i < text.length(); ++i)
{
doCharacter(text.charAt(i)); // this is your code
to process the character
}
break;
}
}
}


http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/ui/Widget.html#sinkEvents(int)
http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/Event.html#ONPASTE
http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/ui/Widget.html#onBrowserEvent(com.google.gwt.user.client.Event)

cokol

unread,
Jul 22, 2010, 12:51:04 PM7/22/10
to Google Web Toolkit
why do u want to do that?if a user pastes text via context menu u
would not get it anyway. I would go for this:let paste the text and in
ur onchange handler u would trigger a scan for inserted text and
simulate the keyevents for each charachter entered.but again please
tell us ur idea :)

On Jul 22, 5:28 pm, Jim Douglas <jdoug...@basis.com> wrote:
> You can't do this in any reliable cross-browser way.  The following
> code works only in IE and WebKit.  It fails in Firefox because there's
> no reliable way to retrieve the pasted text in Firefox (there are
> unreliable ways involving the Mozilla security manager).  It fails in
> Opera and Mobile Safari (iOS) because they don't fire the ONPASTE
> event.
>
> But if you want to give it a shot, the basic strategy would be:
>
> (1) Subclass the TextArea.
>
> (2) Do this:
>
>         sinkEvents(Event.ONPASTE);
>
> (3) Add this:
>
>     public static native String getClipboardData(Event event)
>     /*-{
>         var text = "";
>
>         // This should eventually work in Firefox:
>         //https://bugzilla.mozilla.org/show_bug.cgi?id=407983
> http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/g...)http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/g...http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/g...)

Catorcio

unread,
Jul 26, 2010, 7:07:49 AM7/26/10
to Google Web Toolkit
The code by Jim Douglas does exactly what I need. OnPaste is triggered
also for pastes via the context menu.

OnChange is not good for me, because I need to recognize as soon as
the user has typed a word, so I need to track live the charaters being
entered (entered maybe through a paste).

Thanks a lot!

Reply all
Reply to author
Forward
0 new messages