I have a really nasty problem. My java application is running on unix and I'm using VNC to connect (from windows). The problem is that VNC has some issues with registering non-ansi keysum's.
KbdAddEvent: unknown KeySym 0xc0 -
allocating KeyCode 117
Even so, keyboard input works correctly in xterm, mozilla and etc. But when I'm using java for this keycodes to work, I need first to enter them wiht my keyboard (all of them, so VNC can allocate them), and after that to start java.
So my question is, is there a way running java instance to re-read keycodes from environment, so for me to be able to use non-ansi keycodes without typing them first, and only after that starting some java application.
Related
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.
I'm designing a console application for a server running RedHat. The end users should be able to run this app with any terminal of their choosing. (For example; Gnome Terminal, Putty SSH/ Telnet, MS Telnet Client and others).
In most terminal applications there's nothing wrong, however when I launch my program from a MS telnet session I notice my special inputs for System.in and System.console() get totally messed up. A backspace will write ^H to the screen and other keys write gibberish as well.
I've hacked at it enough that I can get it to consistently work, but I'm sure what I'm doing is gross:
if (!System.getenv("TERM").equals("xterm"))
{
System.out.println("\nWARNING: The TERM type is now set to xterm\n");
final String[] cmd = { "/bin/sh", "-c", "export TERM=xterm" };
Runtime.getRuntime().exec(cmd);
}
Would there be an issue here for terminals that don't support xterm? I notice that the Microsoft Telnet client doesn't allow you to set the TERM type to xterm before you begin a session. Once the session is started, however, setting TERM=xterm seems to solve the problem.
How do most console applications go about this issue?
With character terminal applications, there are always two ends in the communication, which must agree on how to interpret the control characters. Usually both sides are capable of using a variety of codings described in termcap/terminfo database.
On the Unix server-side you can define the coding by setting the TERM environmental variable, or by using stty (a default is used otherwise, often a dumb terminal emulation).
On the client side you also have to set the same terminal emulation as on the server side. Windows native telnet has the capability to define the emulation (see e.g. Configure the Telnet Terminal Type), as have other terminal emulators (e.g. Putty), too.
Regarding your design decisions: The above terminal setting are usually only described in user documentation, and not hardcoded in application, to leave more flexibility. After all, you do not know in advance which terminal (only a simple hardware terminal, supporting a single termcap coding, perhaps?) your users are going to use.
(As your question has little to do with Java or system.in, so you could re-consider the tags you use.)
You should look into these two posts, as they are related to what you are doing.
check env variable
set environment variable
As you are running the console on top of the Unix server, redhat in your case, I would also recommend that you look into the Unix command expect, that allows you to read the input in the console application, and performs the action according to the input of the user.
Here are some examples of the command usage.
sample Expect usages
I am looking for a way to mimic operating-system (Windows in specific) actions through Java. Preferably, the program should run in the background, but it is not a big deal if it does not. I got the background part covered thanks to this question. I was looking for the following specific features :
Maximizing/Minimizing the currently active window. (Can be any window, not just the Java application window.)
Closing the currently active window.
Open installed programs, and system utilities like the calculator, paint, etc. (I figured out this one from this question.)
Shutdown/Restart (This one's done too, thanks to the question here.)
So, my actual question is:
Is it possible to minimize/maximize or close an application window from a java program? (in Windows)
Example Scenario:
Firstly the java program is started, and it runs either as a background process or as a window. Bottom-line is that it should be able to accept triggers like maybe a keyboard shortcut or microphone input to trigger the action. After that suppose a Chrome window is opened and is currently active. Now on pressing the pre-defined shortcut, the Chrome window will minimize/maximize or close.
If the answer to the question is yes, I could use some pointers to start with my application. Thanks!
What you need is like an OS shell programming interface.
In Java side you will define a few interfaces.
Another Java layer will detect which OS is used and will return an implementation of interface: Windows, Linux, Macosx.
Some functionality you can have with simple bash command: in windows cmd, in linux .. to many. Eg shut down, launch MSPaint, Calculator.
Other functionality you can have it with windows API: you will need to write some JNI functions and call it. eg minimize, maximize. It is possible.
Edit:
I see there is no accepted answer, although it is answered properly.
Here is a C# code which does what you need in Java.
Now you need to migrate this code to Java:
In your java class declare a function:
private native maximizeOrMinimizeWindowWithName(String windowName, boolean maximize);
Compile -it
use Javah.exe - it will generate the necesary .h files
Use a C editor, configure environment, use the generated .h file.
-include windows api headers
-load user32.dll
- do more stuf..
compile your C code to .dll
put the your.dll into your app PATH environment variable. ( windows has the . in path, linux not)
-text, bugfix,
for more info you should see a basic JNI tutorials.
-upvote accept :)
This can be initiated from Java, but not actually implemented in Java. In other words, it will take a lot of platform-specfiic JNI library code to get it working.
Java will give you almost no benefit for your use case; you should avoid it altogether for this project.
You should look into Autohotkey. It's an system dedicated to simulate user programmaticly.
Using AH scripts you can easily access all open windows, installed programs and even control mouse and keyboard.
I am creating a program using Java Sockets in which I capture the client desktop and send messaging to client. Its working properly but now I want to block Client applications like Notepad, MS-Word, etc.
How can I do this?
Thanks.
It is hard to do using pure java API.
I do not know what do you mean when you say "block". The easiest way is to check from time to time running processes and kill one named "notepad" by executing taskkill from java.
If you wish to achieve effect of inactivity of application, i.e. user sees the notepad but cannot type you can do the following.
You have to check which application is on front. There is no clean pure java solution for this but you can probably write VBScript or JScript that does this task and run it from java. Once you detected that notepad is on top create transparent window (or even probably half-transparent window) that occupies full screen. Bring it on top. User will not be able to type into notepad because your window is on top but will see it.
Here is reference how to create transparent windows: http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/
Good luck.
If you know babylon translation tool you'd know its word capture feature - when you right click on a word - it tanslates it (from a browser or any documtent).
I want to get the same tool - what program language should i use ?
The os i want to get it work on is win-xp and ubuntu. and I'm writing my program in java.
if it could happen from a java program it would be great.
thanks,
Adi.
You are facing two (IMHO) insurmountable challenges:
Windows and Linux use completely different mechanisms for displaying text on screen. Translating mouse positions into actual text is pretty darned difficult. This is hard even if you're targeting a single operating system.
Java apps generally run in a sandbox, i.e., they can't just go mucking around in the OS asking other windows to tell them what text is under the mouse, nor can they override the default right-click action windows they don't control.
A better UI approach would be something that uses the system clipboard, which I think is available to Java on all systems (I'm not a Java guy, I'm not sure). So, the user would copy a word in any window, perhaps hit some sort of global shortcut key (again, assuming you can assign one in a Java app), and the Java app could access the word in the clipboard and do what it needs to do.