2

This is in continuation of my other question. I am checking if a valid character or number is being pressed

Valid Characters - A to Z and a-z, these characters can be entered using "SHIFT+A=a" and vice-versa "SHIFT+a=A". I am restricting the user to enter other than valid characters

Invalid Characters - "SHIFT+1=!" to "SHIFT+0=)"

Heres a code snippet, which I tried but not sure how to get keyCode of "SHIFT+...."

    @Override
    public void onBrowserEvent(Context context, Element parent, String value,
                NativeEvent event, ValueUpdater<String> vUpdater){

    if (event.getShiftKey()) {
                int code = event.getKeyCode();
    //only a-z and A-Z are allowed if shift key is pressed
    if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
        validShiftKeyPressed = true;
    } else {
        validShiftKeyPressed = false;
    }    
   }

     if (validShiftKeyPressed && 
              (event.getKeyCode()>=48 && event.getKeyCode()<=57)){
       \\do some operation
      }

int code = event.getKeyCode();

The value of the code will always be 16 and validShiftKeyPressed will always be false.

I want to check the value of SHIFT+A or SHIFT+1 or any other combination is pressed. Is there any way this is possible?

Community
  • 1
  • 1
S Jagdeesh
  • 1,523
  • 2
  • 28
  • 47

1 Answers1

1

This isn't exactly an answer to your exact question but I am not sure the path you are on is going to give you what you want. If I am wrong then simply ignore this answer.

I use a variation of the below code to prevent non-numeric user input but still allow the user to move around and edit the field. I added the "Character.isLetter(c)" to this snippet to also allow letters (upper or lower). GWT emulation class states that it only handles ASCII chars. You can find the emulated class in "/gwt-user/com/google/gwt/emul/java/lang/Character" in the gwt-user.jar to look at what it is doing in javascript-land.

Be aware that this type of code in isolation doesn't encompass a full input constraint and validation solution for your users. For example, it doesn't prevent the user from pasting in whatever they want into the field. I typically try to do a full validation of the page prior to saving to make sure that the final input of my fields is valid. I use the GWT Validation feature (bean validation) to do this. This catches any input outages that I wasn't able to prevent from code like this.

protected void handleKeyPress(KeyPressEvent event) {

    // get the char code
    char charCode = event.getCharCode();
    if (charCode == '\u0000') {
        /*
         * On some browsers the charcode does not exist in the keypress
         * event. In this case we switch over to the keycode.
         */
        charCode = (char)event.getNativeEvent().getKeyCode();
    }

    // prevent input other than [a-z|A-Z|0-9] but still allow basic navigation and editing keys
    if ((!Character.isDigit(charCode)) && (!Character.isLetter(charCode)) &&
            (charCode != (char)KeyCodes.KEY_TAB) &&
            (charCode != (char)KeyCodes.KEY_BACKSPACE) &&
            (charCode != (char)KeyCodes.KEY_ENTER) &&
            (charCode != (char)KeyCodes.KEY_HOME) &&
            (charCode != (char)KeyCodes.KEY_END) &&
            (charCode != (char)KeyCodes.KEY_LEFT) &&
            (charCode != (char)KeyCodes.KEY_UP) &&
            (charCode != (char)KeyCodes.KEY_RIGHT) &&
            (charCode != (char)KeyCodes.KEY_DOWN)) {
        event.preventDefault();
    }
}
xsee
  • 1,173
  • 1
  • 9
  • 12