Capture screenshot of Active Window - java

I've created a small application that allows the user to capture the screenshot and save the image at a user defined location. It also allows the user to capture the screenshot of the active window as well.
The issue is with the active window. I have learnt that Java does not provide a method to get the screenshot of the active window. So, I googled and found that developers have been using the Alt+PrntScrn to get the screenshot and get the image from the clipboard.
The code is the following one:
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_PRINTSCREEN);
robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
robot.keyRelease(KeyEvent.VK_ALT);
Thread.sleep(1500);
RenderedImage image = (RenderedImage)getClipboard();
saveToImage(image,targetLocation);
Now what happens is that when the Alt key is pressed by the robot class, the focus goes on the toolbar of the application of which the user wants to get the screenshot and the screenshot is taken of the toolbar and not that of the window that the user has focus on.
Please advise on how to solve this problem.

If this were my application, I'd use JNA -- Java Native Access to get the active window and its size and then use that information to allow the robot to grab the image.

Related

I want to make a program that recognize a button that is in the League of Legends client

I want to make a program that recognize a button that is in the League of Legends client, and when the queue's button shows up the mouse instantly click on it.
Its a sort of a AutoAccept Client...
There is already a program that someone did, but it has so many unnecessary things and it does not work very well....
How do I do to recognize a element inside a program to make my mouse move there when it shows up?
I can try to code this in any language, just gimme a tip pls
You can create a program that takes a screenshot every 100ms, use a recognition algorithm of your choice and then (optionally) performs a mouse click. There are enough libraries that can screenshot for you, you should be able to recognise the button by colour and location and every language should have some method of controlling the mouse.
Sikulix (can be paired with python) is a perfect tool to use for this. It scans over your screen constantly, and if the image in question pops up (the accept button) you can click on it.
You will have to take a screenshot and likely store it as .png so the program knows what it is looking for.
Google Sikulix.

Transferring input from JFrame to another application(Non Java)

I'm trying to create a virtual numpad in Java. The objective is to click a button (0...9) and input is reciprocated in another application with highlighted text field like notepad or calculator. In order to do that I used the robot class but it doesn't work. My code goes like this (for button click 0):
if(e.getSource()==b0){
java.awt.Robot r = new java.awt.Robot();
gui.setFocusable(false);
r.keyPress(KeyEvent.VK_0);
gui.setFocusable(true);
}
I've tried to make the JFrame out of focus on button click so that input goes to highlighted application. Is there any way I can always keep my application on top without giving it focus?
In order to show your frame on top always use:
Jframe.setAlwaysOnTop(true);
see more here

How to set a window invisible but focused?

I want to set a window invisible but focused for a fraction of seconds.
I'm writing a key board emulator using Robot class as a part of project for detecting key loggers.
Keyboard emulator should not disrupt the normal work of the user, so I want to set a hidden window focused for a short span so that the keys entered by the robot class received by the hidden window instead of normal user applications.
Is there any way to do this in Java when window is actually invisible? How to do this? at least in windows platform?
How about transparent full screen window in case the above thing is not possible?
My base paper: http://www.atlantis-press.com/php/download_paper.php?id=9980
Is there any way to do this in Java when window is actually invisible?
No. Not in pure Java at least.
How about transparent full screen window in case the above thing is not possible?
A transparent window does not receive events. So again, no.

How to Control Keyboard and Mouse like UAC window

Can anyone kindly help me out?
I want to create an app, like user access control(UAC) application. When UAC is running we cannot click anywhere on the screen, unless we close UAC window. Also we cannot press any key to do anything, like window key or any of the function key. So I want to create a similar application using C ++ code to control keyboard and mouse, that only mouse and keyboard is enabled in my application window and disable outside and unless i do not close my app i cant perform any other task. My application would be just a graphical simple window with a close button, and obove mentioned controls.
A long time ago Windows supported system modal dialogs. These would prevent the user from interacting with other windows including the desktop. Microsoft removed support for this a long time ago due to the problems that it caused.
Now when Windows needs to provide a system modal window for UAC they use a bit of desktop magic. To simulate a system modal window UAC does something like this.
Create a bitmap and take a snapshot of the current desktop.
Darken the bitmap
Create a new desktop
Set the new desktop as the currently active one.
Create a window the size of the new desktop and draw the bitmap in it.
Now they have a desktop that looks like the old and acts as if it were a system model window. You are then free to create a child window to grab input from the user. The example below shows how to create a desktop and switch to it and should be a good starting point for what you want to do
// TODO: Make a copy of the current desktop
// Prepeare a new desktop and activate it
HDESK oldDesktop = GetThreadDesktop(GetCurrentThreadId());
HDESK desktop = CreateDesktop(L"MyDesktop", NULL, NULL, 0, GENERIC_ALL, NULL);
SwitchDesktop(desktop);
// TODO: Create the window that draws the snapshot of the old desktop
// TODO: Create a dialog with buttons and stuff
// Since we don't have a window sit for 5 seconds staring at blank screen
Sleep(5000);
// Switch back to the old desktop and destroy the one we created
// ALERT: If we crash or exit without doing this you will need to
// restart windows
SwitchDesktop(oldDesktop);
CloseDesktop(desktop);
You can find more information on the desktop related API's

Android amazon mp3 keyguard controls

Anyone have an idea how the Amazon mp3 app is displaying the player controls inside of the keyguard? I've been playing with some of the system alert and system overlay window parameters with now luck. The system alert will display over everything except the lock screen and the system overlay will display over the lock screen but in 4.0.3 and above you can not receive the touch events.
I have also seen options like WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG but it only seems to work for an activity; anything else I use it with gives me an exception that tells me that I can not use that flag with this type of window.
What I have noticed is that the player controls either replace or cover the keyguard clock and the player controls change size depending on the type of lock (like pattern lock or swipe lock). I've also noticed that the keyguard does not display my wallpaper anymore...
Any thoughts are welcome!

Categories

Resources