思路是:
- 鼠标进入按钮使用Robot类执行ctr+c
- 按钮点击事件,从剪切板获取内存,设置到textField中
package sample;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import sample.main.scanNettyClient;
import sample.main.scanNettyClientHandler;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class scanResultController implements Initializable {
@FXML
private TextField searchtext;
@FXML
private GridPane resultgp;
@FXML
private ListView<?> resultlv;
@FXML
private VBox resultright;
@FXML
private Button hidde;
boolean showAndHide=false;
@FXML
private Button searchid;
private scanNettyClientHandler scanH;
@Override
public void initialize(URL location, ResourceBundle resources) {
searchid.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
try {
keyPressWithCtrlC();
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
}
}
});
new Thread(new Runnable() {
@Override
public void run() {
scanH=new scanNettyClientHandler();
scanNettyClient sc=new scanNettyClient();
sc.startScanNettyClient(scanH);
}
}).start();
}
@FXML
void searchidAction(ActionEvent event) throws AWTException, IOException, UnsupportedFlavorException {
Clipboard clip=Toolkit.getDefaultToolkit().getSystemClipboard();
DataFlavor flavor=DataFlavor.stringFlavor;
String s=(String) clip.getData(flavor);
searchtext.setText(s);
scanH.sendmsg(s);
}
@FXML
void hiddeAction(ActionEvent event) {
if (!showAndHide) {
resultright.setVisible(false);
GridPane.setColumnSpan(resultlv, 3);
GridPane.setRowSpan(resultlv, 3);
} else {
resultright.setVisible(true);
GridPane.setColumnSpan(resultlv, 2);
GridPane.setRowSpan(resultlv, 3);
}
}
public void keyPressWithCtrlC() throws AWTException, IOException, UnsupportedFlavorException {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(0x43);
r.keyRelease(0x43);
r.keyRelease(KeyEvent.VK_CONTROL);
r.delay(100);//非常重要
}
}