Send string from java (or other platform) to application - java

I want to send a barcode, read with my cellphone, to my computer. My computer has a simple server running, which listens to barcodes. When a barcode arrives, the server app should be able to input the value of the received barcode into the active application (I don't really care which application is going to get the input, the user should be able to select gedit, a terminal window or the browser if they choose to).
My language at the moment is Java on GNU/Linux (Ubuntu), so I know about the Robot class. But the Robot class emulates a keyboard, which means: when you send VK_1 on a US keyboard layout, the output is '1' indeed, but when you send VK_1 on another layout (like belgian, which I use), which requires shift for the '1' key, the output is '&' (this is the character on the '1' key, when you don't hold shift).
I also found xsendkeys, but this application too requires you to specify whether you need to hold shift. So it will be able to send an 'a' but for an 'A' (thus capital) you need to specify you want to hold shift with your 'a'.
Isn't there an easy way to do this, for GNU/Linux and Windows, just using strings. I want to be able to send "12a68dd" to the active application. And I also would like to be able to send UTF-8 characters to the active application.
I have been looking for a solution, but most require the breakdown in multiple keystrokes, which are often dependent on the keyboard layout.

Seems like you want to be able to send an arbitrary keyboard sequence to any possible application. With that I cannot help you (you should look for "Java UI testing automation" to find any suitable tools), but if the application you are sending the string to listens for it on its standard input, I would go for:
// Example: send your string to "cat" (or "type" on Windows), which simply prints it.
Process spawned = ProcessBuilder.command("cat" /*No arguments*/).start();
spawned.getOutputStream().write(yourString.getBytes("UTF-8"));
Simple stdin/stdout redirection, in other words.

If I understand you correctly, you wish to send a series of characters into another application (the destination). This destination could be any application, and you may not have access to its source code.
The answer is simply no.
Key strokes differ to characters (which I gather you have probably worked out) and Robot was intended just to invoke key strokes. The resulting output of those key strokes is generally different due to the fact most keyboards used do not follow the ISO standard for keyboards.
There are often other ways of accomplishing the same affect though, through APIs, file IO, etc.

I may not have grasped your question completely, but you want to separate applications, both written in Java, to exchange information? I'd recommend you read up on RMI, which exists för that very purpose.

Just wanted to let you know my sollution:
Call xvkbd -text from java and give the text to be writen as argument. If the text contains spaces, I call xvkbd multiple times with a xvkbd -text \[space] call within.
My current way is pretty easy to 'port' to windows, so that wont be too hard to get running with a SendKeys VB application.
Thanks all for your help!

Related

how can we simulate Ctrl+C to stop the console app from waiting for user input in java

I am trying to test a command line app that waits for the user input after every step. I am able to test the app using System Rules provided by Stefan Birkner. Currently, I provide inputs from the beginning to the end which works like a charm and I can assert the final output from system log.
However, I want to test for the negative cases before the end of the app for which I give invalid inputs in the beginning to evaluate the error message. When invalid inputs are given, the console prints an error message and keeps waiting for the user to provide a valid input. How do I send Ctrl+C using as shown below:
systemInMock.provideLines(Ctrl+C);
systemInMock.provideLines accepts only strings. Is there a way to send Ctrl+C signal?
An example of my junit test is shown below:
#Test
public void testInValidMarker() throws Exception{
systemInMock.provideLines("abc","def","1");
Main.main(new String[]{});
assertTrue(systemOutRule.getLog().contains("Invalid marker, try again"));
}
Appreciate your help!
If I'm not mistaken, when you do ctrl+c, it doesn't actually get written to console. If that's true, then in no case will your program ever be given ctrl+c, so provideLines will never be in a position where it is given ctrl+c.
For proof, open up cmd and type in a program with program arguments (in my case, I use ant). If you type ant and then ctrl+c, the cursor is moved to a new line.
There are two ways you can control termination behavior:
You can use a shutdown hook (found from this previously asked question ). Doing this will allow you to handle what happens (potentially with issues).
Or you could create your own termination argument like -q or q, which would trigger an action to end the program (maybe a System.exit(1)). This way you can mock that input.
In UNIX/Linux, when you type CTRL-C, your shell intercepts it and sends the process a SIGINT signal -- see: How does Ctrl-C terminate a child process?
Therefore the System Rules project doesn't have anything to help you -- in this situation the process doesn't receive any character input.
By default, the whole JVM shuts down when it receives SIGINT. This is obviously bad news for a running test.
The SO question Signal handling using "TERM" -- may be of use.
A side effect of Java's portability is that for some OS features, it either abstracts things away until they're unrecognisable, or doesn't expose them at all.
I suspect what you're asking for can't be achieved.
If you're allowed to change the requirements slightly, you could ask the user to close with CTRL-D -- this closes stdin with EOF.
Although it's quite the overkill, you could launch a whole new JVM running your program, using ProcessBuilder. You might imagine you'd get an API to send arbitrary signals to that process. But for portability reasons, all you can do is process.destroy(), which sends SIGTERM.
Tried this as a comment, but it didn't read right. It's not exactly an "Answer" though.
So Java is really bad at console input. It reads an entire line at once and you can't do anything about it--there is no way to trap special characters or even see any of the input before the user hits return. Also I think a ctrl-d will close your input session--(Add that test to your use case if you don't use any other suggestion here because it can put you into a state you didn't expect!)
Three suggestions:
The simplest: If you can use a GUI and aren't really looking for an ongoing input/response REPL the simplest answer is usually to use JOptionPane to throw up a quick dialog. It's a one-line solution to get some user input, but not so good for an ongoing command-driven system.
If you can't use swing (If you are running headless) then you may have little choice, but you can use the JLine library. That will give you a lot more flexibility. This is how Groovysh does it's REPL. It will let you see each character as it is typed and do things like completion where a user might type part of a file name and hit tab and you put the rest in for him.
If you don't want to use JLine but want a REPL feel there is also a more complex GUI solution--create a swing console window. A trivial solution would just be a text input box to allow typing and a text area to display results, but there are certainly libraries out there with more complete console solutions.
The point here is that using Java standard input alone is just not a good solution for anything beyond a trivial/personal script--and even then I avoid it. Perhaps not the answer you asked for, but maybe it's the one you need :)

Java key release event from STDIN in terminal raw mode

I'm trying to write a console game using java, and when you hold down a key, I want it to move your character constantly.
This means I need key press and key release events, because there is a delay when you hold down a key before it starts typing that same letter over and over.
I've already put the terminal into raw mode using stty, but I don't seem to be getting key release events.
This answer seems to indicate that I can indeed get those events from STDIN: https://stackoverflow.com/a/1598436/2404047
What I actually receive when in raw mode is a single byte for most keys, but in some cases, like the arrow keys, I get multiple bytes.
Is there a way I can get key press and release events through STDIN, or is a console game like this simply not doable?
This is your missunderstanding. You cannot get events from console using pure java.
Possible solutions are:
Get events using native utility, redirect them to file/pipe and read them into java code from that file
Use JNI/JNA directly
Use library that already wraps JNI and expose Java API for this purpose. Take a look on Charva that provides you API you need and even much more.

accessing androids auto correct facilities?

Is there a way of using Android's auto correct / predictive text capabilities with a bespoke input method? I'd like to be able to access a list of the nearest words to the word entered, similar to what happens when we send a text. For example if I entered the string "hapy" I would get a list containing "happy", harpy", "hazy" ...
Looks like a yes.
And the place to start: http://developer.android.com/guide/topics/text/spell-checker-framework.html
You wouldn't want to. First off, predictive text was only turned into a service with 4.0. Before then it was just part of the keyboard, and most keyboards still implement their own I suspect. Secondly, it would be optimized for typing mistakes, not voice mistakes. Typing g instead of f is common (they're next to each other), doing it by voice is not. It wouldn't work well.
But the built in voice to text behavior does return alternatives- it returns an array list of possible texts. That is your auto-correct.

editing a string using BufferedReader in Java [duplicate]

Say I allow the user to edit something, like the phone number in an Address Book (actually, that's exactly what I'm doing). Is there something that I can add to println that will allow me to insert a variable to display as fully editable text? The assignment that I'm doing this for doesn't actually call for this, but I think it would be cool to have. I'm looking on Google but can't find anything, then again I don't really know what I'm looking for and whether or not I have the correct terms in mind ...
No, not using only what Java provides in the framework. Editing some text would require to
act on key press, which is not possible as in Java the input is buffered (i.e., wait for Enter to be pressed)
to move around in the text you output, which is also not possible
This could be done using some native code (ncurse on linux, ...), using JNI or JNA, but not that easily.
Note that there are some projects that aim to add those functionalities, so if you can use something outside of the core libraries, you could give them a tries... for instance http://code.google.com/p/java-console-api/
There are various options for this, in order of simplicity and portability to features and complexity:
Simply prompt for the information, reading a complete (return-terminated) line of response, and allow the normal terminal input facilities to be used for basic editing.
Use something like the gnu readline library to allow more advanced editing. You still won't have widgets (text input boxes at specific places on screen) as such though. There's a java implementation here: http://java-readline.sourceforge.net/
Use something like ncurses to specifically position the cursor, print text labels, handle keypresses, and implement your own text input box. Not fun.
Use a textual user interface library (TUI), like this one: http://www.bmsi.com/tuipeer/
If you opened a window that looks like the console window, and could react to keypress events, then you could do what you are asking, but, otherwise, if you are just running a program, the program will have ceased executing and returned control to your console, so it can't do anything else.
But, if you use a scriptable version of java you could write your own shell, and then you could do what you are asking, as the shell would not cease executing.
But, that will probably be beyond your course.

Is it possible to print text that can be edited by the user (for console programs)?

Say I allow the user to edit something, like the phone number in an Address Book (actually, that's exactly what I'm doing). Is there something that I can add to println that will allow me to insert a variable to display as fully editable text? The assignment that I'm doing this for doesn't actually call for this, but I think it would be cool to have. I'm looking on Google but can't find anything, then again I don't really know what I'm looking for and whether or not I have the correct terms in mind ...
No, not using only what Java provides in the framework. Editing some text would require to
act on key press, which is not possible as in Java the input is buffered (i.e., wait for Enter to be pressed)
to move around in the text you output, which is also not possible
This could be done using some native code (ncurse on linux, ...), using JNI or JNA, but not that easily.
Note that there are some projects that aim to add those functionalities, so if you can use something outside of the core libraries, you could give them a tries... for instance http://code.google.com/p/java-console-api/
There are various options for this, in order of simplicity and portability to features and complexity:
Simply prompt for the information, reading a complete (return-terminated) line of response, and allow the normal terminal input facilities to be used for basic editing.
Use something like the gnu readline library to allow more advanced editing. You still won't have widgets (text input boxes at specific places on screen) as such though. There's a java implementation here: http://java-readline.sourceforge.net/
Use something like ncurses to specifically position the cursor, print text labels, handle keypresses, and implement your own text input box. Not fun.
Use a textual user interface library (TUI), like this one: http://www.bmsi.com/tuipeer/
If you opened a window that looks like the console window, and could react to keypress events, then you could do what you are asking, but, otherwise, if you are just running a program, the program will have ceased executing and returned control to your console, so it can't do anything else.
But, if you use a scriptable version of java you could write your own shell, and then you could do what you are asking, as the shell would not cease executing.
But, that will probably be beyond your course.

Categories