简体   繁体   中英

How to fire tab key event?

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.

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.

如果您真正想要的只是导航到下一个组件,您可以执行以下操作:

KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();

您可以为此使用Robot

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