X-1.0rc3:Java: Focus to browser url field using type() a shortcut

Asked by Rahul Misra

Initially I was capturing the url field of the browser as an image and than clicking on it. This was working fine but it needed to capture images for every browser. Bad idea!!!

Better solution is to type in : press ATD key + D. Now I am trying to do this in java. Here is my code:

import org.sikuli.script.*;

public class T3 {

        public static void main(String[] args) {
                Screen s = new Screen();
                try{
                 App app = new App("C:\\Users\\rmisra\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
                 app.open();
                 Thread.sleep(3000);
                 app.focus();
                 s.highlight(0);
                 s.type(Key.ALT + "D"); //dosent work
                 s.type(null, Key.ALT + "D"); //dosent work
                 s.type(s, Key.ALT + "D"); //dosent work
                 s.type("http://abc.com");
                }
                catch(Exception e){
                        e.printStackTrace();
                }

        }
}

I am using the latest sikuli script jar. This does not work. It is not sending focus to browser url field. What am I missing? When I do Alt + D on the open window through the keyboard manually, focus goes to url text field.

I do have a general question though: if I am not trying to click on a specific image what should be used as the first field of type method? How does it know that we need to send the event to the screen when PSRL field is null?

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Rahul Misra
Solved:
Last query:
Last reply:
Revision history for this message
Rahul Misra (rmisra) said :
#1

Got it. Was messing up the use of key modifier in the api.

import org.sikuli.script.*;

public class T3 {

        public static void main(String[] args) {
                Screen s = new Screen();
                try{
                 App app = new App("C:\\Users\\rmisra\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
                 //App app = new App("C:\\Program Files\\Internet Explorer\\iexplore.exe");
                 app.open();
                 Thread.sleep(3000);
                 app.focus();
                 s.highlight(0);
                 s.type(null, "d",KeyModifier.ALT);
                 s.type("http://abc.com" + Key.ENTER);
                }
                catch(Exception e){
                        e.printStackTrace();
                }

        }
}

Revision history for this message
Jeff Sant (sant-jeff) said :
#2

Dear Friend,

Why i am getting error while executing the code

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
 KeyModifier cannot be resolved to a variable
 Key cannot be resolved to a variable

 at T3.main(T3.java:14)

Revision history for this message
RaiMan (raimund-hocke) said :
#3

@Jeff

He is using the latest build of rc3 from
http://sikuli.org/download.shtml#last-build