beryx / text-io

A library for creating interactive console applications in Java
http://text-io.beryx.org/
Apache License 2.0
342 stars 45 forks source link

Shutdown CTRL+C #2

Closed kirpi4ik closed 7 years ago

kirpi4ik commented 7 years ago

It seems that the console is not responding to the regular CTRL+C event(both System.console and swing terminal ), this is a limitation?

siordache commented 7 years ago

Currently, text-io expects only regular text as input. For key combinations such as Ctrl+C the behavior is unspecified and it depends on the underlying TextTerminal implementation. For Swing, Ctrl+C is by default associated with "copy". Using Ctrl+C with a SystemTextTerminal or a ConsoleTextTerminal will shut down the JVM. Note however, that (since version 1.5.0) TextIoFactory usually returns a JLineTextTerminal if System.console() is not null. JLineTextTerminal currently ignores the Ctrl+C, but this behavior can be easily changed by executing: reader.setHandleUserInterrupt(true); I should probably insert this statement in the JLineTextTerminal constructor.

kirpi4ik commented 7 years ago

Thanks for explanation, make sense. Although maybe would be nice to have some generic key listener I know that it would be hard for non-GUI console without jna. For SwingTextTerminal instance for now probably I can just add custom event dispatcher

kirpi4ik commented 7 years ago

btw, why the console would forbid closing the window, I would expect that terminal should have common or similar behavior for all implementations ? https://github.com/beryx/text-io/blob/master/text-io/src/main/java/org/beryx/textio/swing/SwingTextTerminal.java#L139

siordache commented 7 years ago

I added support for handling user interrupts. For the Swing and the Web text terminal, Ctrl+Q is used by default to trigger a user interrupt. (I didn't use Ctrl+C, because it is usually associated with copy in these environments.) The key combination that triggers a user interrupt can be changed programmatically. Closing the Swing window will also trigger a user interrupt.

The default user interrupt handler shuts down the JVM. A different handler can be configured by calling registerUserInterruptHandler(). See the commented lines in TextIoDemo for examples.

siordache commented 7 years ago

Implemented in 1.9.0. Configuration improvements (using terminal properties) in 2.0.0.