DELETE key and dot key.

399 views
Skip to first unread message

David Ojeda

unread,
Jul 16, 2008, 6:19:36 PM7/16/08
to Google Web Toolkit
Hello,

I have a problem with a KeyListener, it seems that the delete key and
the dot key (.) have the same code... Is there a way to permit the
delete key but not the dot key in a textbox?
Here's a snip:

textBoxGSM.addKeyboardListener(new
KeyboardListenerAdapter(){
public void onKeyPress(Widget sender, char
keyCode, int modifiers) {
if(!Character.isDigit(keyCode) &&
keyCode != (char)KEY_BACKSPACE && keyCode != (char)KEY_ENTER &&
keyCode != (char) KEY_DELETE)
textBoxGSM.cancelKey();
}
});

Thanks a lot!

Thomas Broyer

unread,
Jul 17, 2008, 5:34:04 AM7/17/08
to Google Web Toolkit
The general rule is that keyPress is for "printable chars", while
keyDown and keyUp also receive "other keys", so your KEY_* chacks
should belong to onKeyDown or onKeyUp.
This however is not that simple: http://www.quirksmode.org/js/keys.html
GWT uses "event.which || event.keyCode || -1" in all cases, and this
might be your problem here!
http://developer.mozilla.org/en/docs/DOM:event.which

IIUC, event.which will be the charCode if the key maps to a character
(and only in keyPress), or keyCode otherwise. So checking for the
KEY_* in onKeyDown or onKeyUp should catch the DELETE key.
But it might not solve your problem, as some browsers (firefox?) will
pass the KEY_DELETE to keyPress, in which case event.which will equal
the event.keyCode (46 here, which happens to also be the
event.charCode for a dot). In brief: you could allow "." but disallow
DELETE easily, but not the other way around (I'd be happy to be wrong
here!)

I'd have guessed onKeyPressed is passed the charCode (with a fall back
to keyCode for IE) and onKeyDown/Up the keyCode, but it's not the
case...

If I were you, I'd try using DOM.eventGetCurrentEvent() and JSNI to
access charCode and keyCode; and eventually fill in an issue for
onKeyPressed to receive charCode instead of keyCode; or at least try
to behave (almost) consistently across browsers, and if possible
follow the DOM.

David Ojeda

unread,
Jul 21, 2008, 5:22:07 PM7/21/08
to Google Web Toolkit
Excellent response Thomas... I will try that and post back...
I am looking forward to allow DELETE but not ".", so let me see what
can I do
Thank you

On Jul 18, 4:34 am, Thomas Broyer <t.bro...@gmail.com> wrote:
> On 17 juil, 00:19,David Ojeda<david.oj...@gmail.com> wrote:
>
>
>
> > Hello,
>
> > I have a problem with a KeyListener, it seems that the delete key and
> > the dot key (.) have the same code... Is there a way to permit the
> > delete key but not the dot key in a textbox?
> > Here's a snip:
>
> >                 textBoxGSM.addKeyboardListener(new
> > KeyboardListenerAdapter(){
> >                         public void onKeyPress(Widget sender, char
> > keyCode, int modifiers) {
> >                                 if(!Character.isDigit(keyCode) &&
> > keyCode != (char)KEY_BACKSPACE && keyCode != (char)KEY_ENTER &&
> > keyCode != (char) KEY_DELETE)
> >                                         textBoxGSM.cancelKey();
> >                         }
> >                 });
>
> > Thanks a lot!
>
> The general rule is that keyPress is for "printable chars", while
> keyDown and keyUp also receive "other keys", so your KEY_* chacks
> should belong to onKeyDown or onKeyUp.
> This however is not that simple:http://www.quirksmode.org/js/keys.html
> GWT uses "event.which || event.keyCode || -1" in all cases, and this
> might be your problem here!http://developer.mozilla.org/en/docs/DOM:event.which
Reply all
Reply to author
Forward
0 new messages