How to fire tab key event?

纵饮孤独 提交于 2020-01-02 02:14:08

问题


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.


回答1:


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.




回答2:


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

KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();



回答3:


You can use Robot class for this



来源:https://stackoverflow.com/questions/4546214/how-to-fire-tab-key-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!