[1.0] Windows: Java: Screen.selectRegion() hangs when used in JFrame button handler

Asked by jaloveczki péter

What I am trying to accomplish is to select an area on the screen with the mouse (outside of the may frame) and get the resulting region coordinates using Sikuli.

The code that should do this looks is below:

Screen screen = Screen.getPrimaryScreen();
Region region = screen.selectRegion("Select the area.");
What happens is that the cursor turns into a selector cross (the ones you usually see when you expect this function), but I can't select the area and actually the only way I can get back from the application is by killing it. Not too many examples I have found so I am asking for help here.

How can I make this work?

Also one other question:

I have downloaded the following script version:

Sikuli-IDE-1.0.0-Win64.zip

This means if I want to create a crossplatform solution I have to include like 6 jars? I have found a more universal Java API it seems (that is what it is called actually):

https://code.google.com/p/sikuli-api/

With all required supported OS but I can't find a single example on what I am trying to do that is similar to the little code snippet I pasted here. The sikuli script I am using now and this Sikuli API (apparently not the same) seems to be just different enough to amke this difficult.

Any suggestions? Thanks a lot in advance.

Question information

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

What happens if you try to use
reg = selectRegion();

in the Sikuli-IDE or with the interactive Jython prompt using sikuli-script.cmd -i ?

I only have a chance, to test it later on on my Win7 64. Hope it is not a bug ;-)

--- Sikuli Java API
Yes, it is very different on the API level, but similar according to the features. Not sure, wether it has a selectRegion() similar function.

--- 6 jars to be cross platform
I am just finalizing the service update 1.0.1 to be released the next days: there will be an option, to have one sikuli-java.jar, that works on windows, mac and linux automagically just out of the box for Java and Jython development. So you might simply use this jar with your Java developement in your IDE, pack this jar with your application and every one can download and use it without any additional efforts.

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

Just tested on my Win7 64 Bit with Java 7.

--- Sikuli stuff in
c:\SikuliX

--- in system path
c:\SikuliX\libs

--- a NetBeans Java project with c:\SikuliX\sikuli-script.jar in projects libraries
--- Java app SikTest
  package siktest;
  import org.sikuli.script.*;
  public class SikTest {
    public static void main(String[] args) {
      Screen s = new Screen();
      Region reg = s.selectRegion();
      Debug.user(reg.toString());
    }
  }

--- output
[user (30.07.13 17:47:53)] R[147,148 330x221]@S(0)[0,0 1440x900] E:Y, T:3,0

It worked as expected.

Revision history for this message
jaloveczki péter (petivagyokenlev) said :
#3

I tested with java7 still not good can you please send me a link to the sikuli jar you used? I do not doubt it works for you if I can get it to work with same jar I wil be pleased.

Revision history for this message
jaloveczki péter (petivagyokenlev) said :
#4

Okay I get it now. Sorry for being so stupid, I actually tried to invoke this inside of a frame (Particulalry in an EventHandler). I have tested it in a stripped down main functions and it works for me as well.

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

He got it ;-)

Revision history for this message
jaloveczki péter (petivagyokenlev) said :
#6

Is there a workaround for this though? Lets say I want to invoke this aera selection aftera button is pressed?

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

Workaround for what?

You said it works?

Might be helpful if you paste a principal (stripped-down to the essence) version of your java code.

Revision history for this message
jaloveczki péter (petivagyokenlev) said :
#8

My current problem is that I can get it to work in a stripped down class with a main method (like in your example), BUT I can't get it to work when I call the same snippet from an eventhandler of a button. Only thing you need to do is put a button on a stripped down frame and for click event handler paste the same code snippet you have posted here.

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

I confirm this situation: It is not possible, to present the transparent overlay window as it should be, when selectRegion() and userCapture() are used inside a button handler of a currently active JFrame (visible or not).

a possible workaround is outlined in comment #1 of the related bug
(https://bugs.launchpad.net/sikuli/+bug/1206879/comments/1)

Revision history for this message
jaloveczki péter (petivagyokenlev) said :
#10

Thank you for your confirmation and the workaround.

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

is a bug now