Here is a function that I made make a textbox a number only textbox:

    public static void applyNumericMask(TextBox textBox, final boolean
allowDecminal)
    {
        textBox.addKeyDownHandler(new KeyDownHandler(){
            public void onKeyDown(KeyDownEvent keyDownEvent) {
                int keyCode = keyDownEvent.getNativeKeyCode();
                if ((!((keyCode > 46 && keyCode < 58)) && !((keyCode > 96 &&
keyCode < 108))) && (keyCode != (char) KeyCodes.KEY_TAB)
                    && (keyCode != (char) KeyCodes.KEY_BACKSPACE)
                    && (keyCode != (char) KeyCodes.KEY_ESCAPE)
                    && (keyCode != (char) KeyCodes.KEY_DELETE) && (keyCode
!= (char) KeyCodes.KEY_ENTER)
                    && (keyCode != (char) KeyCodes.KEY_HOME) && (keyCode !=
(char) KeyCodes.KEY_END)
                    && (keyCode != (char) KeyCodes.KEY_LEFT) && (keyCode !=
(char) KeyCodes.KEY_UP) && (keyCode != 190 || !allowDecminal) && (keyCode !=
110 || !allowDecminal)
                    && (keyCode != (char) KeyCodes.KEY_RIGHT) && (keyCode !=
(char) KeyCodes.KEY_DOWN)) {

                    keyDownEvent.preventDefault();
                    keyDownEvent.stopPropagation();
                }
            }
        });

        textBox.addKeyUpHandler(new KeyUpHandler(){
            public void onKeyUp(KeyUpEvent keyUpEvent) {
                int keyCode = keyUpEvent.getNativeKeyCode();
                if ((!((keyCode > 46 && keyCode < 58)) && !((keyCode > 96 &&
keyCode < 108))) && (keyCode != (char) KeyCodes.KEY_TAB)
                    && (keyCode != (char) KeyCodes.KEY_BACKSPACE)
                    && (keyCode != (char) KeyCodes.KEY_ESCAPE)
                    && (keyCode != (char) KeyCodes.KEY_DELETE) && (keyCode
!= (char) KeyCodes.KEY_ENTER)
                    && (keyCode != (char) KeyCodes.KEY_HOME) && (keyCode !=
(char) KeyCodes.KEY_END)
                    && (keyCode != (char) KeyCodes.KEY_LEFT) && (keyCode !=
(char) KeyCodes.KEY_UP) && (keyCode != 190 || !allowDecminal) && (keyCode !=
110 || !allowDecminal)
                    && (keyCode != (char) KeyCodes.KEY_RIGHT) && (keyCode !=
(char) KeyCodes.KEY_DOWN)) {

                    keyUpEvent.preventDefault();
                    keyUpEvent.stopPropagation();
                }
            }
        });
    }

You can actually merge the two if statements, but I was just really lazy.
 This also blocks copy/paste.  There are probably better ways to do this,
but this has been working for me.

On Wed, Apr 28, 2010 at 2:29 PM, Graham J <[email protected]> wrote:

> I had some input checking going on with the old KeyListener interface
> that would cancelKey() the event if the input was not a digit.
>
> I'm trying to convert that over to use a KeyDownHandler, but I can't
> figure out how to check if they key is a digit.
>
> This is the closest I've got:
>
> public void onKeyDown(KeyDownEvent event)
> {
>        int keyCode = event.getNativeKeyCode();
>        if (!Character.isDigit(keyCode)
>        {
>                // TextBox.cancelKey() suppresses the current keyboard
> event.
>                ((TextBox) event.getSource()).cancelKey();
>        }
> }
>
> The catch is, Character.isDigit(int) is not emulated in GWT -- it only
> accepts a char parameter.
>
> So, how do I convert the keyCode to a char, or, check isDigit() on the
> keyCode? Or an entirely different method? IS getNativeKeyCode() the
> right approach for this?
>
> --
> 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]<google-web-toolkit%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
>
>

-- 
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.

Reply via email to