Can a Program Differentiate between User Key Input and Program key Input? - java

Is it possible for a program to block key input from other programs but NOT key input directly from the user's hand hitting a key on the keyboard?
For example:
ProgramA contains a text field .
ProgramB is Windows On-Screen keyboard.
After selecting the text field in ProgramA, I attempt to press the letter 'c' on the On-Screen Keyboard program but it does not appear in the text field.
However, when I press the letter 'c' directly from my keyboard it appears within the text field of ProgramA.
I know how key events work, how they are queued, and I've seen the BlockInput Function in c++
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646290%28v=vs.85%29.aspx
But that function simply blocks all input.
Ive encountered programs that filter the input in this way and can't seem to figure out how. Am I missing something completely obvious?
This question is mainly pertaining to c++ on the Windows 7 operating system.

You can never be sure if input is coming from hardware. The simplest example is a custom driver wich will simulate real keyboard driven by software input.
Some fancy idea can be attaching microphone to device, to listen for sounds/vibrations for input filtering, but do you mean complete hardware solution or application, wich will be given to personal users?

Related

How to make PrintWriter write in log-in screen or in any text box I choose in windows 10

I am writing a java program to print password in log-in page of windows 10 (to get automatically login when I do some action)
I try to use printwriter to do that .
I managed to print the text inside a text file .
but I want to print the text in password's text box of login page in windows 10
any idea please ..
Thanks
import java.io.*;
class Hasan{
public static void main(String[] args)
try {
PrintWriter writer=PrintWriter(System.out);
writer.write("mypassword");
}
catch (Exception e) {
System.out.println(e);
}
}
}
when I used System.out I just could printed text in console screen
That's not going to work. While you can use for example java.awt.Robot to fill out things when the desktop is unlocked and your program is running, the login screen is a whole different thing. For security reasons you won't be able to get any custom programs to do the logging in for you. At least not with Java, and not without root access.
PrintWriter is designed to print a sequence of bytes on a stream. UI widgets, like textboxes, have no stream support.
If you want to target a textbox owned by a process in the same security context as yours, you have to identify the process owning the window and use the OS primitives (SendMessage, PostMessage, ...) to enter your text.
Processes outside your security context are unreachable and at least require that your process is elevated.
Note that certain textboxes may refuse certain types of message when they are working in ''password'' mode, i.e. masking the input. This is to prevent applications from stealing the context of the textbox.
A more wise solution, would be to let the user set the focus on the UI control they wants, and then let them pressing a keyboard hotkey to trigger your program. Your program can then use use SendInput to send the keyboard characters one by one as if the user was typing.
Both the solutions are feasable in native language (C, C++) or PInvoked (C#), for java, have a look at Sending a Keyboard Input with Java JNA and SendInput().

Java CLI App - keep input from scrolling up

I get that this isn't possible to do with normal java, although if there are any libraries out this it would be very useful.
Essentially, I'm designing a console app and running into an issue that when output happens while something is typed in the input line, that input text will move up and appear before the line that just got output. Is it possible to fix this in some form so that the text you are inputting that stays at the bottom?
EX:
I'm typing something as input into my commandline app, and then the program prints something WHILE I'm typing - this causes what was originally on the input line to be scrolled up with whatever the output text was. When you are trying to type something in this can obviously be detrimental. I know it's possible to prevent this.. (Other programs have done it... EX: Minecraft Server)
(If I need to be more descriptive I can.)
You could use the help of threads. One that listens to user input, the other process the actual output. This problem is similar to basic race condition problems when multiple threads attempt to read and write to a shared resource.
Your shared resource is that console. You need to keep the Input/Output operations synchronized. Have a look at race condition.

Command-line interface in Java

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.

Java Stream Keyboard Input

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.

Send string from java (or other platform) to application

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!

Categories