I am writing a Java application that uses both Scanner and Console to get input from the user. I would like to add command history support so that the user can use the ARROW KEYS to search previous inputs (similar to a terminal). Is there a way to do this? Right now, when I use either the Scanner or the Console, I get weird symbols like ^[[A when pressing the arrow keys.
I have read about KeyListener and KeyEvent, but my application does not use a GUI.
Thanks!
Good question - +1'd. On Windows, I would SetConsoleMode to change the console to take raw input, but on *Nix it looks like there is more work involved. Unfortunately I don't have any code that I can show you right now, but have a look at this link and see if it helps.
http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/
Related
String cmd = "start calc.exe";
Process process = Runtime.getRuntime().exec(codeString);
I can call calculator out, but I wish to specify a accurate position like (200,300).
how can I rewrite my cmd String?
I know that java.awt.window can set a window or frame to the specific position.
Is there any method I can use to fill frame or window with my process?
There is no clean pure java solution because JDK does not provide API that can control non-java windows. So, if you want to can use JNI/JNA.
But I can suggest you a patch that will typically work.
Windows OS allows moving windows using keyboard. Try the following manually:
Win+R
type calc and press enter
press alt+space
press M
press enter
now use arrows to move the window. Press ESC to exit this mode.
All these actions can be implemented using java.awt.Robot.
So, you can run calculator and then immediately move its window where you want.
Well, this is not clear solution, but very simple one.
Expected Problems:
Alt+space is mapped to other, custom application
Other window that started together with calc overlaps it.
User will see that window is created somewhere and then quickly moved.
So, everything depends on how important all this for you. This solution is good as an exercise or demo but bad for real commercial application.
i want to create a terminal application in java, but i don't know how to replace text lines that are already printed (kind of "edit the lines that already printed"). \r only returns to the beginning of the last line, and i want to display a 2 dimensional grid.
this is a sample for what i want to print:
System.out.println("################");
System.out.println("#--------------#");
System.out.println("#--------------#");
System.out.println("################");
System.out.println("\r################");
System.out.println("#-------X------#");
System.out.println("#--------------#");
System.out.println("################");
you should try JLine library. It provides many userful functions for command line applications. http://jline.sourceforge.net/javadoc/
Using System.out.println whatever text is printed to the console is already flushed and cannot be edited. What you can do is you can clear the console and reprint using a fresh set Sys Outs. But for a graphical application I would suggest you have a look at JAVA AWT which will give you functions like paint() and repaint() and other rich UI functions.
For clearing the console you can refer to this: clear console
For JAVA AWT: AWT tutorial
So I am trying to use a robot (either keypress or mouse) to simulate some tests on an application. However, this program isn't allowing me to do so and I'm pretty sure it is a security feature. The application I am using is TEMS Investigation and I am trying to simulate a record and stop recording for some automated tests I have. The program I am using to automate these tests can not access TEMS so there is no way for me to do this without a Robot. So my questions are:
1) How can I override whatever it is that is preventing me from using my robot.
2) Would this be considered illegal if I did? I don't need to break any laws...I just want to get my testing done! lol
I can't even runs something as simple as:
Robot r = new Robot();
r.keyPress(KeyEvent.VK_F2);
r.keyRelease(KeyEvent.VK_F2);
It will run, as I have downloaded some software to tell me what keys I am pressing....just wont be recognized while the TEMS application has focus.
Update: I found out using Notepad I can't use any keys (like "ALT +
F") to perform any menu options. It doesn't even pull up the "File"
menu. I am able to simulate typing in text, just not the context menu.
What was really weird is that I can't even use mouseMove() while TEMS
has the focus
It sounds like your runtime has no knowledge of the TEMS app. If you start it in your java code it should work. For example the following:
Runtime.getRuntime().exec("notepad");
robot.keyPress(KeyEvent.VK_J);
Will bring up notepad and type in the letter J. Try exec-ing TEMS and then send the key press.
And no, it's not illegal.
UPDATE:
To open the file menu in notepad, you would do the following:
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F);
The two keys must be pressed together before a robot.keyRelease.
I created a text-based game similar to Zork and I need a gui to run it outside of Eclipse. I want to run it as a jar. (by the way I'm on a mac if that changes anything). I only need an output field and an input field. What would be the easiest way to achieve this?
And how much of my code would I need to change? (I used System.out.print for output and a Scanner for input)
If you want to crate GUI like console the simple way to do it is to add textarea component to your frame or or panel that has scroll bars through the viewport. Create a stream that feeds the component with text. Then simply redirect standard output to that stream. Finish. Start your GUI and enjoy the console.
If you don't want to run this on a terminal, you should probably use Swing with a JTextArea in which you append all the messages to the user, and a simple JTextField for the user to enter his commands.
Here's a quick example of JTextArea so you get an idea. You'll need to read more about events on Swing to make things like reacting to the user pressing the ENTER key to read the contents of the text field and run the game logic.
Note that the screenshot on the example above uses the "Metal" look and feel, but it should look much closer to a native application on the Mac.
I'm looking for a way to inject a keystroke into the OS keyboard input buffer,
like when you click a button the program inserts one (or more) keyboard strokes. I wanted to do this in java because I want to run this in (win,linux and osx). I guess that I'll have to make use of the JNI, do anyone have some ideas?
Thanks all stackoverflowers ;)
My guess is that the java.awt.Robot class will do this for you:
new Robot().keyPress(...);
http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#keyPress(int)
java.awt.Robot "is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed."
Check java Robot . I believe this is what you are looking for.
Also check this out. Example