Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Getting keycode from KeyUpEvent

    In evaluating SmartGWT for our company use I'm porting the demo GWT app that makes a round trip client/server call and displays the results in a dialog.

    In GWT the form submission has an event handler that looks at the integer keycode on the KeyUpEvent, but in SmartGWT, the only functionality appears to be getting the "name" of the key - per the Javadocs, "A" or "Enter."

    Several questions:

    is this entirely kosher?
    is the name of the key locale independent?
    what happens if I have a non-Latin keyboard or a letter with an accent mark?
    is there a "best practices" way of getting the key that was hit from the user?

    Thanks.

    #2
    You can use the utility methods EventHandler.getKeyEventCharacter() and EventHandler.getKeyEventCharaterValue() to return the actual character or numeric character code from the pressed key.

    The keyName paradigm is useful as a way of papering over native browser differences when you're interested in reacting to events on specific keys rather than looking at the value being typed by the user. There are some advantages to this:
    - not all keys generate character codes at all
    - not all browsers generate character codes for keyDown / keyUp events as well as keyPress
    - it can be useful to know whether a specific key ("M", for example) without having to deal with the fact that it may produce an upper or lowercase value (or some other value if combined with the ctrl key, etc)

    The keyNames are correct for a latin keyboard. For a keyboard with additional or different character keyset some keyNames will typically map to the key that produced an equivalent character, but in some cases the keyName may be unavailable in this case.
    Recommended approach depends on what you're trying to achieve. If you're listening for a particular key or key combo, such as Ctrl + A you would probably want to use keyNames. If you're interested in the character value that the user entered you should use the characterValue.

    Comment


      #3
      So, would this be the "correct" way to use this particular static utility function - assuming I needed to get the integer value of the <Enter> key?

      Code:
      public void onKeyUp(KeyUpEvent event) {
      
        int keyCode = EventHandler.getKeyEventCharacterValue();
        if (keyCode == 13) {
          // do something
        }
      }

      Comment


        #4
        Yes that looks correct.

        However if you're trying to react to an enter keypress by submitting a form you probably just want to set save on enter to true.
        http://www.smartclient.com/smartgwte...ang.Boolean%29

        Comment

        Working...
        X