Last active
February 2, 2021 03:34
-
-
Save Pooh3Mobi/03b096024671de2f19432bb20e744c5b to your computer and use it in GitHub Desktop.
AWT Clipboard String handling in Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Clipboard.onChanged(block: (String?) -> Unit) { | |
addFlavorListener { e -> block((e.source as Clipboard).clipboardString) } | |
} | |
var Clipboard.clipboardString: String? | |
set(value) { | |
val ss = StringSelection(value) | |
setContents(ss, ss) | |
} | |
get() { | |
return try { | |
getData(DataFlavor.stringFlavor).toString() | |
} catch (e: Exception) { | |
null | |
} | |
} | |
fun test() { | |
val c: Clipboard = Toolkit.getDefaultToolkit().systemClipboard | |
val currntCpString = c.clipboardString | |
c.onChanged { string -> | |
text = string ?: "error: cannot get clipboard" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not supporting removeFlavorListener