I want to automatically put the contents into a textArea when a text in notepad is copied via Ctrl+C. I'm wondering of how to listen to the Keys of notepad. Is it possible to listen to Keys of notepad or any other program?
Asked
Active
Viewed 425 times
1
-
2Listen to the clipboard instead. http://stackoverflow.com/q/14226064/2855515 – brian Nov 21 '16 at 15:09
-
Listening to `Ctrl+C` globally is possible. It's called a keylogger and it's definitely nothing you want to implement in your java-application. As @brian already pointed out, the better approach is to listen for the system-clipboard instead. – Nov 21 '16 at 15:12
3 Answers
2
Here is a JavaFX version of the clipboard listeners referenced in other answers and comments:
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.input.Clipboard;
import javafx.stage.Stage;
public class SystemClipboardWatcher extends Application {
@Override
public void start(Stage primaryStage) {
TextArea clipboardView = new TextArea();
AnimationTimer timer = new AnimationTimer() {
Clipboard systemClipboard = Clipboard.getSystemClipboard();
@Override
public void handle(long timestamp) {
String content = systemClipboard.getString();
// do anything you need with this, e.g.:
clipboardView.setText(content);
}
};
timer.start();
primaryStage.setScene(new Scene(clipboardView, 600, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

James_D
- 201,275
- 16
- 291
- 322
0
No, it is not possible to get all keyevents of all application in java.
So you have 2 possible solutions.
You only get the changes of the clipboard, with a clipboard-listener. SO example
Use of JNA, JNI, jintellitype, ... to listen to the system-keyevents SO example
0
It is possible but a very bad coding practice. If you still want to do it: https://github.com/kwhat/jnativehook.
A way better idea would be to watch for changes in the clipboard.

Grunzwanzling
- 443
- 4
- 14