Listen to the Android keyboard to detect when a particular word is written

4

I would like to be able to obtain the keys that the user is clicking within an activity to know when he has written a specific word on the Android keyboard. So far I have used the onKeyDown method without much success:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    String key = KeyEvent.keyCodeToString(keyCode);
    word = word + key;
    Log.d("palabra",word);

    if (word.compareTo(coincidencia)){
        //Hacer algo cuando la palabra introducida coincida con la que espero
    }

    return super.onKeyDown(keyCode, event);
}

This code does not show anything in the Log, so I can not understand how this method really works.

I need this to work whenever something is entered on the keyboard, so it would not be worth comparing with what the user can enter in an input for example.

Any suggestions?

    
asked by Hechi 27.03.2017 в 18:00
source

1 answer

2

Try using "dispatchKeyEvent"

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
String key = KeyEvent.keyCodeToString(keyCode);
word = word + key;
Log.d("palabra",word);

if (word.compareTo(coincidencia)){
    //Hacer algo cuando la palabra introducida coincida con la que espero
}
    return super.dispatchKeyEvent(event);
}
    
answered by 31.03.2017 / 20:46
source