Well, I'm ran into this problem while doing the tutorial. I applied
your solution, but it still doesn't work properly. When I press enter
in the text box, the error alert comes up, but only for a fraction of
second, after which it disappears. It works fine for the button click.
Here's my pertinent code. Any idea what I might be missing? Google
should really update their tutorial so it works. This type of thing
doesn't encourage people who are new to the product to continue to use
it.

    // Listen for keyboard events in the input box.
    newSymbolTextBox.addKeyDownHandler(new KeyDownHandler() {
      public void onKeyDown(KeyDownEvent event) {
        if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
{
          addStock();
        }
      }
    });

  }

  /**
   * Add stock to FlexTable. Executed when the user clicks the
addStockButton or
   * presses enter in the newSymbolTextBox.
   */
  private void addStock() {
    final String symbol =
newSymbolTextBox.getText().toUpperCase().trim();
    newSymbolTextBox.setFocus(true);

    // Stock code must be between 1 and 10 chars that are numbers,
letters, or dots.
    if (!symbol.matches("^[0-9A-Z\\.]{1,10}$")) {
      Window.alert("'" + symbol + "' is not a valid symbol.");
      newSymbolTextBox.selectAll();
      return;
    }

    newSymbolTextBox.setText("");

    // TODO Don't add the stock if it's already in the table.

    // TODO Add the stock to the table.

    // TODO Add a button to remove this stock from the table.

    // TODO Get the stock price.


  }

On Dec 3 2010, 12:36 pm, Brian Reilly <[email protected]> wrote:
> Greg,
>
> As I mentioned in a thread earlier today 
> (http://groups.google.com/group/google-web-toolkit/browse_thread/threa...),
> try using a KeyDownEvent handler and inspect
> event.getNativeEvent().getKeyCode()
> instead of event.getCharCode().
>
> From the other thread, it sounds like this behavior may have changed in 2.1,
> so it could be that the documentation is wrong, but only as of fairly
> recently.
>
> -Brian
>
> On Fri, Dec 3, 2010 at 3:21 PM, Greg Dougherty
> <[email protected]>wrote:
>
> > Ok, the tutorial says that to get a user pressing an enter key in a
> > TextBox you should write something like the following:
>
> >http://code.google.com/webtoolkit/doc/latest/tutorial/manageevents.html
>
> >        public void onKeyPress (KeyPressEvent event)
> >        {
> >                char    keyPress = event.getCharCode();
> >                int     keyCode = keyPress;
> >                if (keyPress == KeyCodes.KEY_ENTER)
> >                        goToRecord ();
> >        }
>
> > Unfortunately, when I do that, I get a keyCode of 0 for  Enter, Tab,
> > and Left Arrow (the keys I tested), while I get the actual key when I
> > type a number key.  What gives?  Is the tutorial wrong?  If so, what
> > should I be calling?
>
> > TIA,
>
> > Greg
>
> > --
> > 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