function keys jsch (Java) - java

I use jsch for connect to a router Cisco and execute somme commands. But in some cases it ask me to press function keys as F1, F2...
I want to know if this is possible and how?
Thanks a lot!!!

Check Robot and KeyEvent. The second class has static int fields representing almost every key on the keyboard. F1 is VK_F1, F2 is VK_2 ... and so on.
To send a 'F1' key stroke use this :
Robot robot = new Robot();
robot.keyPress(VK_F1);

Related

How to close a dialog box using Java, that pops up during runtime?

I am trying to make a project that performs OCR (image to text conversion).
I am using the AspriseOCR jar file and i set up my project accordingly.
It works fine.
Whenever i run the project, when the call is made to aspriseOCR API, a dialog box pops up in windows asking if i want to go to their website. I need to automate the whole process. So i dont want this dialog box to appear.
Is there any way by which i can close this dialog box in my code itself, in case it appears? Is there a way i can press the Enter button (it will close accordingly). I am using Java, Eclipse.
Note: This is not on web browsers. So I cannot use Selenium related commands.
Edit:-
I tried to use this :-
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
But it wouldn't solve my problem as I need to add this in the API source code, which I do not have access to. Is there any other approach or method to close the dialog box?
So a part of the code is :-
`{
String s = ocr.recognize( new File[] {new File("C:\\work\\"+file)},
Ocr.RECOGNIZE_TYPE_ALL, Ocr.OUTPUT_FORMAT_PLAINTEXT);
Robot robot;
robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
System.out.println("\n\n pressed enter\n\n");
System.out.println("Result:\n\n"+s);
ocr.stopEngine();
}`
So when the control goes to the function ocr.recognize(...) the dialog box comes up and I do not have access to that source code which is present in that jar file.
Is there any way that I can parallelly run another thread which waits for the opening of this dialog box and closes it ( this thread parallelly running with ocr.recognize). Kindly let me know how to identify if a windows dialog box has been opened using java code.
You can use Robot class for this purpose as below:
//if you want to press alt+f4; to close window or
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_F4);
// to press enter
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Java Robot does not simulate TAB KEY in windows8. Why? and how can i fix it?

I was trying to simulate TAB KEY using class java.awt.Robot; in Windows8 but it does not work(just like no key pressed). I had tried in OS X Yosemite, it worked perfectly.
try {
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
}catch (AWTException e){}
Someone can tell me why it does not work? and how can I simulate tab key in Windows8 ?
One more question, what does this method do?
robot.waitForIdle()

How to simulate a real mouse click using java?

I'm attempting to perform a mouse click in Java, to click something in an external program. To do this, I'm using java.awt.robot, and the following code:
Robot bot = new Robot();
int mask = InputEvent.MOUSE_BUTTON1_DOWN;
bot.mouseMove(x, y);
bot.mousePress(mask);
bot.mouseRelease(mask);
Here's the problem. The external program is able to detect that this click is computer-generated and not human-generated, and hence, its rejecting this click.
I have already tried moving the mouse there naturally and that didn't have any effect. So my guess is, that it must be listening to the keyboard state or such, and telling from that, that the click is computer generated.
What do I have to do to set all keyboard / mouse states to act in the same way as a normal mouse click would?
Well I had the same exact requirement, and Robot class is perfectly fine for me. It works on windows 7 and XP (tried java 6 & 7).
public static void click(int x, int y) throws AWTException{
Robot bot = new Robot();
bot.mouseMove(x, y);
bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
May be you could share the name of the program that is rejecting your click?
FYI, in newer versions of Windows, there's a new setting where if a program is running in Adminstrator mode, then another program not in administrator mode, cannot send any clicks or other input events to it. Check your source program to which you are trying to send the click (right click -> properties), and see if the 'run as administrator' checkbox is selected.
it works in Linux. perhaps there are system settings which can be changed in Windows to allow it.
jcomeau#aspire:/tmp$ cat test.java; javac test.java; java test
import java.awt.event.*;
import java.awt.Robot;
public class test {
public static void main(String args[]) {
Robot bot = null;
try {
bot = new Robot();
} catch (Exception failed) {
System.err.println("Failed instantiating Robot: " + failed);
}
int mask = InputEvent.BUTTON1_DOWN_MASK;
bot.mouseMove(100, 100);
bot.mousePress(mask);
bot.mouseRelease(mask);
}
}
I'm assuming InputEvent.MOUSE_BUTTON1_DOWN in your version of Java is the same thing as InputEvent.BUTTON1_DOWN_MASK in mine; I'm using 1.6.
otherwise, that could be your problem.
I can tell it worked because my Chrome browser was open to http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html when I ran the program, and it changed to Debian.org because that was the link in the bookmarks bar at (100, 100).
[added later after cogitating on it today]
it might be necessary to trick the listening program by simulating a smoother mouse movement. see the answer here: How to move a mouse smoothly throughout the screen by using java?
With all respect the most likely thing is that you are mistaken about why the click is being 'rejected'. Why do you think some program is trying to determine if it's human or not? The Robot class (have used it a lot) should send messages that the operating system has no way to distinguish from a user doing the click.
Some applications may detect click source at low OS level. If you really need that kind of hack, you may just run target app in virtual machine's window, and run cliker in host OS, it can help.
You could create a simple AutoIt Script that does the job for you, compile it as an executable and perform a system call there.
in au3 Script:
; how to use: MouseClick ( "button" [, x, y [, clicks = 1 [, speed = 10]]] )
MouseClick ( "left" , $CmdLine[1], $CmdLine[1] )
Now find aut2exe in your au3 Folder or find 'Compile Script to .exe' in your Start Menu and create an executable.
in your Java class call:
Runtime.getRuntime().exec(
new String[]{
"yourscript.exe",
String.valueOf(mypoint.x),
String.valueOf(mypoint.y)}
);
AutoIt will behave as if it was a human and won't be detected as a machine.
Find AutoIt here: https://www.autoitscript.com/

How to type special key using Sikuli Java Standalone jar?

In Sikuli X I can use something like
type("x", KEY_CTRL)
How can I do the same in Sikuli API?
I used in my projects this way:
Screen screen = new Screen();
screen.click(<your texbox>); // click into textbox to have a cursor there
screen.type("a", KeyModifier.CTRL); // this sends Ctrl+"a" to select all text <- this is what you asked for !
screen.type(Key.BACKSPACE); // this sends Backspace key pressed, i.e. here deletes all selected text
Note that I used both KeyModifier and Key. This way it worked.
try this,
Screen screen = new Screen();
screen.type("x",Key.CTRL)
screen.type("\n");
Use the key codes in KeyEvent:
DesktopKeyboard keyboard = new DesktopKeyboard();
keyboard.keyDown(KeyEvent.VK_CONTROL);
keyboard.type("x");
keyboard.keyUp(KeyEvent.VK_CONTROL);

java: how to send accelerator key

I am using Junit4 under eclipse.
I would like to write a test which can be able to send the action : ctrl+shift+P
I tried this using JTable as I don't know for which component I could use the sendAcceleratorKey :
myTable.sendAcceleratorKey(InputEvent.CTRL, InputEvent.SHIFT_DOWN_MASK)
but I can't add a third argument to say KeyEvent.P.
How can I send this action which changes the menu?
Thanks!
I guess you can use the Robot class.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_P);
Thread.sleep(1000); // Time for your code to react to the event
assert(...);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_P);
I can't find any reference to sendAcceleratorKey(). But if it really exists and it does what you want, it looks logic to me to use the the method this way, using key modifiers:
myTable.sendAcceleratorKey(InputEvent.CTRL | InputEvent.SHIFT_DOWN_MASK,
KeyEvent.VK_P);
Otherwise, try to swap the parameters, depending on the methods signature.
myTable.sendAcceleratorKey(KeyEvent.VK_P,
InputEvent.CTRL | InputEvent.SHIFT_DOWN_MASK);

Categories

Resources