If anyone else is looking for this, I've added more error checking
that might come in handy. The error checking presumes that zero is not
a valid entry.
You could combine the if statements. I've posted them this way for
conprehension.
//class vars
public static final int QTY_BOX_LENGTH = 2;
public String previousValue;
//use a focus and a keyboard listener
//If we lose focus with a zero value, reset
public void onLostFocus(Widget sender) {
if ( ((TextBoxBase) sender).getText().length() == 0 || ((TextBoxBase)
sender).getText().equals("0") )
{
((TextBoxBase) sender).setText(this.previousValue);
}
}
public void onKeyDown(Widget sender, char keyCode, int modifiers) {
switch(keyCode)
{
case KEY_LEFT:
case KEY_RIGHT:
case KEY_BACKSPACE:
case KEY_DELETE:
return;
}
if ( !Character.isDigit(keyCode) )
{
((TextBoxBase) sender).cancelKey();
return;
}
//entry past the max length specified is prohibited
if ( ((TextBoxBase) sender).getText().length() >= QTY_BOX_LENGTH )
{
((TextBoxBase) sender).cancelKey();
return;
}
//if a user enters zero as the first character, cancel the key
if ( ( (((TextBoxBase) sender).getText().length() == 0) && (keyCode
== '0') ) )
{
((TextBoxBase) sender).cancelKey();
return;
}
}
public void onKeyUp(Widget sender, char keyCode, int modifiers)
{
//If the user enters zero, ahead of an existing value, reset to the
existing value
String s = ((TextBoxBase) sender).getText();
if ( s.substring(0,1).equals("0") )
{
((TextBoxBase) sender).setText(s.substring(1));
return;
}
//reset the previousValue var or do whatever is next...
foo();
}
On Nov 6, 1:05 am, Schimki86 <[EMAIL PROTECTED]> wrote:
> I use a KeyboardListener to check the typed character. I've seen this
> example in the book "GWT in Action":
>
> public void onKeyUp(Widget sender, char keyCode, int modifiers) {
> if (!Character.isDigit(keyCode)) {
> ((TextBox)sender).cancelKey();
> return;
> }
>
> }
>
> In IE it works but in FF the Listeners is listen to the control-,
> delete- and backspace- key too. I can't delete an character because
> these keys are no digits.
>
> How can I check if the keys are a control, delete or the backspace?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---