Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Getting java program to execute ctrl-alt-backspace

Hi guys

Hope you can help me with this problem.

I'm writing a java program (JDK1.4, running on Unix - Fedora Core 3) which needs to excute the command CTRL-ALT-BACKSPACE.

Currently I am using the following code to exeute normal unix commands e.g. du -h > outputfile.out, and this seems to work ok.

String command = ...
Process proc = Runtime.getRuntime().exec(command);

Any idea on how to get to execute CRTL-ALT-BACKSPACE

Thanks
jjamesis
[509 byte] By [jjamesis] at [2007-11-11 7:21:36]
# 1 Re: Getting java program to execute ctrl-alt-backspace
http://forum.java.sun.com/thread.jspa?threadID=440311&messageID=1984692Is there a Java equivalent for VB's "SendKeys"?
The Robot class, introduced in JDK 1.3+ handles this functionality -- but a whole lot more.

This class allows you to (accord to Sun):

"...This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations..."

VB includes sample code on how to send keystrokes to the Notepad.exe app ( do F1 on "SendKeys" in OnLineHelp).

The Java equivalent:

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

public class RobotTest01{
//Create an array of keycode data
int keyInput[] = {
KeyEvent.VK_H,
KeyEvent.VK_E,
KeyEvent.VK_L,
KeyEvent.VK_L,
KeyEvent.VK_O
};

public static void main(String[] args)
throws AWTException,IOException{

Runtime.getRuntime().exec("notepad");

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_SHIFT);

for (int i = 0;
i < keyInput.length; i++){
robot.keyPress(keyInput[i]);
robot.delay(500);

}
}

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/84964

Subject: Re: Sending keystrokes to GUI applications
From: "Ron Coutts" <rcoutts envistatech.com>
Date: Wed, 12 Nov 2003 03:24:13 +0900
In-reply-to: 84961
After some more digging at Microsoft's web site, it seems I've answered
my own question. The following snippet will start Notepad and send it
some keys.

Ron

require 'win32ole'

wsh = WIN32OLE.new('WScript.Shell')
wsh.Run("Notepad.exe")
while not wsh.AppActivate("Notepad")
sleep .1
end
wsh.SendKeys("This is a test.")

> --Original Message--
> From: Ron Coutts [mailto:rcoutts@envistatech.com]
> Sent: November 11, 2003 10:15 AM
> To: ruby-talk ML
> Subject: Sending keystrokes to GUI applications
>
>
> I'm trying to automate a Windows GUI application that has no command
> line interface and no OLE Automation interface. I only need to send a
> few keystrokes to this application. I thought Ruby would be great for
> this job but I've searched extensively and can't find any
> information on
> how to do it. Below is an example in Java of what I'd like to do
> (Notepad is not the application I'm trying to automate).
>
> Can anyone provide any suggestions or code snippets showing
> how to send
> keystrokes to a Windows GUI application. Any help would be much
> appreciated.
>
> Regards,
> Ron
>
>
> import java.awt.AWTException;
> import java.awt.Robot;
> import java.awt.event.KeyEvent;
> import java.io.IOException;
>
> public class Automate {
> public static void main(String[] args) {
> try {
> Runtime.getRuntime().exec( "notepad.exe");
> Robot robot = new Robot();
> robot.keyPress(KeyEvent.VK_A);
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (AWTException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
> }
>
>

http://bellsouthpwp.net/d/g/dgs60/support/java.html
--
http://********info.ru/article523.html
var wsh=WScript.CreateObject("WScript.Shell");
wsh.Run("calc");
WScript.Sleep("500");
wsh.AppActivate("Калькулятор");
wsh.SendKeys("2");
WScript.Sleep("500");
wsh.SendKeys("*");
WScript.Sleep("500");
wsh.SendKeys("3");
WScript.Sleep("500");
wsh.SendKeys("~");
xsouldeath at 2007-11-11 22:38:42 >