Chris
Chris

Reputation: 1363

Press a key with Java

I was wondering if it is possible to press a key with Java. Not quite sure how to approach this. There must be some type of class that has like sendKeyPress(); or something.

Upvotes: 15

Views: 52769

Answers (2)

shatakshi shukla
shatakshi shukla

Reputation: 1

Action act = new Action(driver);
act.sendkeys(Keys.ENTER).build().perform();

Upvotes: 0

Petr Janeček
Petr Janeček

Reputation: 38424

You can do it easily with the Robot class. That just virtually presses the button, with no special targeting or anything.

For example, to press Enter:

Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);

Upvotes: 23

Related Questions