### Copy text using API methods

An example of copying text to the clipboard using API methods (requires the JavaObject library).
This solution can be used f.e. in a webapp where the browser runs on the device executing the webapp.
' Copy a text string to the clipboard
' Example (from ABMaterial test app) `
'Sub ActionButton_Copy_Clicked(Target As String, SubTarget As String)
' If Clipboard_SetString(mContent) Then
' ShowToast("toastgreen",$"Copied content to the clipboard."$)
' Else
' ShowToast("toastred",$"Can not copy the content to the clipboard."$)
' End If
'End Sub
'`
' Copy a string to the clipboard using API methods (JavaObjects)
`Sub Clipboard_SetString(Text As String) As Boolean
Dim Result As Boolean = False
Try
Dim joStringSelection As JavaObject
joStringSelection.InitializeNewInstance("java.awt.datatransfer.StringSelection", Array(Text))
Dim joToolkit As JavaObject
joToolkit.InitializeStatic("java.awt.Toolkit")
Dim joClipboard As JavaObject
joClipboard = joToolkit.RunMethodJO("getDefaultToolkit", Null).runmethod("getSystemClipboard", Null)
joClipboard.RunMethod("setContents", Array(joStringSelection, joStringSelection))
Result = True
Catch
Log($"[ERROR] Set Clipboard: ${LastException.Message}"$)
End Try
Return Result
End Sub`

Changed: 2018-05-24 10:10, Category: Clipboard, File: T6C172M2173.htm, ID: 2173