How to do Key.DELETE in Eclipse (Java)

Asked by Mario

I am trying to do a Delete in Eclipse but an error is generated saying:

"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Key cannot be resolved to a variable at Sikuli_Notepad.main(Sikuli_Notepad.java:12)"

My Code:
--------------------------------
import edu.mit.csail.uid.*;

public class Sikuli_Notepad {

        public static void main(String[] args) {
                Screen s = new Screen();
                try{
                  s.wait("c:/eclipse/start.png");
                  s.click("c:/eclipse/start.png", 0);
                        s.wait("c:/eclipse/start.png");
                        s.click("c:/eclipse/Run.png", 0);
                        s.type(null, Key.DELETE, 0);
                        //s.type(null, "hello world\n", 0);
                }
                catch(FindFailed e){
                        e.printStackTrace();
                }

        }

}

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Jeremiah Jacquet
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Don't know much about Java, but it seems that it is not recognized, that Key is a Sikuli class. DELETE and all the others are constants defined in this class.

Just looked into the source:
The class Key is not available on the Java level, only on the Jython/Python level, to which you don't have access when working the way you do.

I guess, you have to do it on your own: KeyDelete e.g. is a unicode constant u"\ue006" (hope you know ;-)
Maybe looking into
http://bazaar.launchpad.net/%7Esikuli-dev/sikuli/0.10/annotate/head%3A/sikuli-script/src/main/python/sikuli/Key.py
helps.

If you look into the source of class Region, you will see in the private method type_ch() that these unicode constants are converted into appropriate calls to the keypress function of the robot.
http://bazaar.launchpad.net/%7Esikuli-dev/sikuli/0.10/annotate/head%3A/sikuli-script/src/main/java/edu/mit/csail/uid/Region.java
at line 672

Revision history for this message
Raymond Gonzales (atlas1015) said :
#2

Being able to use keystrokes was killing me because I needed to use Keystrokes to close apps in the mac. I found this post about how to use keystrokes in Java. Its super easy to implement!

http://www.1your.com/drupal/robotclassdemo

Read the article and you will be able to use Keystrokes with just a few lines of code :)

Revision history for this message
Raymond Gonzales (atlas1015) said :
#3

Here is my demo of how I was able to use keystrokes. I am basically opening IE on a windows machine and then closing it using Keystrokes :

----------------------------------------------------------------------------

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import edu.mit.csail.uid.FindFailed;
import edu.mit.csail.uid.SikuliScript;

public class testKeystrokes {
 public static void thinktime(String mseconds) {
  int milliseconds = Integer.parseInt(mseconds);

  try {
   Thread.sleep(milliseconds);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }//end method thinktime

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

                                //creating sikuli object
                                SikuliScript app = new SikuliScript();

                                //creating Robot object so I can use Keystrokes
  Robot robot = new Robot();

  app.openApp("C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe");
  thinktime("10000");

                                //having code select specific Keyboard keys

  robot.keyPress(KeyEvent.VK_ALT);
  robot.keyRelease(KeyEvent.VK_ALT);

  robot.keyPress(KeyEvent.VK_F);
  robot.keyRelease(KeyEvent.VK_F);

  robot.keyPress(KeyEvent.VK_X);
  robot.keyRelease(KeyEvent.VK_X);

 }//end method main

}//end class
-------------------------------------------------------------------

Revision history for this message
Best Jeremiah Jacquet (jeremiahjacquet) said :
#4

The KeyEvents in Robot are easier to use then Swing's. Listen to this Ray guy he knows ;)

Revision history for this message
Mario (mmatthee) said :
#5

Thanks Ray