4

I need to know when the user hits the Shift key (Soft Keyboard) in my EditText. currently, I'm using an addTextChangedListener, but it isn't called when the Shift key is pressed (though it is when any other key is pressed)

What can I do to know when the user hits the Shift key?

Thanks,

Mat

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
Digital Hazard
  • 137
  • 1
  • 3
  • 9

2 Answers2

5

You have to use View.OnKeyListener

edit1.setOnKeyListener(new View.OnKeyListener() {
 @Override
 public boolean onKey(View v, int keyCode, KeyEvent event) {
    if(event.isShiftPressed())
      {

       }
    return false;
  }
});
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • Thanks so much for your help. Does this work with the soft keyboard? It's not working for me :-/ – Digital Hazard Oct 13 '11 at 13:56
  • @DigitalHazard - I'm not aware of the situation you have. Take a look at this thread - http://stackoverflow.com/questions/4282214/onkeylistener-not-working-on-virtual-keyboard – KV Prajapati Oct 13 '11 at 14:09
4

the example below can help you

//while your activity class should be implementing OnKeyListener like below
//public class MyActivity extends Activity implements OnKeyListener { ....}
myEditText.setOnKeyListener(this);

then by overriding the method of key listener you can sense the shift key

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    switch (v.getId()) 
    {
    case R.id.myEditTextId:
    if(keyCode==59)//59 is shift's keycode
    //do your stuff here against pressing shift key
    break;
    }
    }

you can get android key code list here http://qdevarena.blogspot.com/2009/04/android-keycodes-list.html

Umar Qureshi
  • 5,985
  • 2
  • 30
  • 40