2016-11-21 65 views
1

當記事本中的文本通過Ctrl + C複製時,我想自動將內容放入textArea。我想知道如何聽記事本的鑰匙。是否有可能聽記事本或任何其他程序的鑰匙?Keylistener從另一個程序的Ctrl + C

+2

收聽到剪貼板來代替。 http://stackoverflow.com/q/14226064/2855515 – brian

+0

全球可以聽到「Ctrl + C」。它被稱爲鍵盤記錄器,它絕對沒有你想在你的Java應用程序中實現的東西。正如@brian已經指出的,更好的方法是聽取系統剪貼板。 – Paul

回答

2

這是在其他的答案和評論中引用的剪貼板聽衆的一個JavaFX版本:

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); 
    } 
} 
0

不,不可能在java中獲得所有應用程序的所有keyevent。

所以你有2個可能的解決方案。

  1. 您只能通過剪貼板偵聽器獲取剪貼板的更改。 SO example

  2. 使用JNA,JNI,jintellitype,...的聽系統的KeyEvents SO example