/** * Fired when a keyboard action generates a character. This occurs after onKeyDown and onKeyUp are * fired for the physical key that was pressed. * * <p>It should be noted that many browsers do not generate keypress events for non-printing * keyCode values, such as KEY_ENTER or arrow keys. These keyCodes can be reliably captured either * with onKeyDown(Widget, char, int) or onKeyUp(Widget, char, int). * * <p>Subclasses that override this method must call <tt>super.onKeyPress(sender, keyCode, * modifiers)</tt> to ensure that the Widget recieves its events. * * @param sender the widget that was focused when the event occurred. * @see com.google.gwt.user.client.ui.KeyboardListener */ public void onKeyPress(KeyPressEvent event) { Widget sender = (Widget) event.getSource(); char keyCode = event.getCharCode(); if (sender == tbHexColor) { // Disallow non-hex in hexadecimal boxes if ((!Character.isDigit(keyCode)) && (keyCode != 'A') && (keyCode != 'a') && (keyCode != 'B') && (keyCode != 'b') && (keyCode != 'C') && (keyCode != 'c') && (keyCode != 'D') && (keyCode != 'd') && (keyCode != 'E') && (keyCode != 'e') && (keyCode != 'F') && (keyCode != 'f') && (keyCode != (char) KeyCodes.KEY_TAB) && (keyCode != (char) KeyCodes.KEY_BACKSPACE) && (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 != (char) KeyCodes.KEY_RIGHT) && (keyCode != (char) KeyCodes.KEY_DOWN)) { ((TextBox) sender).cancelKey(); } } else { // Disallow non-numerics in numeric boxes if ((!Character.isDigit(keyCode)) && (keyCode != (char) KeyCodes.KEY_TAB) && (keyCode != (char) KeyCodes.KEY_BACKSPACE) && (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 != (char) KeyCodes.KEY_RIGHT) && (keyCode != (char) KeyCodes.KEY_DOWN)) { ((TextBox) sender).cancelKey(); } } }
public void onKeyPress(Widget sender, char keyCode, int modifiers) { if ((!Character.isDigit(keyCode)) && (!((keyCode == KEY_TAB) || (keyCode == KEY_ENTER) || (keyCode == KEY_BACKSPACE) || (keyCode == KEY_DELETE) || (keyCode == KEY_LEFT) || (keyCode == KEY_RIGHT) || (keyCode == KEY_UP) || (keyCode == KEY_DOWN) || (keyCode == KEY_HOME) || (keyCode == KEY_END) || (keyCode == '-')))) { TextBox textBox = (TextBox) sender; textBox.cancelKey(); } }
protected void allowDigitsOnly(KeyPressEvent event) { event.getNativeEvent().getKeyCode(); // This is for support for firefox for special characters like enter,tab,etc... if (event.getUnicodeCharCode() == 0) { return; } if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_BACKSPACE || event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER || event.getNativeEvent().getKeyCode() == KeyCodes.KEY_TAB || event.getNativeEvent().getKeyCode() == 46 /*dot symbol*/ || event.getUnicodeCharCode() == 46 /*firefox dot symbol*/ || event.getNativeEvent().getKeyCode() == 45 /*minus symbol*/ || event.getUnicodeCharCode() == 45 /*firefox minus symbol*/) { return; } if (!Character.isDigit(event.getCharCode())) { setMessage("invalid value"); textbox.cancelKey(); } else { clearMessage(); } }