Keyboard shortcut using Keys - java

apologies for my basic question but I am a kinda java newbie and I would love to write a code that will do a keyboard shortcut CTRL + A.
I have imported:
import org.sikuli.hotkey.Keys;
import org.sikuli.script.Key;
and wrote a variety of codes similar to this:
Keys.CTRL + KeyEvent.VK_A / Keys.A / ...
Unfortunately, I did not manage to make it work..
I have two issues:
how to make it work as a keyboard shortcut
how to add code of an "A"
I have read about the modifiers and tried to find a solution here but without a bit of luck.
I was thinking about adding sth like KeyPress and KeyRelease but idk how to make it work.
Any suggestions?
Thank you in advance!

for anyone who is trying to do the same - I think I have found a solution:
import java.awt.*;
import java.awt.event.KeyEvent;
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_A);
r.keyRelease(KeyEvent.VK_A);
r.keyRelease(KeyEvent.VK_CONTROL);
} catch (AWTException ex) {
// Exception
}

Related

How to press a keyboard key without a physical keyboard in selenium

I'm trying to build an automation script that will install a chrome extension.
On my local system (windows 10) all works fine while using Robot class with java, since I have a physical keyboard connected to my computer.
The problem is - when I try to run this automation on a virtual machine(Amazon EC2, windows server), the Robot class is not working because it doesn't detect a physical connection of a keyboard.
Is there any other way to simulate a keyboard stroke without a keyboard attached?
FYI, I have to use the keyboard because google install box is not part of the page and selenium wont recognize it.
I've tried the sendKeys function but it didn't work because it will affect only the webpage itself and not pop outside of the page
I believe you can use java robot functions to mimic the keyboard interactions.
Example:
package org.kodejava.example.awt;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class CreatingKeyboardEvent {
public static void main(String[] args) {
try {
Robot robot = new Robot();
// Create a three seconds delay.
robot.delay(3000);
// Generating key press event for writing the QWERTY letters
robot.keyPress(KeyEvent.VK_Q);
robot.keyPress(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_Y);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
I don't think you can do this with Selenium, cause it is meant to test webpages, not to automate a human-computer interaction.
If you want to automate a complex scheme like this, you may try a more complete solution, like UiPath :
https://www.uipath.com/
This is a solution meant for automation, so it will give you more tools to achieve your goal. It has a community edition which is free, and an active forum, so you should be able to handle it quickly !

Java robot class mouseMove not going to specified location

To be honest I am not entirely sure what is wrong. This is the short version of a ton of other basic robot command movements under the if and if else.
Whenever I run the program the mouse should move to the designated position and click. However when I run the program it does not move to the position I indicated, instead it moves to a different position each time I run it(I do not have any listeners designated to change the position so the position shouldn't be changing). I do not know if it is something with the code I have written itself or possibly my imports? The program was running correctly until recently in which I added the else at the end to end the program, I have run it without the else and still come up with the same issue. Any help would be much appreciated.
package creator;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
public class RobotDemo extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
public static void main(String[] args) throws AWTException, IOException
{
double value = (-0.66721);
{
if (value < -0.3)
{
Robot robotdelta = new Robot();
//Enters Chrome from java
robotdelta.delay(5000);
robotdelta.mouseMove(587, 1045);
robotdelta.delay(1000);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
//Enters online platfrom
robotdelta.mouseMove(770, 21);
robotdelta.delay(1000);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
}
//secondary situation
else if (value > 0.3)
{
Robot robotdelta = new Robot();
//Enters Chrome from java
robotdelta.delay(1000);
robotdelta.mouseMove(587, 1045);
robotdelta.delay(100);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(100);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(100);
//Enters online platfrom
robotdelta.mouseMove(770, 21);
robotdelta.delay(100);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(100);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
}
else
{
system.exit(0);
}
}
}
public void actionPerformed(ActionEvent e) {
}
}
As a disclaimer, I was playing around with this class for a while, and the most important thing I learned was this was a tool meant for very rudimentary testing, and really no large scale crucial operation should ever hinge on this class working exactly as expected.
To answer your question, there is really no way to get exactly where you tell mouseMove() to go (at least not when I was working with it). However, what seemed to get it pretty close was to call mouseMove() multiple times to the same place (Yes, this is very hacky and not desirable). For example, is I wanted to move the mouse to (300,600) on screen, I found that if you do:
mouseMove(300,600);
mouseMove(300,600);
mouseMove(300,600);
// ... can put more if you want
for some strange reason it gets much closer to where you want to go than if you just call mouseMove() one time. I have no idea why this might be the case, but hopefully this helps. Not to mention, it is also a good idea to put ample delays in between calling the robot to do different actions, and to ensure that waitForIdle() is invoked.

How to clear netbeans output with code

I was wondering if there was some sort of command I could output that would clear the netbeans output window? I just want the current output to clear, but still have the ability to output more after clearing at run time. Something similar perhaps to BlueJ's:
System.out.print('\u000C');
You asked:
I was wondering if there was some sort of command I could output that would clear the netbeans output window?
No, not with a standard console. To do this you need to create either a Swing GUI and clear your text component, use another GUI such as SWT, or use a non-standard 3rd party console.
If you use Netbeans,
use this method:
public void clear() throws AWTException {
Robot rob = new Robot();
try {
rob.keyPress(KeyEvent.VK_CONTROL); // press "CTRL"
rob.keyPress(KeyEvent.VK_L); // press "L"
rob.keyRelease(KeyEvent.VK_L); // unpress "L"
rob.keyRelease(KeyEvent.VK_CONTROL); // unpress "CTRL"
Thread.sleep(1000); // add delay in milisecond, if not there will automatically stop after clear
} catch (InterruptedException e) {
}
}
add import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

java console mouselistener

I am trying to make a mouse recorder, I cant seem to get a mouse listener to work with a console, is this possable and how would I go about it Thanks.
Unless you wrote your own console that fired mouse events, I dont' think you're going to be able to do it. What widget are you going to register your mouselistener against otherwise? The console isn't a swing component, therefore, no swing events.
You can do this by using global hooks.
In order to use them you'll need to include some natives or try the same using JNI (see: wikipedia).
Two examples:
http://kra.lc/blog/2011/07/java-global-system-hook/(works well - I would advice to re-indent the c++ content and if you know how to do, merge it into 1-2 files - your eyes will thank you)
http://www.jotschi.de/Technik/2008/01/06/java-global-keyboard-hook-jni.html(never tried, but looks more simple)
Edit:
Example for some playback functionalities:
import java.awt.AWTException;
import java.awt.DisplayMode;
import java.awt.MouseInfo;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.util.Random;
// class instructions
try {
PointerInfo pntInfo = MouseInfo.getPointerInfo();
DisplayMode dispMode = pntInfo.getDevice().getDisplayMode();
int newX = new Random().nextInt( dispMode.getWidth() );
int newY = new Random().nextInt( dispMode.getHeight() );
new Robot( pntInfo.getDevice() ).mouseMove( newX, newY );
} catch ( AWTException exception ) { }
Sorry for my late answer ;)

Control a Windows apps using Java

I would like to know if there is any way I can control a Windows application using Java code. I have already googled it, and found that it can be done using JNI or a library called NewJawin.
I want to control Windows Media Player using Java code, e.g. play, pause, and change songs, but could find no relevant example to get me started so far. Do you guys have any suggestion?
As no one has answered this question, I thought I would.
public void firePlay() {
//CTRL + P
//import java.awt.Robot
//import java.awt.KeyEvent
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_P);
robot.keyRelease(KeyEvent.VK_P);
robot.keyRelease(KeyEvent.VK_CONTROL);
} catch (AWTException ex) {
Logger.getLogger(atest.class.getName()).log(Level.SEVERE, null, ex);
}
}
This would play/pause the video. You can see other shortcuts here(http://windows.microsoft.com/en-AU/windows-vista/Windows-Media-Player-keyboard-shortcuts)

Categories

Resources