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

Execute some code after ENTER Key pressed #319

Closed
firm1 opened this issue May 25, 2016 · 5 comments
Closed

Execute some code after ENTER Key pressed #319

firm1 opened this issue May 25, 2016 · 5 comments

Comments

@firm1
Copy link

firm1 commented May 25, 2016

Hi,

What is the best way with RichtexFx to execute some code after ENTER Key ? I use 0.6.10 release.

I want if possible override this line for example, but i don't know how i can do that.

Can you explain me please ?

@TomasMikula
Copy link
Member

TomasMikula commented May 25, 2016

Hi,

if you are using 0.6.10, you actually need to look at this version of the file, which uses an older version of WellBehavedFX.

You will need to do something like this:

// define the event handler
EventHandler<KeyEvent> handler = EventHandlerHelper
        .on(keyPressed(ENTER)).act(e -> { /* code to execute on Enter goes here */ })
        .create();

// install the event handler
EventHandlerHelper.install(area.onKeyPressedProperty(), handler);

@firm1
Copy link
Author

firm1 commented May 25, 2016

Thank for answer.

But this code override the Enter key behavior. I just need to add something
after existing behavior, not rewrite.

Le mer. 25 mai 2016 18:48, Tomas Mikula notifications@github.com a écrit :

Hi,

if you are using 0.6.10, you actually need to look at this version
https://github.com/TomasMikula/RichTextFX/blob/3d37aa19b0f3e2f171dafb7a500f2dee122e394c/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaBehavior.java#L72
of the file, which uses an older version of WellBehavedFX.

You will need to do something like this:

// define the event handlerEventHandler handler = EventHandlerHelper
.on(keyPressed(ENTER)).act(e -> { /* code to execute on Enter goes here */ })
.create();
// install the event handlerEventHandlerHelper.install(area.onKeyPressedProperty(), handler);


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
#319 (comment)

@TomasMikula
Copy link
Member

The most reliable way is to first replicate in your handler whatever StyledTextAreaBehavior does, i.e. area.replaceSelection("\n"):

EventHandler<KeyEvent> handler = EventHandlerHelper
        .on(keyPressed(ENTER)).act(e -> {
                area.replaceSelection("\n");
                /* your code goes here */
        })
        .create();

@TomasMikula
Copy link
Member

You could also just try using the usual area.addEventHandler(KeyEvent.KEY_PRESSED, e -> { ... }), but there is no guarantee that this will be called after the default handler.

@firm1
Copy link
Author

firm1 commented May 26, 2016

Ok thanks for your advice.

It's ok for me

@firm1 firm1 closed this as completed May 26, 2016
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

2 participants