tags:

views:

574

answers:

3

I am catching keyboard events/presses using the onKey method:

public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
    //do something
    return false;
}

This fires off just fine for physical keyboard presses but it does not fire on virtual keyboard presses. Is there an event handler to handle virtual keyboard presses?

+3  A: 

If it's an EditText, see if you can use a TextChangedListener instead.

myEditText.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //do stuff
        }

    });
James
that did the trick! i'll post the code below since it doesn't format well in here, thanks!
+1  A: 

Virtual keypresses are delivered directly to the selected view, they don't propagate through parent views like hardware keypresses. Are you overriding onKey on something other than the EditText/List/Whatever that's getting keypresses? (the thing you click on to get the virtual keyboard)

I am overriding onKey on my activity that implements onClickListener:public class Foo implements OnClickListener{..public void onKey(View arg0, int arg1, KeyEvent arg2){ if(arg0 == searchBox){ //do something }}..}where searchBox is an EditText
A: 
myEditText.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //do stuff
        }

    });