Get the mouse input

805 views
Skip to first unread message

Rajatt Sodhi

unread,
Oct 8, 2016, 7:03:30 AM10/8/16
to lanterna-discuss
Hi everyone, I have looked at this project and it is really interesting. I am trying to get the mouse input but I cannot find how to do this as I am trying to develop a little text editor with mouse support.


AvL42

unread,
Oct 8, 2016, 3:56:25 PM10/8/16
to lanterna-discuss
Mouse-support in Lanterna is only available for "real" terminals (Linux/Unix & MacOS).

What terminal-emulation on what OS do you intend to use?

Rajatt Sodhi

unread,
Oct 8, 2016, 4:11:42 PM10/8/16
to lanterna-discuss
Thanks for your reply. I am developing the app for linux/unix OS.

AvL42

unread,
Oct 8, 2016, 4:32:38 PM10/8/16
to lanterna-discuss
In that case, I'd suggest you look at the TerminalInputTest  (https://github.com/mabe02/lanterna/blob/master/src/test/java/com/googlecode/lanterna/terminal/TerminalInputTest.java) for how to enable Mouse-modes.
I haven't used it yet, myself, but if you have questions, then post them here.

Another "warning": mouse support in terminals mostly sucks (that's not lanterna's fault).  Once an application enables it, it means that you cannot easily select text to the clipboard anymore, unless (you also press shift). Especially if you intend to write an editor, then make mouse-support optional/configurable.   I know that for "vim" I always disable it, either by setting TERM to a non-mouse-supporting type, or by actually digging into vim-docs to see how to turn it off, properly.


Rajatt Sodhi

unread,
Oct 8, 2016, 4:40:21 PM10/8/16
to lanterna-discuss
Thanks for the quick reply. I will try and post any improvement/problem so it will be useful for other people.

Rajatt Sodhi

unread,
Oct 12, 2016, 1:10:18 PM10/12/16
to lanterna-discuss
I have another question related to mouse support, is mouse supported also on ssh session? I mean if the programm is installed on another machine and a user uses an ssh connection to connect to that machine and runs a programm that uses lanterna , will the mouse work(if it is captured in the programm)?

I also get the mouse working on the application by calling the setCaptureMouse method on the defaultTerminalFactory and casting the terminal to extended terminal and then initializing the screen with the istance of the extended terminal. Now I can set focus on different elements of the GUI bi clicking on them.

Andreas Leitgeb

unread,
Oct 12, 2016, 1:46:32 PM10/12/16
to lanterna...@googlegroups.com

unless the user uses a very ancient or trivial terminal emulator, it will work.  ssh doesn't even know - it just passes the data through transparently. It all depends on the actual terminal(emulator).
if ssh goes over a slow/laggy network, then other parts of lanterna might fail, though.

one more note: if you click inside the terminal drag outside and release there, you might get to see quite absurd pairs of coordinates, so don't assume it to be always sane.

Rajatt Sodhi

unread,
Oct 12, 2016, 2:01:47 PM10/12/16
to lanterna-discuss
thank you for the quick reply. Yes i have seen that if a click and drag outside i get strange cordinates. In addition if I click on the empty space of a window the application closes and I cannot type anything in the normal terminal (actually i can type but i cannot see the character i am inputting, i think this is due to the fact that i have not yet handled the input (pollinput).

Andreas Leitgeb

unread,
Oct 12, 2016, 4:17:45 PM10/12/16
to lanterna...@googlegroups.com

if that happens, do you get a shell prompt?  if so, then blindly type "stty sane".
lanterna shouldn't ever crash this way. Maybe you use an older version? If not, then we'd like a reproducible test case to see if we can fix it.

Rajatt Sodhi

unread,
Oct 12, 2016, 4:34:31 PM10/12/16
to lanterna...@googlegroups.com

I am using the latest version, when the problem occurs if i type stty sane two times i can see the command i am typing but i can also see the mouse coordinates appearing when i use the mouse(i suppose the code of setting the mousecapturemode to null is not executed , but when for example i press enter on the exit button where it sets the mousecapturemode to null i still get the same problem but i need to type stty sane only once in this case) . How can i provide a test case? Do i need to put the code here?

Thanks.
Rajatt

Andreas Leitgeb

unread,
Oct 12, 2016, 4:47:36 PM10/12/16
to lanterna...@googlegroups.com

if you could create a single class that does it all in its main method, then sharing that code here would be fine. So far I've only used TerminalInputTest, and that doesn't open windows, so I can't check this specific exiting behaviour.

in those cases where you needed stty sane twice, there probably were still some remainders of the mouse-sequences in the buffer.  typing ctrl-u combo before stty sane would reset the input line.

Rajatt Sodhi

unread,
Oct 13, 2016, 1:42:47 PM10/13/16
to lanterna-discuss

import java.io.IOException;
import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.gui2.BasicWindow;
import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.Direction;
import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.TextBox;
import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.screen.TerminalScreen;
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.terminal.ExtendedTerminal;
import com.googlecode.lanterna.terminal.MouseCaptureMode;



/**
 * testing mouse, application closes when click is performed in empty space of a window or on the screen
 * (mouse reporting remains enabled and in the terminal mouse reporting is still visible).
 * @author rajatt
 *
 */

public class lanternagroup {

   
public static void main(String[] args) throws IOException, InterruptedException {
       
       
//is it better to create a terminal and calling the setMouseCaptureMode on the default factory and
       
//then setting it to null by casting terminal to EXtendedTerminal or same as I have done?
       
ExtendedTerminal ter = (ExtendedTerminal) new DefaultTerminalFactory().setForceTextTerminal(true).createTerminal();
        ter
.setMouseCaptureMode(MouseCaptureMode.CLICK);
       
final Screen screen = new TerminalScreen(ter);
        screen
.startScreen();
        screen
.clear();
       
MultiWindowTextGUI gui = new MultiWindowTextGUI(screen);        

       
Panel menubar = new Panel();
        menubar
.setLayoutManager(new LinearLayout(Direction.HORIZONTAL).setSpacing(1));
       
       
TextBox text = new TextBox(new TerminalSize(10,10),TextBox.Style.MULTI_LINE);
        menubar
.addComponent(text);
       
        menubar
.addComponent(new Button("Open", new Runnable(){

           
@Override
           
public void run() {
               
Window op = new BasicWindow("Select file");
                gui
.addWindow(op);
                op
.setComponent(new Button("Close", new Runnable(){
                   
                   
@Override
                   
public void run() {
                        op
.close();                        
                   
}                    
               
}));
               
           
}            
       
}));
        menubar
.addComponent(new Button("Save"));
        menubar
.addComponent(new Button("Exit", new Runnable(){
           
public void run(){
               
try {
                    gui
.getActiveWindow().close();
                    screen
.stopScreen();
                    ter
.setMouseCaptureMode(null);
                   
               
} catch (IOException e) {
                   
System.out.println("Error exit");
               
}
           
}
       
}));
       
       
       
       
       
       
Window main = new BasicWindow("Test");
        main
.setComponent(menubar);    
        gui
.addWindowAndWait(main);
       
       
       
        screen
.stopScreen();
        ter
.setMouseCaptureMode(null);
       
       
   
}

}


Please find enclosed code as requested to reproduce the problem.
Rajatt

Andreas Leitgeb

unread,
Oct 13, 2016, 3:22:55 PM10/13/16
to lanterna...@googlegroups.com

Well spotted. Apparently, this path wasn't tested much, so far.

I'll add some push-requests on github to fix it, but I don't have any powers on the project, so we will have to wait for Martin to actually accept them.

PS: the reason why mouse mode isn't reset on crash is that you don't yet have the addWindowAndWait call inside a try-block, and the cleanup (stopScreen & setMouseCaptureMode) in its finally-block.

Andreas Leitgeb

unread,
Oct 13, 2016, 4:07:18 PM10/13/16
to lanterna...@googlegroups.com

Rajatt Sodhi

unread,
Oct 13, 2016, 4:29:27 PM10/13/16
to lanterna-discuss
Thanks.

Andreas Leitgeb

unread,
Oct 15, 2016, 3:30:14 AM10/15/16
to lanterna...@googlegroups.com

Martin has merged the fix, so you could get the sources from github, and hopefully see no more nullpointerexceptions.

Rajatt Sodhi

unread,
Oct 16, 2016, 1:02:40 PM10/16/16
to lanterna-discuss

Thanks, I will try the new sources.
One question, is it possible to binds key (also mouse) to some commands(for example the f1 button to underline some text in a textBox)?

Rajatt.

Andreas Leitgeb

unread,
Oct 16, 2016, 1:51:28 PM10/16/16
to lanterna...@googlegroups.com
Main obstacle here is, that TextBox doesn't support "rich text", yet, and I wouldn't bet on that it would be added within the next year - although some "basement" work in that direction (namely towards allowing labels to display rich text") is currently in the making.

To add keystrokes to components, it is necessary to create a subclass of some component, and override its handleKeyStroke method. (MouseEvent is a subclass of KeyStroke, so you get these, too, in that method.)


--
Det här meddelandet skickas till dig eftersom du prenumererar på ett ämne i gruppen "lanterna-discuss" i Google Grupper.
Sluta prenumerera på det här ämnet genom att besöka https://groups.google.com/d/topic/lanterna-discuss/rrFrIqHUPDU/unsubscribe.
Om du vill sluta prenumerera på den här gruppen och på alla ämnen i den skickar du ett e-postmeddelande till lanterna-discuss+unsubscribe@googlegroups.com.
Fler alternativ finns på https://groups.google.com/d/optout.

Rajatt Sodhi

unread,
Oct 16, 2016, 1:57:57 PM10/16/16
to lanterna...@googlegroups.com
Thanks , do you think that it is possible to develop a text editor using Lanterna (it will have basic functions such as a textbox where a user edits some text and some buttons to save/open file)?

Andreas Leitgeb

unread,
Oct 16, 2016, 2:23:35 PM10/16/16
to lanterna...@googlegroups.com
The TextBox is a rather simple Texteditor, and surely you could use it together with some buttons as a standalone editor, but...

Java apps aren't exactly known for fast startup, so no matter how simple the editor will be, it'll take a couple seconds to start.

There are already lots of powerful plain text editors (e.g. with search&replace, copy&paste, syntax highlighting, and much more) that start up within the blink of an eye.

Unless you think of some entirely new paradigm for text editing, then the demand for text editors (especially for terminals) is already quite well satisfied: vim, emacs, nano, joe, ...


Rajatt Sodhi

unread,
Oct 16, 2016, 2:33:13 PM10/16/16
to lanterna...@googlegroups.com

Thanks for the reply. I am doing this editor in Java and specially in Lanterna as I am doing my final year project (undergraduate for Software Engineering) and one requirement is that it can run in the terminal.

Rajatt Sodhi

unread,
Oct 16, 2016, 2:35:54 PM10/16/16
to lanterna...@googlegroups.com
​An addition that I am doing is that there will be a command line as part of the editor where the user can input some commands to apply to the text, for example capitalise all the text, move the cursor to the end of the line and add a line and so on).

Andreas Leitgeb

unread,
Oct 16, 2016, 2:43:28 PM10/16/16
to lanterna...@googlegroups.com
In that case, merely embedding the TextBox into a small application might be considered as "cheating" by your teachers ;-)
Rather ask your teachers first, if using the TextBox is ok, or if you're required to create something like TextBox, on your own.


2016-10-16 20:35 GMT+02:00 Rajatt Sodhi <rajat...@gmail.com>:
​An addition that I am doing is that there will be a command line as part of the editor where the user can input some commands to apply to the text, for example capitalise all the text, move the cursor to the end of the line and add a line and so on).

--

Rajatt Sodhi

unread,
Oct 16, 2016, 3:01:59 PM10/16/16
to lanterna...@googlegroups.com
Ok , I will ask my teacher, but probabily the important thingh I will do is add the key to command bindings, undo redo functionality and a little language that can be applied to the text (in the TextBox :-) ) .

Rajatt

Andreas Leitgeb

unread,
Oct 16, 2016, 3:07:44 PM10/16/16
to lanterna...@googlegroups.com
I wish you good success with your project!

If you find any further crashes, please report them, too ;-)

2016-10-16 21:01 GMT+02:00 Rajatt Sodhi <rajat...@gmail.com>:
Ok , I will ask my teacher, but probabily the important thingh I will do is add the key to command bindings, undo redo functionality and a little language that can be applied to the text (in the TextBox :-) ) .

Rajatt

--

Rajatt Sodhi

unread,
Oct 16, 2016, 3:08:47 PM10/16/16
to lanterna...@googlegroups.com
Thanks.

Rajatt Sodhi

unread,
Oct 19, 2016, 6:21:19 AM10/19/16
to lanterna-discuss
Hi, I have a question, is it possible to bind keys to some methods (for example user press a button called bind and then it asks for an input and then binds the button pressed to a method, so for example user wants to use \ to cancel a line in a textbox, press bind and then input \ and select the command it wants, now the user can use \ to cancel a line)?

Thanks.
Rajatt

AvL42

unread,
Oct 21, 2016, 6:23:29 PM10/21/16
to lanterna-discuss
As soon as the button is active (that is the state when you can press Enter to fire the action) you can check for any key in handleKeyStroke, so you don't really need to "ask" for input.

Probably your teacher didn't originally expect you to use a library like lanterna, so they probably tried to make it easier for those who just read keys one by one and do actions.
You can do that with lanterna, too: you can call  screen.readInput() and will receive a KeyStroke or a MouseEvent, but calling readInput directly doesn't fit in well with the textGui concept.


Rajatt Sodhi

unread,
Oct 21, 2016, 6:31:39 PM10/21/16
to lanterna...@googlegroups.com

Thaks, what I would like to achieve is dynamic binding for a particular component, do you think adding a hastable with keys and runnable events in handlekeystroke will help in achieving this? For example put a hastable inside textbox class and when a user press the button to bind a key, then this key is added to the hash table along with the runnable, so then in handlekeystroke it can check if there is a matching so it runs the runnable otherwise handle normally.
Is there a way to suppress ctrl c? As this closes the lanterna application?

Thanks for your reply and explanation.
Rajatt

Andreas Leitgeb

unread,
Oct 21, 2016, 7:32:32 PM10/21/16
to lanterna...@googlegroups.com
You already know how to handle KeyStrokes for a component and call specific code if these keys are pressed.
You can of course also use a HashMap<Character,Runnable> and from handleKeyStroke check for KeyStrokes
of KeyType "Character" and then use the character to look up the Runnable in the Map.

I won't do all your project for you, of course :-)


For the Ctrl-C:  to change this, you would need to create a UnixTerminal specifically, and pass to the constructor
the value UnixTerminal.CtrlCBehaviour.TRAP   (default is: UnixTerminal.CtrlCBehaviour.CTRL_C_KILLS_APPLICATION).
But then, you can't easily run it from eclipse, anymore. You could decide based on command line arguments (args in main)
to either call the DefaultTerminalFactory for a Terminal, or to create a UnixTerminal with that extra argument to trap Ctrl-C.

Unfortunately, the DefaultTerminalFactory cannot do it for you, and even if we added it to the Factory, then you'd still
not see it in the beta3 jars.


--

Rajatt Sodhi

unread,
Oct 21, 2016, 7:38:51 PM10/21/16
to lanterna...@googlegroups.com

Thanks,
I will download the new sources so i can have access to latest sources. :-)

Rajatt

Andreas Leitgeb

unread,
Oct 21, 2016, 7:44:24 PM10/21/16
to lanterna...@googlegroups.com
That will at least help you against the crashes when you click at "wrong" positions.
I can't promise yet when there will be any change about the Ctrl-C behaviour.

Rajatt

--

Rajatt Sodhi

unread,
Oct 24, 2016, 5:17:52 PM10/24/16
to lanterna-discuss

Hi, is it possible to get selected text from a TextBox? (i think probabily another condition can be added to the handlekeystroke method so it is possible to get the mouse positions while it is dragging and get the postion when it releases the mouse button, so start and end cordinates can be retireved?)


Thanks
Rajatt

Martin Berglund

unread,
Oct 30, 2016, 10:16:24 AM10/30/16
to lanterna...@googlegroups.com
There is no code currently in the GUI system to capture mouse input other than activating focus on particular components. It wouldn't be impossible to do this, but it would be a lot of work for a pretty narrow use-case. My recommendation is that you copy the code for the TextBox and modify it by adding a selection range and add mouse input handling to control it.

Martin

--
Det här meddelandet skickas till dig eftersom du prenumererar på gruppen "lanterna-discuss" i Google Grupper.
Om du vill sluta prenumerera på den här gruppen och inte längre få någon e-post från den skickar du ett e-postmeddelande till lanterna-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages