15

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

Kewitschka
  • 1,445
  • 1
  • 21
  • 35

3 Answers3

7

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

royB
  • 12,779
  • 15
  • 58
  • 80
  • 1
    Please note the following from the [doc](http://developer.android.com/reference/android/widget/TextView.html): `Key presses in software keyboards will generally NOT trigger this listener, although some may elect to do so in some situations. Do not rely on this to catch software key presses.` – user2340612 Mar 02 '16 at 10:59
  • software keyboard...he asked about hardware keyboard. Also the native software keyboard app does trigger the listener (as swiftkey) and it's the best possible solution – royB Mar 02 '16 at 11:21
2

You can use android:inputType="textCapSentences" in your editText, if you want to make the first letter, start with caps. There are other options also, if you want some different behaviour.

Amit Tiwari
  • 3,684
  • 6
  • 33
  • 75
1

I don't think there is any Android API which can detect state of capslock. But i do have its alternative. If you want to detect that whether CAPSLOCK is on or off, Its better to use TextWatcher for your EditText.

mEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String character = s.toString();
            /*
            Matche entered character with Rajex wheter its capital or small
             */
            if (Pattern.matches("[a-z]",character)) {
                Log.e("","CapsLock is OFF");
            }
            else if (Pattern.matches("[A-Z]",character)){
                Log.e("", "CapsLock is ON");
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
Anuj Sharma
  • 4,294
  • 2
  • 37
  • 53
  • For my problem it is necessary to know the caps lock state because I want to prevent 2 capital letters in a row EXCEPT caps lock is activated. – Kewitschka Mar 02 '16 at 10:32
  • @Kewitschka Sorry But i don't have other solution, You may wait for someone else to answer on your question. All the best, if you get solution do post it on this thread, It could help others. – Anuj Sharma Mar 02 '16 at 10:39