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.
Related
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.
I want to create a cross platform solution for providing access to the input, error and output streams of a Process in Java.
Basically, what I want to create is a text area that displays the Process' output and error streams, and allows you to supply data to the input stream. In other words, pretty much what Eclipse is already providing with its Console when you run an application.
Now, a basic implementation of this was easy, I simply send all key presses to the input stream. But, of course, I ran into trouble with pasting, backspace and arrow keys, handling ctrl-C and so on.
It seems I should wait before sending data to the Process' input stream. But wait for what? Should I send all entered (and pasted) text at each return key? Or after an interval? What about ctrl-C, ctrl-X and so on. Do I send arrow key movement to the input stream?
The easiest and most user-friendly solution is to have a "Send" button which sends the entire contents of the text area and clears it. Think instant messenger apps or SO comment editor.
You should not wait for anything, simply send - but send in a separate Thread, not your GUI-Event-thread, so the latter one does not block.
For handling the special characters, look what you would get when these signs are entered in a text console.
I am writing a program that takes commands via the console.
However, I do not want to press "enter" in order to send that command.
I want the code to constantly monitor what I am entering, and execute the code when I am finished.
The commands are coming as text from a speech recognition program, therefore, eliminating the need for the "enter" stroke is pretty key.
Any one have any ideas?
I have recently come to the knowledge of events in java and i believe this would help you. You would just need to associate the speech recognition printing to the screen with an event in java and it would have a listener to listen for the event and when it sees the event it would execute your desired code. I currently have a thread opened where im trying to get some good examples of this, perhaps that will help.
Java Events Question
Already answered here:
How to read a single char from the console in Java (as the user types it)?
No portable way to do it, depends on your platform.
I haven't worked with Java since college, but if you're reading from the command line, then you are using System.in.read() or something similar. Since these are blocking method calls, your application will never be notified about the new input until ENTER is pressed.
If I'm dead wrong, please let me know.
You're probably better off using a simple (possibly even hidden) that can take the user text and an event listener on the UI element that reads it in to the application's text processor.
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!
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.