FXMisc / RichTextFX

Rich-text area for JavaFX
BSD 2-Clause "Simplified" License
1.21k stars 236 forks source link

AltGr doesn't work #89

Closed ReneHollander closed 10 years ago

ReneHollander commented 10 years ago

The AltGr Key to modify a keys behaviour doesn't print any characters. For example 8+AltGr should lead to a [ printed, but it does just nothing. The AltGr Key is used on German keyboards. I don't know about the other layouts out there.

Edit: Using RichTextFX 0.5 on JDK 8 Update 20

TomasMikula commented 10 years ago

Hi Rene, does this work with a regular TextArea from JavaFX?

ReneHollander commented 10 years ago

Hey Tomas! It works in a normal TextArea from JavaFX without a problem.

TomasMikula commented 10 years ago

Hi Rene,

I just tested it and it works for me in the provided RichTextFX demos (e.g. JavaKeywords), using JDK 8u20 on Linux. What platform are you on?

ReneHollander commented 10 years ago

I tried it on Windows and Linux, both have the german keyboard layout enabled. On the Windows machine it doesn't work, on Linux I don't have any problems with the demo.

ReneHollander commented 10 years ago

I did some testing. If I try to type the [ key on a simple JavaFX TextArea, this event is getting raised:

KeyEvent [source = TextArea@3f5157ee[styleClass=root text-input text-area], target = TextArea@3f5157ee[styleClass=root text-input text-area], eventType = KEY_PRESSED, consumed = false, character =  , text = 8, code = DIGIT8, controlDown, altDown, shortcutDown]

As you can see control, alt and shortcut is beeing pressed. In StyledTextAreaBehavior on line 133-136 you check if either control, alt or meta (I think it's shortcut) is pressed, if one of them is, the character is not getting appended to the TextArea. I think windows is a bit wierd here, so I just removed the checks and I was able to type for e.g the [ key.

I hope I was able to help identifying the cause of the problem. Looking forward to a fix :)

TomasMikula commented 10 years ago

Hi Rene, sorry for the delay. I cannot test it on Windows right now. The event you report is of type KEY_PRESSED, however, character input in StyledTextArea (and I believe in TextArea as well) is handled using KEY_TYPED events. Can you please check what KEY_TYPED event is produced on TextArea or StyledTextArea (they should be the same)?

I think you correctly identified where the event is blocked. However, your suggested fix (removing the checks for control keys) would mean that, for example, Ctrl+Z would produce the character 'Z' in addition to "Undoing" the last action, which is not desirable.

Anyway, if it works correctly in TextArea, we can make it work in RichTextFX, we just have to figure out how.

ReneHollander commented 10 years ago

I checked the KeyTyped Event:

KeyEvent [source = CodeArea@3f0f5198[styleClass=styled-text-area code-area], target = CodeArea@3f0f5198[styleClass=styled-text-area code-area], eventType = KEY_TYPED, consumed = false, character = [, text = , code = UNDEFINED, controlDown, altDown, shortcutDown]

I tried out some key combinations like Ctrl+Z and did not had a problem with writing for example a Z. The fix was more like a quick and dirty lets see if it works kind off fix :D. If you need any more testing, I will gladly help you!

TomasMikula commented 10 years ago

You are right, after removing the checks, Ctrl+Z does not produce 'Z'—it is prevented at this line

&& isLegal(e.getCharacter())

because Ctrl+Z produces KEY_TYPED event with the character 0x1A, which is a control character and is filtered out by the isLegal method.

However, Shift+Ctrl+Z does type 'Z' in addition to "Redo" (the KEY_TYPED event that is produced does contain the character 'Z').

ReneHollander commented 10 years ago

Interesting. I also tried the Ctrl+Shift+Z key combo to redo but I did not get an extra Z and isLegal returned false.

TomasMikula commented 10 years ago

So there really is a difference in what events and characters are produced between Windows and Linux. It doesn't seem to be resolvable in a platform independent manner.

This is the relevant code in JavaFX to handle this (copied from TextInputControlBehavior.java):

// Filter out control keys except control+Alt on PC or Alt on Mac
if (event.isControlDown() || event.isAltDown() || (isMac() && event.isMetaDown())) {
    if (!((event.isControlDown() || isMac()) && event.isAltDown())) return;
}
TomasMikula commented 10 years ago

Can you please sanity test the behavior of my latest commit on Windows?

ReneHollander commented 10 years ago

It seems to work now! Thanks!

TomasMikula commented 10 years ago

Thanks for your help. I made a 0.5.1 release that includes this fix.