beryx / text-io

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

can't copy and paste text from Swing console in linux #18

Closed mokun closed 5 years ago

mokun commented 6 years ago

Hi,

Is there a method to call to enable the ability to copy and paste text from a Swing terminal in linux ?

Thx!

siordache commented 6 years ago

Ctrl-C / Ctrl-V should work out of the box. I have no issues with copy and paste on my Linux system.

shirishag75 commented 6 years ago

I think I know part of the problem. I doubt you have played the game. In the game CTRL+C is defined to show list of countries as can be seen in the screenshot I'm sharing below.

mars-sim-ctrl-c-show-countries

@mokun One workaround is have some other key combo for list of countries.

Some other workarounds could be to have it behave like a traditional x-term-emulator which means having a full-fledged Title bar support which means having things like

Edit and Help at the very least . Help showing the version of beryx/text-io and Edit having Select All, Copy and Paste using either keyboard or/and mouse.

Another could be just be having a mouse support with right-click menu which has Select All, Copy and Paste.

The easiest though would be to drop CTRL+C from list of countries and perhaps have something like

CTRL+Shift+C for list of list of countries perhaps ?

Even though then the problem of Selecting arbitary rows and columns of text is still not solved. I did try to use the mouse to mark from where I want text to be selected till where it should select but was unsuccessful. I have to confess I have been spoiled in being able to use this functionality by almost any x-term-emulator.

The game has a console front-end and hence we need the functionality to be able to copy partial text and show the inconsistencies therein so they can be ironed out by @mokun

I do hope I have been able to make myself clear.

Please let me know if I have been unable to articulate the issue if unclear somewhere and will try to be more clear.

siordache commented 6 years ago

You can register handlers for the copy and paste actions. Try something like this:

terminal.registerHandler("control INSERT", t -> {
    t.getTextPane().copy();
    return new ReadHandlerData(ReadInterruptionStrategy.Action.CONTINUE);
});
terminal.registerHandler("shift INSERT", t -> {
    t.getTextPane().paste();
    return new ReadHandlerData(ReadInterruptionStrategy.Action.CONTINUE);
});

Replace "control INSERT" and "shift INSERT" with the key combinations you prefer.

shirishag75 commented 6 years ago

Could there be something for arbitary text selections via mouse or should I/we put up a new bug instead to address that ?

siordache commented 6 years ago

This will append the text selected by mouse to the input:

terminal.registerHandler("shift INSERT", t -> {
    String selectedText = t.getTextPane().getSelectedText();
    if(selectedText != null) {
        t.getTextPane().setCaretPosition(t.getDocument().getLength());
        t.appendToInput(selectedText, false);
    }
    return new ReadHandlerData(ReadInterruptionStrategy.Action.CONTINUE);
});
mokun commented 6 years ago

@siordache ,

The Ctrl-C and Ctrl-V works by default in Windows at least.

Appreciate with your snippet of codes above. Now I can change it to whatever I need.

Other than keyboard, can we have a right-click popup menu for doing copy and paste with a mouse ?

shirishag75 commented 6 years ago

@siordache ,

The Ctrl-C and Ctrl-V works by default in Windows at least.

Appreciate with your snippet of codes above. Now I can change it to whatever I need.

Other than keyboard, can we have a right-click popup menu for doing copy and paste with a mouse ?

It will surely help us if we can have mouse support as well. Would making a right-click menu with options for Copy and Paste be too much work ?

siordache commented 6 years ago

The SwingTextTerminal exposes its frame and textPane, so you can add context menus or main menus to them using plain Swing programming. I recommend putting these changes in a subclass of SwingTextTerminal, as shown here.

mokun commented 5 years ago

@siordache , thanks much for the implementation ! I'm closing this thread now.