dc480506
dc480506

Reputation: 21

How to know if the typed alphabet is uppercase in Android Studio?

The keyCode returned is between 29 and 54 for alphabets which doesn't account for uppercase. I even tried the isCapsLockOn() method to distinguish between the uppercase and lowercase alphabets but no success.

public boolean onKeyDown(int keyCode, KeyEvent event) {
        try {
            if (keyCode >= 29 && keyCode <= 54) {
                if (event.isCapsLockOn()) {
                    dos.writeInt(keyCode + 36);
                    dos.flush();
                }else{
                    dos.writeInt(keyCode+68);
                    dos.flush();
                }
            }else if(keyCode>=7 && keyCode<=16){
                dos.writeInt(keyCode+41);
                dos.flush();
            }
            else {
                switch (keyCode) {
                    case 62: //space
                        dos.writeInt(32);
                        break;
                    case 66:// enter
                        dos.writeInt(10);
                        break;
                    default:
                     break;
                }
            }
        }catch (Exception e){

        }
        return super.onKeyDown(keyCode, event);
        }

Please tell me if there is a way to identify if the alphabet is uppercase or not !! Thanks in advance!!

Upvotes: 0

Views: 513

Answers (2)

dc480506
dc480506

Reputation: 21

So i got to work around this by using getUnicodeChar() method. I will post my solution in case it helps someone in future.

public boolean onKeyDown(int keyCode, KeyEvent event) {
        try {
            Log.d(TAG,"Unicode: "+event.getUnicodeChar());
            if(keyCode==67){
                dos.writeInt(8);// Special case since backspace returns unicode 0 in android
                dos.flush();
            }else if(keyCode!=59){ // Also ignoring shift key if it is pressed
                dos.writeInt(event.getUnicodeChar());
                dos.flush();
            }
        }catch (Exception e){

        }
        return super.onKeyDown(keyCode, event);
        }

Upvotes: 0

Jorn Rigter
Jorn Rigter

Reputation: 1215

You could simply use

public boolean onKeyDown(int keyCode, KeyEvent event) {
    return Character.isUpperCase(event.getKeyChar());
}

which will return true if the char is in uppercase.

See this link for an explanation of isUpperCase(char c) and this link for getting the char of a KeyEvent.

Upvotes: 3

Related Questions