-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClipHelper.pde
70 lines (58 loc) · 1.91 KB
/
ClipHelper.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// //////////////////
// Clipboard class for Processing
// by seltar, modified by adamohern
// v 0115AO
// only works with programs. applets require signing
// CLIPHELPER OBJECT CLASS:
class ClipHelper {
Clipboard clipboard;
ClipHelper() {
getClipboard();
}
void getClipboard () {
// this is our simple thread that grabs the clipboard
Thread clipThread = new Thread() {
public void run() {
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
}
};
// start the thread as a daemon thread and wait for it to die
if (clipboard == null) {
try {
clipThread.setDaemon(true);
clipThread.start();
clipThread.join();
}
catch (Exception e) {
}
}
}
void copyString (String data) {
copyTransferableObject(new StringSelection(data));
}
void copyTransferableObject (Transferable contents) {
getClipboard();
clipboard.setContents(contents, null);
}
String pasteString () {
String data = null;
try {
data = (String)pasteObject(DataFlavor.stringFlavor);
}
catch (Exception e) {
System.err.println("Error getting String from clipboard: " + e);
}
//println(data);
return data;
}
Object pasteObject (DataFlavor flavor)
throws UnsupportedFlavorException, IOException
{
Object obj = null;
getClipboard();
Transferable content = clipboard.getContents(null);
if (content != null)
obj = content.getTransferData(flavor);
return obj;
}
}