[英]send commands to process
我想創建一個啟動Textfield的程序。 在文本字段中,我可以編寫將啟動另一個進程的命令。
示例:文本字段:“ open firefox olaf”然后程序將啟動firefox。 Olaf是命令結束的信號。
我的程序將Textfield String
數組與命令String
數組進行比較,例如,如果單詞“ open”在兩個字符串數組中均相等,則可以在切換案例方法中運行以下方法“ open”。 多數民眾贊成在很好。
我的問題:現在我想要一個這樣的命令:文本字段:“鍵入firefox www.web.de olaf”該程序應專注於firefox,並鍵入firefox“ www.web.de”,但我不知道如何去做這個。 我不想將機器人類與robot.keyPressed()
我也想輸入網站的文本字段。
我為什么要這樣做? 我只是想學習Java並使用語音語音,所以不需要輸入“ open firefox olaf”。 我可以說;)
如果我需要使用C ++可以
import java.awt.BorderLayout;
import java.awt.Robot;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class KeyEventClass extends JFrame implements KeyListener, FocusListener {
private static final long serialVersionUID = 1L;
public KeyEventClass(){
this.setLayout(new BorderLayout());
JTextField field = new JTextField();
field.addKeyListener(this);
this.add(field, BorderLayout.CENTER);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.addWindowListener( new WindowAdapter() {
public void windowActivated( WindowEvent e ){
field.requestFocus();
field.setText("");
}
});
}
String[] Commands = {"open"};
String[] Input = {"","","","","","","","","","","","","","",""} ;
int i = 0;
// Save the input in the String[] Input Variable but without VK_SPACE
public void keyPressed(KeyEvent e){
if(i > 20) { System.out.println("Error Index over..");}
if(e.getKeyChar()==KeyEvent.VK_SPACE){
i++;
}
if( e.getKeyChar() != KeyEvent.VK_SPACE ) Input[i] += e.getKeyChar();
}
// Compare Input and Commands
public void keyReleased(KeyEvent e) {
if( Input[i].compareTo("olaf")==0){ // olaf is a Signal of the end of the command line
for(int k =0; k< Commands.length;k++){
for(int z=0; z < Input.length;z++){
if(Input[z].compareTo(Commands[k])==0){
switch(k){
case 0: open(Input[z+1]); //after start follow the name of the process
System.out.println("case0 = open");
delete();
break;
}
}
}
}
}
}
public void open(String process){
try {
Robot robot= new Robot();
ProcessBuilder pb = new ProcessBuilder("cmd", "/c",
"C:\\Verknupfung\\"+process+".lnk"); //"C:\\Verknupfung\\" is a Path to start Process
pb.start();
robot.delay(1000);
this.requestFocus(); //set focus on KeyEventClass after start process
} catch (Exception x) {System.out.println("Error to starts process:"+x);}
}
public void delete(){
for(int k=0;k< Input.length;k++) Input[k] = "" ;
i=0;
}
public static void main(String[] args) {
new KeyEventClass();
}
public void keyTyped(KeyEvent e) {
}
@Override
public void focusGained(FocusEvent arg0) {
}
@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
}
}
您可以使用命令行參數來實現。
./firefox www.stackoverflow.com
因此您可以使用ProcessBuilder將其傳遞給:
String urlAddress = someJTextField.getText();
ProcessBuilder pb = new ProcessBuilder("/firefox", urlAddress);
pb.start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.