Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect character key presses?

I know how to listen for when the ENTER button is pressed in a TextView, as shown in the code below:

textView.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
            if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                enterPressed();
                return true;
            }
            return false;
        }
});

However... how do I listen for when a character key (A-Z, 0-9, special characters, etc.), basically everything else other than ENTER, BACKSPACE, or SPACE, are pressed? I want to do this because I want a button to become enabled when the user has started typing text into a TextView. Strangely, the onKey() method isn't even called when these character keys are pressed, so is there another way I'm suppose to listen for them? Thanks in advance!

like image 685
Brian Avatar asked Jun 29 '11 04:06

Brian


People also ask

How can you tell if someone pressed a key?

To detect keypress, we will use the is_pressed() function defined in the keyboard module. The is_pressed() takes a character as input and returns True if the key with the same character is pressed on the keyboard.

How do I know what's pressing on my keyboard?

Go to Start , then select Settings > Accessibility > Keyboard, and turn on the On-Screen Keyboard toggle. A keyboard that can be used to move around the screen and enter text will appear on the screen.

How do you unlock characters in Keydown event?

keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. Using jQuery e. which you can get the key code and using String. fromCharCode you can get the specific character that was pressed (including shiftKey ).


2 Answers

Text watcher might help you

textView.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }

});
like image 126
MGK Avatar answered Sep 20 '22 23:09

MGK


"Key presses in software keyboards will generally NOT trigger this method, although some may elect to do so in some situations. Do not assume a software input method has to be key-based; even if it is, it may use key presses in a different way than you expect, so there is no way to reliably catch soft input key presses."

http://developer.android.com/reference/android/view/View.OnKeyListener.html

like image 40
Xarph Avatar answered Sep 16 '22 23:09

Xarph