Question

I've been writing an Android app that is mainly driven by hardware keyboard input. I'm in the process of adding soft keyboard support, but I'm not quite there yet. But I digress...

Originally I was checking the integer keyCode value passed in to my OnKey function against the enums in the KeyEvent class. Simple enough...

But I need to know if the user pressed the combination of keys that would generate the > symbol. I therefore checked if both ALT was pressed and the X keyCode was passed in...but it turns out that different keys on different phones generate the > symbol.

Now, instead, I'm checking the value of the getUnicodeChar method of KeyEvent against the known values for the Unicode characters.

My problem is solved - but is there an accepted way to do character checking? Do most people just not need the ALT-symbols, or are most people using software keyboards that are standard across devices? There's no built-in table of Unicode character enums (that I know of, anyhow), so that makes the code a little uglier too. Just curious!

Was it helpful?

Solution

I believe you are correct in that the only way to detect which symbol/alternative key has been pressed is to look at the unicode character.

Often, people are only interested in the primary keys, so the KeyCode is sufficient.

You can't depend on any particular IME (soft keyboard) being used. Note also that text entered through an IME is accessed by the text widgets via the InputConnection, so it often does not use KeyEvents like the hardware keyboards do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top