Friday, January 28, 2011

Re: Getting a user Enter key

Interesting... maybe the alert box is getting the key press event,
which isn't being fired until after the alert has popped up because
that's happening on key down. Maybe using
event.getNativeEvent().getKeyCode() in a KeyPressHandler will work.
I'm not sure because I haven't run into this case myself.

The workaround link that David Chandler posted is to issue #5558 in
the GWT issue tracker:

http://code.google.com/p/google-web-toolkit/issues/detail?id=5558

In particular, Thomas Broyer posted what looks like some nice code for
reliably detecting the enter key:

http://code.google.com/p/google-web-toolkit/issues/detail?id=5558#c6

-Brian

On Thu, Jan 27, 2011 at 11:27 PM, Michael Phoenix
<michaelandrewphoenix@gmail.com> wrote:
> 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 <brian.irei...@gmail.com> 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
>> <dougherty.greg...@mayo.edu>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 google-web-toolkit@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > google-web-toolkit+unsubscribe@googlegroups.com<google-web-toolkit%2Bunsubscribe@googlegroups.com>
>> > .
>> > 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 google-web-toolkit@googlegroups.com.
> To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
> 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 google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment