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...
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...
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);
}
}
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.
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) {
}
});