how to use printscreen on laptop in java which requires to press the "fn" key

Asked by lakshmi

Hi
I am trying to use printscreen on my HP laptop. This requires me to first press the "fn" (function) key and then the printscreen option. Can I use keydown("fn") and then printscreen?

I tried this but I guess there is no "fn" button allowed. Also what is the difference between C_ALT and ALT keywords.

Regards,
Lakshmi.

Question information

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

--- Also what is the difference between C_ALT and ALT keywords.
For API usages the Key.ALT version is recommended, since it is of type String (easy concatenation with other Strings).
the C_XXX contain the same, but as Java type char. it is implemented and hence preferably for internal use.

--- press and hold fn-key
the fn-key usage as such is not available to the Java level. It is a hardware trick to produce key-codes on compact keyboards, that could otherwise not be produced, since the real key is not available on that compact keyboard.
To the Java level the intended key is reported.
So in your case, if you press fn-printscreen: what is the intended key press?
if it is printscreen, then you can try: Key.PRINTSCREEN

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

... just an example on my MB-Air keyboard
- pressing the down key is equivalent to Key.DOWN
- if I press fn-down, the intended key is PageDown, hence the equivalent in Sikuli would be Key.PAGE_DOWN

Revision history for this message
lakshmi (lakshmidesai) said :
#3

So should it be:
s.type(Key.PAGE_DOWN);
s.type(Key.PRINTSCREEN);

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

Uuups ???

PAGE_DOWN was only an example for the fn-key usage and Sikuli simulation on my laptop keyboard.

So if the PRINTSCREEN feature is your intention, then
s.type(Key.PRINTSCREEN);

should do it.

BTW: you might capture() with Sikuli as well
ScreenImage img = s.capture();
String tempFileName = img.getFile();

now you might do with
File tempFile = new File(tempFileName)

... whatever you want

Revision history for this message
lakshmi (lakshmidesai) said :
#5

Hi RaiMan,
Thanks for that.

But unfortunately nothing worked.
Capture screen does get me an image but, its not getting pasted onto a word document. It neither created a new file at the location I have specified.

Can you please help me with this. I did read about the other questions raised on the same, is using a third party tool is the only way to capture the screenshot?

Revision history for this message
lakshmi (lakshmidesai) said :
#6

Also how do I use wait in Java class. if I use screen.wait(10), its giving me a java.lang.IllegalMonitorStateException

Java classes can be run from command line using the below command:

Java -cp %sikuli-script.jar" <filename>.java

but I am trying this with sikuli-ide.jar. I am getting an error as NoclassDefFound

How do I go about with this.

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

on comment #6

--- wait in Java class.
use screen.wait(10f)

--- Java -cp %sikuli-script.jar" <filename>.java
to run a java program, you need compiled classes (.java ia source file)

Java -cp %sikuli-script.jar" <filename>.class

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

on comment #5:

--- its not getting pasted onto a word document
it is currently not possible, to easily get a captured image to the clipboard, so it could be pasted elsewhere

--- It neither created a new file at the location I have specified.
how did you do that?

Revision history for this message
lakshmi (lakshmidesai) said :
#9

Thanks for the answers. It did help.

Comment # 6:

Forgot to mention that I did try running the .class file. I am still getting the same error.
Copying the code below. Its just a simple class on google search.

public class GoogleSearch {
 public static void openApp(){
  App.open("C:\\Program Files\\Internet Explorer\\iexplore.exe");
 }

public static void openGoogle() throws InterruptedException, FindFailed{
  Screen s = new Screen();
  s.type("img/1378697905588.png", "www.google.com.au");
  s.type("\n");
  s.type("img/1381291211278.png", "sikuli java tutorial");
  s.type("\n");
 }

public static void main(String[] args){
 openApp();
 try {
  openGoogle();
 } catch (FindFailed e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }

Comment # 5:
Well, it would be a good feature to add. Saving screen shots. I will try to look for an alternative for this.
ScreenImage img = s.capture();
  String tempFName = img.getFile();
  String name = tempFName.substring(tempFName.indexOf("-")+1,tempFName.indexOf(".png"));

<name> is passed to this method.

public static void takePictureOfError(String Name) throws IOException, AWTException {
// new File("C:\\Lakshmi - EA\\Sikuli\\Image Files").mkdir();
   Toolkit toolkit = Toolkit.getDefaultToolkit();
   Dimension screenSize = toolkit.getScreenSize();
   Rectangle screenRect = new Rectangle(screenSize);
   Robot robot = new Robot();
   BufferedImage image = robot.createScreenCapture(screenRect);
   ImageIO.write(image, "png", new File("C:\\Lakshmi - EA\\Sikuli\\Image Files\\"+ Name));
}
This creates a file for me with the name as the image. but not sure with what extension. I am not able to view that file.

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

---- at comment on comment #6
Uuups, some kind of overkill ;-)

ScreenImage img = s.capture();
String tempFName = img.getFile();

//String name = tempFName.substring(tempFName.indexOf("-")+1,tempFName.indexOf(".png"));

String name = new File(tempFname).getName()

but this does not help.

to store the temp image somewhere else:
new File(tempFname).renameTo(new File("some new filename"))

this works within the same file system

Otherwise we have
move(Path source, Path target, CopyOption... options)
from java.​nio.​file.​Files

... and with version 1.0.1 it is even easier:
String imgFName = s.capture().getFile(targetPath, targetName)

where targetName might omit .png, which is auto-added

Revision history for this message
lakshmi (lakshmidesai) said :
#11

Thanks RaiMan, that solved my question.

Revision history for this message
lakshmi (lakshmidesai) said :
#12

These 2 lines solved my problem. Thanks!!

ScreenImage img = s.capture();
ImageIO.write(img.getImage(), "jpeg",new File("C:\\Lakshmi - EA\\Sikuli\\Image Files\\Image" + ++i +".jpeg"));