简体   繁体   中英

How can I catch key press in java while working with selenium geckodriver?

I was working with javafx and I could do something like this

scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent e) {
        System.out.println(e.getCode());
        //do something
    }       
});

to get all the key press while my app has the focus.

Is there a way to do the same while working with selenium? I dont want to send something to the browser, I just want, for example, if I press ctrl+q execute driver.quit() and close everything.

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent; 

    Robot rob = new Robot();
    rob.keyPress(KeyEvent.VK_CTRL); 
    rob.keyPress(KeyEvent.VK_Q); 
    rob.keyRelease(KeyEvent.VK_CTRL); 
    rob.keyRelease(KeyEvent.VK_Q);

should do the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM