java: how to send accelerator key
I am using Junit4 under eclipse.
I would like to write a test which can be able to send the action : ctrl+shift+P
I tried this using JTable as I don't know for which component I could use the sendAccelerator开发者_运维百科Key
:
myTable.sendAcceleratorKey(InputEvent.CTRL, InputEvent.SHIFT_DOWN_MASK)
but I can't add a third argument to say KeyEvent.P
.
How can I send this action which changes the menu?
Thanks!
I guess you can use the Robot class.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_P);
Thread.sleep(1000); // Time for your code to react to the event
assert(...);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_P);
I can't find any reference to sendAcceleratorKey()
. But if it really exists and it does what you want, it looks logic to me to use the the method this way, using key modifiers:
myTable.sendAcceleratorKey(InputEvent.CTRL | InputEvent.SHIFT_DOWN_MASK,
KeyEvent.VK_P);
Otherwise, try to swap the parameters, depending on the methods signature.
myTable.sendAcceleratorKey(KeyEvent.VK_P,
InputEvent.CTRL | InputEvent.SHIFT_DOWN_MASK);
精彩评论