Console does not appear on Robot - java

I'm creating a utility console that does some basic key pressing for me.
But for some reason, the console view does not appear, but it is still doing its job.
This is the piece of code:
try {
Robot robot = new Robot();
int x =1;
for(int i = 0;i<10;i++){
delay();
System.out.println("Processing Bot...");
}
do{
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
delay();
System.out.println("Key Active : Tab");
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
for(int i = 0;i<10;i++){
delay();
}
System.out.println("Key Active : 1");
robot.keyPress(KeyEvent.VK_2);
robot.keyRelease(KeyEvent.VK_2);
for(int i = 0;i<10;i++){
delay();
}
System.out.println("Key Active : 2");
}while(x!=0);
...
}
How do I make the console appear, because it is hard closing the apps from here
Any help would be appreciated :)
Edit:
This is the delay code :
public static void delay(){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Related

If Radio box is selected, switch between two clicking methods

So i have this code here, and it quite literally doesn't work and i just don't know why. It doesn't click, doesn't print out anything. My goal is to make the buttons function as switches between two methods of clicking. Right mouse button clicks, and Left mouse button clicks. Can anyone tell me why this doesn't function?
if (rbRightClickRadioButton.isSelected()) {
System.out.println("RMB Clicker");
Robot clicker = null;
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
Thread.sleep(delay);
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
} else if (rbRightClickRadioButton.isSelected()) {
System.out.println("LMB Clicker");
Robot clicker = null;
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
Thread.sleep(delay);
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
If you want a button to work as a switch between two options, I would rather use checkbox than radiobutton. The main problem your code has right now is the simple fact that you check the same expression in your if statements.
You should either check if the other radiobutton is selected in your else if statement or just assume that if the rbRightClickRadioButton is not selected, then the other one is, and you don't use else if just a simple else.

How to drag and drop in selenium when there are multiple elements

The following code is not working. The reportDataFields displays a list of items(for ex abc, abd, abe) and I want to select abc and drop into the target. It does not fisplay any error message either.
Actions action = new Actions(driver);
List<WebElement> reportFields = driver.findElements(By.className("reportDataFields"));
WebElement target = driver.findElement(By.id("rptDataSections"));
for (int i = 0; i < reportFields.size(); i++) {
if (reportFields.get(i).getText().equals(Section)) {
action.dragAndDrop(reportFields.get(i), target).release().build().perform();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I think you should create a new instance of Actions interface every time you use it.
Try below code with my personalized drag and drop functionality:
List<WebElement> reportFields = driver.findElements(By.className("reportDataFields"));
WebElement target = driver.findElement(By.id("rptDataSections"));
for (int i = 0; i < reportFields.size(); i++) {
if (reportFields.get(i).getText().equals(Section)) {
WebElement draggedFrom = reportFields.get(i);
new Actions(driver)
.moveToElement(draggedFrom)
.pause(Duration.ofSeconds(1))
.clickAndHold(draggedFrom)
.pause(Duration.ofSeconds(1))
.moveByOffset(1, 0)
.moveToElement(target)
.moveByOffset(1, 0)
.pause(Duration.ofSeconds(1))
.release().perform();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

EDIT - Java while loop means events aren't called

So I've updated my code with the help of Cruncher, and now the clicker appears to work better. However whilst the while(pressed) loop is running, no other events are called & so it stays running.
public class Function implements NativeMouseListener {
private Robot robot;
private boolean pressed = false;
private boolean skip = false;
public Function()
{
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
private void repeatMouse()
{
skip = true;
robot.mouseRelease(InputEvent.BUTTON1_MASK);
while (pressed)
{
System.out.println("pressed while loop " + pressed);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public void nativeMouseClicked(NativeMouseEvent nativeMouseEvent) {
}
#Override
public void nativeMousePressed(NativeMouseEvent nativeMouseEvent) {
System.out.println("GG");
if (!(nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1)) {
System.out.println("Returned.");
return;
}
if (!Native.get().getData().getEnabled())
{
System.out.println("Isn't enabled.");
return;
}
pressed = true;
repeatMouse();
}
#Override
public void nativeMouseReleased(NativeMouseEvent nativeMouseEvent) {
System.out.println("released");
if (!(nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1)) {
System.out.println("Returned 2");
return;
}
if (!skip)
{
System.out.println("pressed " + pressed);
pressed = false;
System.out.println("pressed " + pressed);
} else {
skip = false;
}
}
}
Any idea why the while loop would stop events from being called? Do I need to use multi threading or some of that jazz?
Thank you.
For one, your main method is not included in your code, but I assume it contains the following line(or similar):
new Project()
try {
bot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
while (pressed) {
bot.mousePress(InputEvent.BUTTON1_MASK);
//bot.mouseRelease(InputEvent.BUTTON1_MASK);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Now let's look at this code. When this runs, pressed will be false at the beginning(presumably), and it will just exit and not be running on later clicks.
What you want to do is have your loop started when you register a click. Let's move it into another method
private void repeatMouse() {
bot.mouseRelease(InputEvent.BUTTON1_MASK);
while (pressed) {
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Now let's call it in your mouse down native hook
#Override
public void nativeMousePressed(NativeMouseEvent nativeMouseEvent) {
if (nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1)
{
pressed = true;
System.out.println(pressed);
repeatMouse();
}
}
EDIT:
It appears your other problem is that after the first mouseRelease, the handler will get called from the native library. I have a potential solution for this.
First next to where you define your pressed variable, define a new skipRelease
boolean skipRelease = false;
Then before every call to mouseRelease, first set skipRelease to true. Then change your mouseRelease handler to the following
#Override
public void nativeMouseReleased(NativeMouseEvent nativeMouseEvent) {
if (nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1)
{
if(skipRelease) {
skipRelease = false;
return;
}
pressed = false;
System.out.println(pressed);
}
}

Is there any way to control mouse through java?

I want to control mouse by robot class. Just move to left or right. Meanwhile I run eclipse with administrator status. Here is my code below.
public class ControlMouse {
public static void main(String[] args) {
try {
Robot robot = new Robot();
Thread.sleep(1000);
robot.mouseMove(0, 0);
Thread.sleep(1000);
robot.delay(1000); // this one is the same...
robot.mouseMove(100, 100);
} catch (Exception e) {
System.out.println("e = " + e.toString());
} catch (Error e) {
System.out.println("e = " + e.toString());
}
}
}
But it seems doesn't work and no error or exception message. Dose any one may give me some advice? Thank you. The development environment is window 7. And I expect I can see the mouse cursor or traces will change. But I can’t.
You will have to put some delay and then check. without delay / sleep, sometimes GUI elements cannot be handled properly. You might even miss the event.
class ControlMouse {
public static void main(String[] args) {
try {
Robot robot = new Robot();
Thread.sleep(1000);
robot.mouseMove(0, 0);
Thread.sleep(1000);
robot.mouseMove(100, 100);
} catch (Exception e) {
System.out.println("e = " + e.toString());
} catch (Error e) {
System.out.println("e = " + e.toString());
}
}
}
The above code will work. It will move to 0,0 and then to 0,100

Robot class - If a Button Is Pressed?

I have read and understood how the Robot class in java works. Only thing I would like to ask, is how do I press and release the mouse button inside an if statement. For example I would to make a click only if (and right after) the space button is pressed/released. I would use the code:
try {
Robot robot = new Robot();
if (/*insert my statement here*/) {
try {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (InterruptedException ex) {}
}
} catch (AWTException e) {}
Unfortunately, there isn't a way to directly control hardware (well, in fact there is, but you would have to use JNI/JNA), this means that you can't simply check if a key is pressed.
You can use KeyBindings to bind the space key to an action, when the spacebar is pressed you set a flag to true, when it's released you set that flag to false. In order to use this solution, your application has to be a GUI application, this won't work with console applications.
Action pressedAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
spaceBarPressed = true;
}
};
Action releasedAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
spaceBarPressed = false;
}
};
oneOfYourComponents.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "pressed");
oneOfYourComponents.getInputMap().put(KeyStroke.getKeyStroke("released SPACE"), "released");
oneOfYourComponents.getActionMap().put("pressed", pressedAction);
oneOfYourComponents.getActionMap().put("released", releasedAction);
Then, use
try {
Robot robot = new Robot();
if (spaceBarPressed) {
try {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (InterruptedException ex) {
//handle the exception here
}
}
} catch (AWTException e) {
//handle the exception here
}
As GGrec wrote, a better way to do it would be to execute your mouse press directly when the keyboard event is fired:
Action pressedAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (InterruptedException ex) {
//handle the exception here
}
}
};
My suggestion is that you listen for the keyboard event, and when you receive it, you execute your code without the if statement. Add the listener to your canvas, or whatever.
Careful not to recreate the Robot class each time.
new KeyAdapter() {
#Override
public void keyReleased(final KeyEvent e) {
if (e.keyCode == KeyEvent.VK_SPACE)
try {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (InterruptedException ex) {
}
}
}

Categories