Raphi
Raphi

Reputation: 203

onKeyUp is not firing when I press most keyboard keys

In my Android application, I would like to perform some logic each time a key on the keyboard is pressed, for example, I want to evaluate if they typed a "magic phrase".

To do so, I overrode the method onKeyUp in my Activity and experimented to see if it was firing by making it fire a Toast.

 @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Toast toast = Toast.makeText(this, "Key Down", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 0);
        toast.show();
        return super.onKeyUp(keyCode, event);
    }

The problem is, the Toast is not showing when I press numbers or letters on the keyboard. It is only showing up when backspace is pressed. Why is this?

How do I get to work for all keys?

Upvotes: 0

Views: 140

Answers (1)

Raphi
Raphi

Reputation: 203

Ended up solving this by adding a TextChangedListener to my EditText field.

Here's my simple code, which just makes a little toast appear with the digit count:

            EditText passcodeInput = (EditText) findViewById(R.id.passcodeInput);
            passcodeInput.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    digitsPressed++;
                    Toast.makeText(Passcode.this, Integer.toString(digitsPressed), Toast.LENGTH_LONG).show();
                }
            });

Upvotes: 1

Related Questions