发送命令进行处理

时间:2015-10-25 11:44:37

标签: java firefox

我想创建一个启动Textfield的程序。 在Textfield中,我可以编写将要启动的命令 另一个过程。

实施例: Textfield:"打开firefox olaf" 然后程序启动firefox。奥拉夫就是那个信号 命令结束。

我的程序比较Textfield String数组 使用命令String数组,例如 这个词"打开"在两个字符串数组中都相等 在开关案例方法中,以下方法"打开"。 这很好。

我的问题: 现在我想要这样的命令: Textfield:"输入firefox www.web.de olaf" 该程序应该专注于Firefox,并应该在Firefox中输入 " www.web.de",但我不知道该怎么做。 我不想用机器人课 robot.keyPressed()

此外,我想输入网站的文本字段。

为什么我这样做? 我只想学习java并使用语音演讲,所以我不需要键入"打开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
    }

} 

1 个答案:

答案 0 :(得分:0)

您可以使用命令行参数来实现此目的。

./ firefox www.stackoverflow.com

所以你可以使用ProcessBuilder传递它:

String urlAddress = someJTextField.getText();
ProcessBuilder pb = new ProcessBuilder("/firefox", urlAddress);
pb.start();