comsult
asked on
Disable printscreen in Java application.
I have created a Java browser which runs a local HTML manual. I don't want my users to be able to copy the information via printscreen. Is there any way to prevent this ? I need a resolution quickly hence the large amount of points offered.
Not without JNI, if at all
Take a look here: http://forum.java.sun.com/thread.jsp?thread=194577&forum=52&message=640226 someone suggests you might be able to do it with Windows Management Interface (if you are running Java udner Win32).
A whole SDK seems to be something of an overkill when all you need to do is consume key messages from the print screen key.
ASKER
Thanks for the comments. I will have a look at the JNI and the WMI. I do need to keep the solution as simple as possible as the browser runs from a JAR on a CD. Is there an actual key mapping that is called when printscreen is used that could be captured ? I suppose it would also have to look at ALT printscreen as well.
ASKER
Thanks for the comments. I will have a look at the JNI and the WMI. I do need to keep the solution as simple as possible as the browser runs from a JAR on a CD. Is there an actual key mapping that is called when printscreen is used that could be captured ? I suppose it would also have to look at ALT printscreen as well.
ASKER
Thanks for the comments. I will have a look at the JNI and the WMI. I do need to keep the solution as simple as possible as the browser runs from a JAR on a CD. Is there an actual key mapping that is called when printscreen is used that could be captured ? I suppose it would also have to look at ALT printscreen as well.
... or implement a ClipboardMonitor. Every time the clipboard will contain a screen capture you should flush'it out.
ASKER
Thanks for the comments. I will have a look at the JNI and the WMI. I do need to keep the solution as simple as possible as the browser runs from a JAR on a CD. Is there an actual key mapping that is called when printscreen is used that could be captured ? I suppose it would also have to look at ALT printscreen as well.
ASKER
Something strange going on here, my comments posted once are quadrupled !
Ovi, a ClipboardMonitor sounds like an interesting idea. I'm still quite a novice when it comes to Java, is that difficult to implement ?
Ovi, a ClipboardMonitor sounds like an interesting idea. I'm still quite a novice when it comes to Java, is that difficult to implement ?
ASKER
Something strange going on here, my comments posted once are quadrupled !
Ovi, a ClipboardMonitor sounds like an interesting idea. I'm still quite a novice when it comes to Java, is that difficult to implement ?
Ovi, a ClipboardMonitor sounds like an interesting idea. I'm still quite a novice when it comes to Java, is that difficult to implement ?
Not really. I will post soon details if you are interested.
About multiplication, don't reload the question page with the reload button of your browser. You have a link in the top left corner of your question page for that ("Reload This Question").
About multiplication, don't reload the question page with the reload button of your browser. You have a link in the top left corner of your question page for that ("Reload This Question").
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This method is unused so you should remove'it:
private int getCurrentEventModifiers() {
int modifiers = 0;
AWTEvent currentEvent = EventQueue.getCurrentEvent ();
if (currentEvent instanceof InputEvent) {
modifiers = ((InputEvent) currentEvent).getModifiers ();
} else
if (currentEvent instanceof ActionEvent) {
modifiers = ((ActionEvent) currentEvent).getModifiers ();
}
return modifiers;
}
private int getCurrentEventModifiers()
int modifiers = 0;
AWTEvent currentEvent = EventQueue.getCurrentEvent
if (currentEvent instanceof InputEvent) {
modifiers = ((InputEvent) currentEvent).getModifiers
} else
if (currentEvent instanceof ActionEvent) {
modifiers = ((ActionEvent) currentEvent).getModifiers
}
return modifiers;
}
That's some pretty neat code Ovi :-) Although you have to make a few adjustments to run it on earlier java versions than 1.4.
ASKER
Thank you very much for this, it is exactly what I need. Thanks to everyone else for their comments also.
Thanks for the points(comsult) and for the nice laugh(schybert) :-).
This is a little more cleaner version, and a final one.
import java.awt.Toolkit;
import java.awt.datatransfer.Clip board;
import java.awt.datatransfer.Clip boardOwner ;
import java.awt.datatransfer.Data Flavor;
import java.awt.datatransfer.Stri ngSelectio n;
import java.awt.datatransfer.Tran sferable;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFrame;
public final class ClipboardMonitor extends Observable implements ClipboardOwner {
private static ClipboardMonitor monitor = null;
private ClipboardMonitor() {
gainOwnership();
}
private void gainOwnership() {
Clipboard clip = Toolkit.getDefaultToolkit( ).getSyste mClipboard ();
try {
Transferable content = clip.getContents(null);
DataFlavor[] f = content.getTransferDataFla vors();
boolean imageDetected = false;
for (int i = 0; i < f.length; i++) {
if (f[i].equals(DataFlavor.im ageFlavor) ) {
imageDetected = true;
break;
}
}
if (imageDetected) {
System.out.println("Image content detected");
flushClipboard();
} else {
clip.setContents(content, this);
}
setChanged();
notifyObservers(content);
} catch (IllegalArgumentException istateexception) {
istateexception.printStack Trace();
} catch (Exception ioexception) {
ioexception.printStackTrac e();
}
}
public void lostOwnership(Clipboard clipboard, Transferable contents) {
System.out.println("Owners hip lost ...");
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(200);
gainOwnership();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
public void flushClipboard() {
Toolkit.getDefaultToolkit( ).getSyste mClipboard ().setCont ents(new StringSelection(""), this);
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException ("There can be only one instance of this monitor!");
}
public static final ClipboardMonitor getMonitor() {
if (monitor == null)
monitor = new ClipboardMonitor();
return (monitor);
}
public static void main(String[] args) {
JFrame f = new JFrame();
ClipboardMonitor monitor = ClipboardMonitor.getMonito r();
monitor.addObserver(new Observer() {
public void update(Observable o, Object arg) {
System.out.println("Clipbo ard has been regained!");
}
});
f.setDefaultCloseOperation (JFrame.EX IT_ON_CLOS E);
f.setSize(500, 100);
f.setVisible(true);
}
}
This is a little more cleaner version, and a final one.
import java.awt.Toolkit;
import java.awt.datatransfer.Clip
import java.awt.datatransfer.Clip
import java.awt.datatransfer.Data
import java.awt.datatransfer.Stri
import java.awt.datatransfer.Tran
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFrame;
public final class ClipboardMonitor extends Observable implements ClipboardOwner {
private static ClipboardMonitor monitor = null;
private ClipboardMonitor() {
gainOwnership();
}
private void gainOwnership() {
Clipboard clip = Toolkit.getDefaultToolkit(
try {
Transferable content = clip.getContents(null);
DataFlavor[] f = content.getTransferDataFla
boolean imageDetected = false;
for (int i = 0; i < f.length; i++) {
if (f[i].equals(DataFlavor.im
imageDetected = true;
break;
}
}
if (imageDetected) {
System.out.println("Image content detected");
flushClipboard();
} else {
clip.setContents(content, this);
}
setChanged();
notifyObservers(content);
} catch (IllegalArgumentException istateexception) {
istateexception.printStack
} catch (Exception ioexception) {
ioexception.printStackTrac
}
}
public void lostOwnership(Clipboard clipboard, Transferable contents) {
System.out.println("Owners
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(200);
gainOwnership();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
public void flushClipboard() {
Toolkit.getDefaultToolkit(
}
public Object clone() throws CloneNotSupportedException
throw new CloneNotSupportedException
}
public static final ClipboardMonitor getMonitor() {
if (monitor == null)
monitor = new ClipboardMonitor();
return (monitor);
}
public static void main(String[] args) {
JFrame f = new JFrame();
ClipboardMonitor monitor = ClipboardMonitor.getMonito
monitor.addObserver(new Observer() {
public void update(Observable o, Object arg) {
System.out.println("Clipbo
}
});
f.setDefaultCloseOperation
f.setSize(500, 100);
f.setVisible(true);
}
}
For people who want to use this cool tool with java 1.3.1 you have to create the imageFlavor yourself since the static field in DataFlavor didn't exist back then. Just create your own imageFlavor object like this:
private static final DataFlavor IMAGE_FLAVOR = new DataFlavor(java.awt.Image. class, "image/x-java-image");
When I mentioned adjustments in plural I had tried it on java 1.2.2 :-)
private static final DataFlavor IMAGE_FLAVOR = new DataFlavor(java.awt.Image.
When I mentioned adjustments in plural I had tried it on java 1.2.2 :-)
Yes, indeed, the other solution is to test the contents of the default DataFlavor from clipboard using following methods:
// System.out.println("Name: " + f[i].getHumanPresentableNa me());
// System.out.println("MimeTy pe: " + f[i].getMimeType());
// System.out.println("Primar yType: " + f[i].getPrimaryType());
// System.out.println("SubTyp e: " + f[i].getSubType());
If you test with those you'll be abble to see if the clipboard content is a image or not and take necesary actions. If I remember corectly the primary type is allways "image", the name is "image/x-java-image" and so on. An 'if' on those properties will be enough to decide if you have a image or not.
// System.out.println("Name: " + f[i].getHumanPresentableNa
// System.out.println("MimeTy
// System.out.println("Primar
// System.out.println("SubTyp
If you test with those you'll be abble to see if the clipboard content is a image or not and take necesary actions. If I remember corectly the primary type is allways "image", the name is "image/x-java-image" and so on. An 'if' on those properties will be enough to decide if you have a image or not.
ASKER
Thanks again Ovi, I am using Java 1.4 so this is perfect. I appreciate all the help from everyone :-)