Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire tab key event?

Tags:

java

swing

awt

How do we fire a tab key pressed event deliberately in Java? I also want to know how to fire a Shift + tab key pressed event programmatically in Java.

like image 435
sasidhar Avatar asked Dec 28 '10 12:12

sasidhar


2 Answers

The following example shows how to simulate mouse and key presses in Java using java.awt.Robot class.

try {
    Robot robot = new Robot();
    
    // Simulate a mouse click
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    
    // Simulate a key press
    robot.keyPress(KeyEvent.VK_SHIFT);
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_SHIFT);
} catch (AWTException e) {
    e.printStackTrace();
}

Edited my post to do the SHIFT + TAB Key Press.

like image 104
LaGrandMere Avatar answered Oct 01 '22 13:10

LaGrandMere


If what you really want is just to navigate to the next component, you can do:

KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
like image 35
Russell Zahniser Avatar answered Oct 01 '22 13:10

Russell Zahniser