Search code examples
androidcapslock

Get caps lock state in Android


How can I get the caps lock state in Android using a hardware keyboard? In pure Java it can be detected with

boolean isOn = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);

But this does not work with Android...


Solution

  • Try This (didn't test it):

    public class CustomEditText extends EditText{
    
    
    public CustomEditText(Context context) {
        super(context);
    }
    
    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.isCapsLockOn()){
            //Do what Ever
        }
        return super.onKeyDown(keyCode, event);
    }
    

    }

    isCapsLockOn