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?
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.