Sorry if this has been discussed anywhere at length - I searched, but wasn't sure what to look for... Also, apologies if it's in the wrong place.
Basically, I have a KeyListener on a JComponent. On the keyTyped event, a couple of tests happen, essentially checking the state of the JComponent, and in one case, a JTextField is displayed, with the focus, and the keyChar is placed in the JTextField, as the first character. Thus, just typing a word gives you an editable JTextField to keep typing into.
That all works correctly... except... when some system process does the 'typing', for example the hand-writing character recognition thing in Windows on a Tablet PC, or the 'front-end processor' used for typing Japanese (and other) characters. In this case, only the first character appears.
So, if I hand-write the word 'hello' in my Windows recognition thing, it seems to be sending through 5 key presses, all in one go. The keyTyped() method gets fired 5 times, but only the 'h' appears in my JTextField. I imagine that the other 4 events are happening before the JTextField has managed to get the focus, so they're just being lost.
The question is: does anyone have a good suggestion to avoid this? My best idea is building some kind of buffer of my own, that catches all the key presses, and in a separate thread grabs the next event after a short delay. That feels pretty flaky though - any better thoughts..?
You say the "h" appears in the text field. If events were happening before the text field got focus, I'd expect the text field to contain "o". The last letter, not the first. You are adding the letters in your code, so the focus on the component shouldn't matter. I suspect something like this is causing your trouble. While you are occupying the event processing thread, subsequent events are ignored. I'm going to move this to the Swing forum since it sounds like a event or threading problem. It would be nice to see some code. Can you provide us with some small test case? I don't have a TabletPC, and I bet few people here do. Does java.awt.Robot have the same behavior?
Thanks Joe, that makes a lot of sense. To be completely honest, something like that was bubbling away in the back of my head after I wrote the post, but I couldn't think of an easy way to articulate it. Also, I didn't want to suggest something that I had no idea about, but might influence the replies!
Working for a small company I now find myself looking at something entirely different, so this will have to wait a while, but your advice will be valuable when I get there!