Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SNAPSHOT] Regression: setting key pressed event handler results in silently discarding all key events with non-printable codes #257

Closed
alt-grr opened this issue Feb 19, 2016 · 10 comments

Comments

@alt-grr
Copy link

alt-grr commented Feb 19, 2016

StyleClassedTextArea textArea = new StyleClassedTextArea();
textArea.setOnKeyPressed(event -> {});

After this, only letters, symbols, new lines can be entered in text field. You can't use backspace, delete, arrow keys etc.

This worked correctly in last stable version 0.6.10.

@JordanMartinez
Copy link
Contributor

Oh! Ok. Yeah, that makes sense now 😄

If you look at StyledTextAreaBehavior's EventHandlerTemplate code, and the javadoc in StyledTextArea, you'll see that you're supposed to override the default event handlers in a special way.

I'm not sure why that worked for 0.6.10 though as it uses the same code...

@alt-grr
Copy link
Author

alt-grr commented Feb 19, 2016

Ohhh, thanks! I totally missed that, because I used StyledTextArea as drop-in replacement for TextArea. I will check if that helps.

@TomasMikula
Copy link
Member

It worked in 0.6.10 because StyledTextAreaBehavior registered the event handlers during skin instantiation, which happens when the control is first displayed, which is typically after the user registers his or her handlers. Now that we don't have skin, StyledTextArea's handlers are registered when StyledTextArea is created, and then your setOnKeyPressed() call overrides the handler that StyledTextArea registered. (Printable characters keep working for you because they are handled via the onKeyTyped handler.)

What @JordanMartinez suggests to do will fix your problem. There is also a newer version of WellBehavedFX (package experimental) that uses yet another way of registering handlers that does not use the onKeyPressed, ... properties at all. I think the "experimental" should replace the current version and be used by RichTextFX.

@alt-grr
Copy link
Author

alt-grr commented Feb 19, 2016

Ok, thanks! 😄

@alt-grr alt-grr closed this as completed Feb 19, 2016
@JordanMartinez
Copy link
Contributor

@TomasMikula This is what you guys were talking about on some bug thread regarding an InputMapping API, correct?

@TomasMikula
Copy link
Member

Yes, exactly. I developed the new experimental version of WellBehavedFX as an alternative to the proposed input mapping API that was supposed to go into JavaFX 9, because I felt strongly that the proposed design was flawed. In the end, plans to get the API into JavaFX 9 were dropped (which I think is better than having a bad API). As a result, I got back to my work and didn't really get to release the new version.

@JordanMartinez
Copy link
Contributor

Ah, ok. Cool!

@TomasMikula
Copy link
Member

For the record, the discussion is here: JDK-8091189.

I then also proposed to Jonathan Giles to work out some use cases side by side, so that we can compare our designs. So I wrote this document with some use cases and asked Jonathan to fill his solutions and optionally add more use cases. He never did it and dropping the feature for 9 was announced shortly after.

@ghost
Copy link

ghost commented Oct 23, 2016

Using:

compile group: 'org.fxmisc.richtext', name: 'richtextfx', version: '0.7-M2'

Consider:

import static javafx.scene.input.KeyCode.AT;
import static javafx.scene.input.KeyCode.ENTER;
import static org.fxmisc.wellbehaved.event.EventPattern.keyPressed;
import static org.fxmisc.wellbehaved.event.InputMap.consume;

public class Test {
  private StyleClassedTextArea editor;

  /** Convenience method. */
  public <T extends Event, U extends T> void addEventListener(
    EventPattern<? super T, ? extends U> event, Consumer<? super U> consumer ) {
    Nodes.addInputMap( getEditor(), consume( event, consumer ) );
  }

  private void init() {
    // enterPressed gets called
    addEventListener( keyPressed( ENTER ), this::enterPressed );
    // atPressed never called
    addEventListener( keyPressed( AT ), this::atPressed );
  }

  private void atPressed( KeyEvent e ) {
    System.out.println( "@ pressed" );
  }

  private void enterPressed( KeyEvent e ) {
    System.out.println( "Enter pressed" );
  }

  private synchronized StyleClassedTextArea getEditor() {
    if( this.editor == null ) {
      setEditor( createTextArea() );
    }

    return this.editor;
  }

  protected StyleClassedTextArea createTextArea() {
    return new StyleClassedTextArea( false );
  }
}

Actual Results

Only Enter presses produce print messages.

Expected Results

Enter presses and @ symbol presses print messages to the console.

Any ideas how to produce the expected results (i.e., how do you trap @ key presses)?

@ghost
Copy link

ghost commented Oct 23, 2016

Nodes.addInputMap( textArea, consume( keyPressed(DIGIT2, SHIFT_DOWN), e -> atPressed( e ) ) );

Possibly not cross-platform?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants