I did my custom pin-code view
class StarsPasswordView : LinearLayout {
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, at
Depending on the keyboard, not all keys are sent through the InputConnection
. Some are sent like hard keys events and must be processed separately. This includes the number pad numbers on the standard keyboard. To the best of my current knowledge, using the OnKeyListener
like you did is the way to go (but see @pskink's comments here).
You can separate the text characters from the control characters with KeyEvent.getUnicodeChar()
as is shown below.
if (keyEvent.getUnicodeChar() != 0) {
// unicode text
char unicodeChar = (char) keyEvent.getUnicodeChar();
} else {
// control char
}
Handle the control codes that you need, but all the valid Unicode characters (including numbers and the new line (enter) character) will be caught.
I cover some aspects of this in more detail in the question and answers I wrote while preparing to answer this question.
I also updated my previous answer about custom views receiving keyboard input.