PDA

View Full Version : [Java] Simulare la pressione di un tasto


agente mm8
08-11-2008, 21:11
E' possibile in java simulare la pressione di un tasto (tastiera e/o mouse)?
Come?
Grazie per l'aiuto.

fbcyborg
09-11-2008, 09:37
Ciao,

per quanto riguarda la tastiera, so che si può fare con KeyEvent. A rigor di logica (ma quì non sono sicuro), dovresti poter emulare anche un evento del mouse utilizzando MouseEvent.

frizzo28
09-11-2008, 14:49
Preso da qui:http://www.java-tips.org/java-se-tips/java.awt/simulating-mouse-and-key-presses.html

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_A);
robot.keyRelease(KeyEvent.VK_A);
} catch (AWTException e) {
e.printStackTrace();
}

ciao :)

agente mm8
09-11-2008, 16:08
Thanks thanks thanks thanks!!!!!
Grazie 1000 a tutti.

Edit: Solo una cosa: il metodo r.mousePress(int) vuole in input un intero che rappresenta il pulsante da premere; ho cercato nella classe MouseEvent, ma non ho trovato risposta.
0 vale per il tasto sx, 1 per il centrale, e 2 per il dx? Oppure no?
E poi il metodo r.mouseWheel(int) serve per far "scrollare" la rotellina del mouse, vero?
Illuminatemi please.

frizzo28
09-11-2008, 17:33
No...non bisogna passargli un intero ma come c'e' scritto InputEvent.BUTTON1_MASK
dai un occhiata qui...:)
Q: More about Robot! I met with a problem in using class Robot.mousePress...

The compiling process is successful. But when I run it, I receive

"IllegalArgumentException:

Invalid combination of button flags". I don t quit understand this information. Part of

my code is as following:

Robot rMouse=new Robot();

int button=1;

rMouse.mousePress(button);

rMouse.mouseRelease(button);

I am really confused. Will you please give me some advice? Thank you in advance!

Answer: You are not using a valid value for the argument to the mousePress() and

mouseRelease() methods. If you check the API documentation, you ll find the valid

values are a combination of one or more of the following constants:

InputEvent.BUTTON1_MASK

InputEvent.BUTTON2_MASK

InputEvent.BUTTON3_MASK

plus others which represent the Ctrl, Alt, and Shift keys. To press the left

mouse button, you want to use:

rMouse.mousePress(InputEvent.BUTTON1_MASK);


Edit: qui c'e' un esempio molto esplicativo http://www.java2s.com/Code/JavaAPI/java.awt.event/InputEventBUTTON1MASKmouseleftbutton.htm

P.S. Si MouseWheel e' per far scrollare la rotellina... leggi qui http://jfcunit.sourceforge.net/api/junit/extensions/jfcunit/RobotTestHelper.html#mouseWheel(java.awt.Component,%20int,%20int)

agente mm8
09-11-2008, 19:22
Ok, grazie 1000.

banryu79
10-11-2008, 12:01
No...non bisogna passargli un intero ma come c'e' scritto InputEvent.BUTTON1_MASK

Senza offesa, ma il metodo prende in ingresso proprio un intero.
Il fatto che poi BUTTON1_MASK sia definito come un intero statico nella classe InputEvent è una questione di coerenza e chiarezza, ma resta pur sempre un intero :)

frizzo28
11-11-2008, 15:36
Senza offesa, ma il metodo prende in ingresso proprio un intero.
Il fatto che poi BUTTON1_MASK sia definito come un intero statico nella classe InputEvent è una questione di coerenza e chiarezza, ma resta pur sempre un intero :)

Hai ragione....:) avevo letto troppo frettolosamente...:)