4

I'm using accelerator to perform CTRL+C then CTRL+V using java/junit is there a way to get the value of CTRL+V to check it ?

xav
  • 5,452
  • 7
  • 48
  • 57
lola
  • 5,649
  • 11
  • 49
  • 61

2 Answers2

1

If you mean how do I simulate the ctrl+V and ctrl+C events within a JUnit test for a Swing application, then I would recommend looking at FEST. Using FEST, you can simulate mouse clicks, or key presses. To simulate a ctrl+V, you would do:

// import static java.awt.event.KeyEvent.*;          
dialog.list("employees").pressKey(VK_CONTROL)
                        .pressAndReleaseKey(VK_V)
                        .releaseKey(VK_CONTROL);

and so on. For more information on simulating user input, see the wiki Simulating Keyboard Input.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
1

As mentioned here, a menu accelerator key stroke such as Ctrl+V should be constructed in a platform independent manner:

int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JMenuItem menuItem = new JMenuItem(…);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, mask));

For comparison, you can get the menu item's KeyStroke via getAccelerator() or from any KeyEvent via KeyStroke.getKeyStrokeForEvent().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045