Дата публикации: 06.03.2019 16:01:54
At JetBrains we decided to migrate all our IDEs from JDK 8 to JDK 11, and discovered a serious problem on Mac. After pressing the right Alt key, the left one stops working. Let's try to solve it.
I quite quickly discovered the source of the problem, and I was shocked. The main cause was that the author added the altGRPressed field, which changed its value from NO to YES on pressing the right Alt key. And this field remained in that state forever! In addition, the nested if-statement made it difficult to analyze the code, since its formatting did not correspond to the braces location. Look at these changes. Can you understand which if-statement the else-statement is related to?
Before fixing anything we must prepare a test. My manual test can be downloaded from here. It can be easily configured for a specific platform, since Mac does not have an AltGr key, and Windows does not have a Command key. When using the test, I discovered the inconsistent behaviour of the Caps Lock key on different platforms. Also, I was very surprised that Windows repeats PRESSED events automatically while a modifier key is pressed. However, this topic is for future consideration.
First, I launched my test on Apple JDK 6. The results can be found here. Basically, its behaviour was predictable, except that pressing/releasing the second key with the same modifier did not send any events.
Then, I launched my test on Oracle JDK 8u201 (results). Its behaviour was almost the same, except that pressing/releasing the second key with the same modifier sent a PRESSED event with the key code 0. This event made no sense to listeners and only polluted the Event Dispatch Thread.
After that, I launched my test on Oracle JDK 11.0.2 (results). Pressing the right Alt key sent a PRESSED event with the unexpected code VK_ALT_GRAPH. After this event, pressing/releasing of any Alt key sent unexpected events.
For a start, I made a simple changes, similar to the changes proposed by Oracle today. They fixed a critical issue that caused me to look into this code. Then I slightly modified the existing code to fix the recognition of different keys with the same modifier. This patch was not the best solution, but just a minimal change.
When I launched my test on JetBrains JDK with my changes, I got these results. There was a predictable fail, since I had not removed support for the AltGr key. Due to the fact that Mac users did not expect such behaviour, I added an ability to turn it off. You can set the RIGHT_ALT_AS_ALT_GRAPH to NO and rebuild JDK. The results of launching my test on such JDK can be found here.