2010-12-28 11 views

risposta

14

L'esempio seguente mostra come simulare il mouse e tasti premuti in Java utilizzando java.awt.Robot classe.

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(); 
} 

Modificato il mio post per eseguire MAIUSC + tasto TAB Premere.

+0

Grazie mille, scheda può essere gestito da VK_TAB, buthow faccio a generare "shift + tab".? – sasidhar

+1

Maiusc + tab è premuto 'SHIFT', quindi' TAB' premuto, quindi questi tasti rilasciati –

+0

@sasidhar @Valentin: Valentin è stato più veloce di me, thx e +1 :) Ho modificato il mio post per fare uno SHIFT + TAB KeyPress . VK_TAB per Tab e VK_SHIFT per Shift Key Press. Puoi trovare tutti i KeyEvents qui: http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/KeyEvent.html – LaGrandMere

3

È possibile utilizzare Robot classe per questo

4

Se ciò che si vuole veramente è solo per passare alla componente successivo, si può fare:

KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(); 
Problemi correlati