Display system messages without interrupting user input in java - java

I am writing a shell program in java, where it prompts to my user to enter input (i.e. "Prompt: ")
However separate threads that monitor activity also print to the shell, ex: "Client Connected!"
Any ideas/tips on how to make this a more user-friendly environment, where input from the user preceding a return would not be interrupted by a system message, but still allowing the message to display?

Typical console applications print information to screen sequentially. Programs the print information while waiting response to user are interactive program that usually control where and how do they print information. As an example you can see how top works on Unix.
There is a way to implement such kind of applications in java. You have to implement escape sequences for each supported terminal, so that you can control where and how to print each character. Fortunately there is such library that already does this.
See for example this discussion:
What's a good Java, curses-like, library for terminal applications?

Related

Java/Command Terminal: Prevent typing in terminal from getting cut off by incoming print statements

I am trying to make a very simple chat program between two computers (server and client) using Java's UDP DatagramSockets and DatagramPackets.
While they are able to successfully send messages to each other and print the messages on each other's terminal, a limitation I found was that if I am in the middle of writing something and there is an incoming message, my own typing will get cut off. This is due to using print statements to display the incoming messages on the terminal. An example:
I am wondering if there are any kind of tools, frameworks or even ways of getting terminal input that can help to prevent this kind of display cut off of sentences we are typing.
There exist frameworks that emulate "smart" terminals like the VT100. (The VT100 used to be considered pretty smart back in its hay-day, some 50 years ago.) Once you have such a terminal emulator, you will be able to position the cursor anywhere you want within the terminal window, (the screen of the terminal,) either by calling methods of some class, or by emitting ANSI escape sequences (look it up) and any text you write to the terminal will be written at the position of the cursor, so you will be able to keep the outgoing text separate from the incoming text.
A google search for "vt100 emulator for java" yields several results, the first one being on GitHub, (so, full source code available,) and is by none other than JetBrains, the company that makes the pretty awesome IntelliJ IDEA IDE.
However, I would advice against doing that, because your chat application will be clunky and it will have a look and feel that will resemble the seventies.
You will be much better off using java's built-in graphics system (AWT/Swing) to create a simple window with two text areas, one for the incoming text, and one for the outgoing text. There are many (I mean, many) instances of sample code floating out there on the great interwebz for doing pretty much about anything using Swing, so you should probably be able to even find a ready-to-run sample chat application in Java using Swing out there.

Capturing keyboard input from specific USB port

i'm stuck at capturing keyboard events.
I'm working with a barcode scanner which is detected by system as traditional keyboard (it is it's only mode, my client has a lot of budget scanners which can't emulate virtual com port).
My goal is to be able to detect at which port is this scanner plugged in, capture any input from that port, and prevent it from being handled by the OS (i don't want the barcode numbers popping up on focused input).
All this must be performed by a background service, which is never an active window, AND the solution must work both on linux and on windows.
I've been able to capture the input using jnativehook, but failed to prevent it from being handled by OS.
As far as i know, it is impossible to achieve this using pure java (JVM gives access to keyboard events only when application window is focused), so how should i handle this issue? I'm okay with using jni and c++ if that's necessary, but i don't know where to start.
JNativeHook does have the ability to discard events on Windows and OSX (not Linux), however, it does not have the ability to determine which USB port the scanner is connected to. The reason it cannot consume events on Linux is because the XRecord API makes a copy of the events and doesn't sit directly on the input event loop. If the devices you are working with can utilize HID, I would take a look into some of the JNI HID library wrappers like gary-rowe/hid4java or signal11/hidapi. The assertion that "it is impossible to achieve this using pure java" is correct. The only way to do it would be though native code, and HID is the most appropriate native method to accomplish your goals. Other input methods like input hooking (used by JNativeHook) will not be able to provide you with the source USB port, nor can they consume events on some platforms (Linux). There is another option out there like melloware/jintellitype that use a different mechanism for capturing input. I don't think it can grab input based on the port, however, the non-portable Linux equivalent of this library uses the XGrab API which will only consume events, but again, I don't know if it will be practical for your input source as I think it can only bind one key per call and that key must use a modifier.
Hope this helps you move forward with your project.

Java block keyboard presses

Is there a way how to block keyboard input in java. I would like to catch the input in the java code, but stop it from being send to OS.
Example: i have notepad opened and i can write just fine, but when i press a combination of keys java app catches that input, and now i should not be able to write with my keyboard. Is this kind of behaviour possible?
I know how to capture key presses but the keyboard blocking part is a mystery to me.
I tried googling it but i did not find any solution.
You have to understand: java applications run with the JVM. A JVM isn't the operating system.
Therefore your ways to access resources belonging to the operating system are very limited.
In other words: there is no generic, cross plattform way of having a Java application being able to "intersect" arbitrary console user input for arbitrary other applications.
Imagine you are a person sitting in a bus - just a guy like everybody else. You have no authority to turn to fellow passengers and ask them for their passport or such things. You are just one guy in the bus, like everybody else. Same here: a Java application is lacking the means to control other processes.
As you are specifically asking about the Windows platform: there might be some options using JNI and specific native calls. See here for example.
So, to be precise: it is not possible in general, but depending on your operating system there might be ways, for example using JNI.
Yes, you can do this - I used this library:
https://github.com/tulskiy/jkeymaster
to successfully to register a "global" keyboard shortcut to open a window in my program that was running in the task tray.
You can't "stop" it from being sent to the OS, but you can register a keyboard shortcut, open your window, and give it focus, so that all other keystrokes go into that text box.

Java: interact with another process

If I have a random program, that shows some text and has some text input, is there a way to write a java program, that reads that text labels and/or fill that text input fields and press an ok button?
Text applications are things that run on the cli, and have no windows. On Microsoft Windows the 'dir' command is an example.
Graphical applications are things that the beginning user might see, and has buttons, text boxes, scroll bars and stuff like that. On Microsoft Windows the 'paint' program is an example.
Web applications are websites that provide front ends as web pages. The 'gmail' application is an example.
If you want to interact with an application using Java, the application type will determine your approach.
text application - use ProcessBuilder, it's a java class designed to launch and (to a degree) interact with processes through stdin, stderr, and stdout.
Graphical application - It depends on the graphical widgets the application uses. If those are not supported by a library that can navigate the presentation, then input is limited to x,y coordinates from the application's origin, and input could fail to go into the right component.
Web applications - Use selenium. It is a custom web browser solution that permits testing of web sites; but, you could use it for your task. It is big and complicated, but considering what is required for this task, it is comparatively easy to use.
Yes you can write a new program that can give input and trigger the service of 'OK' key.
Your first program needs to be designed in the way that - it should accept the input from second program.
You can design the first code as web service in a web application . your first program will be web service provider and second program will be web service consumer.
Using second program you can post the required data to first code. and all the triggers of 'OK' button can be processed from first application.

How can i allow three threads accessing three different consoles in java?

So i am trying to build a simple application to show concurrency in java. I wanna open 3 console windows so that i can get input output from all of them. How can do this?
Edit:I have 3 threads, each thread has its own set of cache. I wanna run 3 consoles separately in java.
i opened command prompt using Runtime class and tried attaching input and outputstream to it. But failed writing or reading from that newly opened console.
In most OSes, a process can open only one console.
You could create multiple processes and communicate between them, but that would show interprocess communication rather than inter-thread communication.
On Windows, you could try spawning child processes using 'more' or 'copy con' commands and writing to their input stream, or run your own java client which just echos input to output.
You could also create multiple JFrames and record the output of the threads in them, but again a given process has to do all its swing updates in the one swing thread, so the different threads have to sync when they output.
You're probably better off writing to the same console with different prefixes; output to the screen is always going to be a single thread in current OSes.

Categories

Resources