class ClipboardListener extends Thread implements ClipboardOwner {
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
boolean bEnough = false;
@Override
public void run() {
Transferable trans = sysClip.getContents(this);
regainOwnership(trans);
while (true) {
if (isitEnough()) {
break;
}
}
}
public void itisEnough() {
bEnough = true;
}
public void itisNotEnough() {
bEnough = false;
}
boolean isitEnough() {
return bEnough;
}
@Override
public void lostOwnership(Clipboard c, Transferable t) {
try {
sleep(200);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
try {
Transferable contents = c.getContents(this);
regainOwnership(contents);
} catch (Exception e) {
e.printStackTrace();
}
}
void processContents(Transferable t) {
if (isitEnough()) {
return;
}
DataFlavor[] flavors = t.getTransferDataFlavors();
for (int i = flavors.length - 1; i >= 0; i--) {
try {
Object o = t.getTransferData(flavors[i]);
if (o instanceof String) {
System.out.println(o.toString().toLowerCase());
break;
}
} catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace();
}
}
}
void regainOwnership(Transferable t) {
sysClip.setContents(t, this);
processContents(t);
}
public static void main(String[] args) {
ClipboardListener b = new ClipboardListener();
b.itisNotEnough();
b.start();
}
}
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
boolean bEnough = false;
@Override
public void run() {
Transferable trans = sysClip.getContents(this);
regainOwnership(trans);
while (true) {
if (isitEnough()) {
break;
}
}
}
public void itisEnough() {
bEnough = true;
}
public void itisNotEnough() {
bEnough = false;
}
boolean isitEnough() {
return bEnough;
}
@Override
public void lostOwnership(Clipboard c, Transferable t) {
try {
sleep(200);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
try {
Transferable contents = c.getContents(this);
regainOwnership(contents);
} catch (Exception e) {
e.printStackTrace();
}
}
void processContents(Transferable t) {
if (isitEnough()) {
return;
}
DataFlavor[] flavors = t.getTransferDataFlavors();
for (int i = flavors.length - 1; i >= 0; i--) {
try {
Object o = t.getTransferData(flavors[i]);
if (o instanceof String) {
System.out.println(o.toString().toLowerCase());
break;
}
} catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace();
}
}
}
void regainOwnership(Transferable t) {
sysClip.setContents(t, this);
processContents(t);
}
public static void main(String[] args) {
ClipboardListener b = new ClipboardListener();
b.itisNotEnough();
b.start();
}
}
No comments:
Post a Comment