问题
I discovered a strange problem with Swing text input fields on OS X Yosemite (using Oracle JDK 8). When I hold down any letter or number key while a text input field has the focus, I can afterwards no longer enter anything in any field of the current JFrame. I then have to close the frame and reopen it to be able again to enter text.
The default behaviour in OS X is to bring up a small menu to choose special letters from when holding down a key for a longer time. I guess that the Swing inputs do have a problem with that.
Did anyone else also notice this, and is there a solution to this problem?
Edit: I've learned that it is possible to disable the character accent menu by setting the flag ApplePressAndHoldEnabled
to false
.
defaults write -g ApplePressAndHoldEnabled -bool false
However, I would like to solve this programmatically within Java, so that text input fields also work for other people using my software.
Thanks
回答1:
It seems to have been a problem with OS X itself. After a restart of my Mac, the problem is now gone.
回答2:
I have been having this exact issue for quite a while. I was frustrated that suddenly all input text fields in my JFrame would stop working. It was only today that I found it to be related the apple's popup for accented characters. For me the issue only happens when I long press one of the keys that has associated accented characters (for example: a, c, e, u, n, ...) while the focus in the frame is on one the components that doesn't have a text input (e.g. a button or a panel). Still looking for a proper solution to fix it programmatically within in the program itself, but here is a workaround I accidentally found:
Whenever the issue happens and the keys stop working, I first click on any "text field" in the frame to make it focused, then I hold any one of those keys with accent popup (e.g. "a" key), until the popup show. Then I just hit escape and from there after all the keys start working again.
Strangely, once I go through this once, the issue doesn't ever happen again on that frame until the next time the frame is closed and opened.
来源:https://stackoverflow.com/questions/32148508/text-input-fields-in-swing-stop-working-on-os-x-when-holding-keys-down