tags:

views:

151

answers:

4

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.

A: 

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.

mikek
That is not the idea :). I have one Java app the server, which should send keystrokes to any other possible application. The server part is only there to actually receive the barcodes from my cellphone.But thanks for your time.
MrSnowflake
Ah, I'm not sure that's doable...and it probably won't be very elegant. If all applications involved are webservices on the other hand, then you're just dandy.
mikek
Of course this is doable, your keyboard driver/kernel module does this :). Though I hoped this would be easy, like the Visual Basic SendKeys examples floating around the internets.
MrSnowflake
A: 

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.

dimitko
That would only work for console applications no?My idea is to have the server input some barcode in the inputfield of a webform or maybe in a text editor.Thanks for the tip though, I'll have a look at the testing automation.
MrSnowflake
As another poster said, short answer is you just cannot provide input to any possible application. *That is simply not possible not only for Java, but for the whole IT sector*. This is why people are developing all sorts of protocols (like WebServices, RMI, IIOP, to name only a few) to allow their applications to "speak" to other applications who "comprehend" these protocols (think of it as human language; you should first learn Japanese if you want to listen and understand it). Any UI automation testing tools you may find will be bound to certain OS or GUI toolkit. You have been warned. :)
dimitko
...and btw, no, that won't work only for console applications. *That will work for all applications who actually read their stdin stream*. Every application has stdin/stdout/stderr streams, even the GUI applications (with no console interface), but not all of them are actually reading/writing the streams.
dimitko
I know all of that. I don't really care if it's not platform independent, though that would be a plus.I want to emulate keypresses, like SendKey(s) on Win32.And you are correct all apps have stdin, but how many GUI apps use that? My guess is very few, and as such I ignored them, but it's an uneducated guess :).
MrSnowflake
A: 

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.

Generic Prodigy
A: 

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!

MrSnowflake