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)
On Jul 21, 8:08 am, Catorcio <[email protected]> wrote:
> 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.
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.