1# Using the Pasteboard to Copy and Paste (C/C++) 2<!--Kit: Basic Services Kit--> 3<!--Subsystem: MiscServices--> 4<!--Owner: @yangxiaodong41--> 5<!--Designer: @guo867--> 6<!--Tester: @maxiaorong2--> 7<!--Adviser: @HelloCrease--> 8 9## When to Use 10 11The pasteboard allows you to copy and paste data of the plain text, hypertext, and URI. 12 13## Basic Concepts 14 15- **OH_PasteboardObserver**: pasteboard observer object, which is used to listen for data changes on the pasteboard. 16- **OH_Pasteboard**: pasteboard object, which is used to query and write. 17- [**OH_UdmfData**](../../reference/apis-arkdata/capi-udmf-oh-udmfdata.md): unified data object. 18 19## Constraints 20 21- The pasteboard content, including system service metadata and application settings, has a maximum size of 128 MB by default. For PCs/2-in-1 devices, the maximum size can be changed through system settings, with a valid range from 128 MB to 2 GB. 22- To ensure the accuracy of the pasteboard data, only one copy can be performed at a time. 23- Currently, supported data types include **OH_UdsPlainText** (plain text), **OH_UdsHtml** (hypertext markup language), **OH_UdsFileUri** (file URI). **OH_UdsPixelMap** (pixel map), **OH_UdsHyperlink** (hyperlink), **OH_UdsAppItem** (application icon), and custom type. The data types supported by ArkTS APIs are different from those supported by NDK APIs. You need to match the data types with the corresponding APIs during usage. For details, see [Using the Pasteboard to Copy and Paste](../pasteboard/use_pasteboard_to_copy_and_paste.md). 24- When you copy and paste data of a custom type, the specified type name cannot be the same as an existing one. 25- Since API version 12, [permission control](get-pastedata-permission-guidelines.md) is added to the pasteboard reading API to enhance user privacy protection. 26- The copy and paste APIs, [setUnifiedData](../../reference/apis-basic-services-kit/js-apis-pasteboard.md#setunifieddata12) and [getUnifiedData](../../reference/apis-basic-services-kit/js-apis-pasteboard.md#getunifieddata12), added in API version 12 are independent of the **OH_Pasteboard_SetData** and **OH_Pasteboard_GetData** APIs mentioned in this topic. Use the corresponding APIs when writing and reading data. 27 28## Available APIs 29 30For details about more APIs and their usage, see [Pasteboard](../../reference/apis-basic-services-kit/capi-pasteboard.md). 31 32| API | Description | 33| ------------------------------------------------------------ | ------------------------------------------------------- | 34| OH_PasteboardObserver* OH_PasteboardObserver_Create() | Creates a pasteboard observer object. | 35| OH_PasteboardObserver_Destroy(OH_PasteboardObserver* observer) | Destroys the pasteboard observer object. | 36| int OH_PasteboardObserver_SetData(OH_PasteboardObserver* observer, void* context, const Pasteboard_Notify callback, const Pasteboard_Finalize finalize) | Sets the callback used to return data changes to the pasteboard observer object. | 37| OH_Pasteboard* OH_Pasteboard_Create() | Creates a pasteboard instance. | 38| void OH_Pasteboard_Destroy(OH_Pasteboard* pasteboard) | Destroys the pasteboard instance. | 39| int OH_Pasteboard_Subscribe(OH_Pasteboard* pasteboard, int type, const OH_PasteboardObserver* observer) | Subscribes to the pasteboard observer. | 40| int OH_Pasteboard_Unsubscribe(OH_Pasteboard* pasteboard, int type, const OH_PasteboardObserver* observer) | Unsubscribes from the pasteboard observer. | 41| bool OH_Pasteboard_IsRemoteData(OH_Pasteboard* pasteboard) | Checks whether the pasteboard data comes from remote devices. | 42| int OH_Pasteboard_GetDataSource(OH_Pasteboard* pasteboard, char* source, unsigned int len) | Obtains the pasteboard data source. | 43| bool OH_Pasteboard_HasType(OH_Pasteboard* pasteboard, const char* type) | Checks whether the pasteboard contains data of the specified type. | 44| bool OH_Pasteboard_HasData(OH_Pasteboard* pasteboard) | Checks whether the pasteboard contains data. | 45| OH_UdmfData* OH_Pasteboard_GetData(OH_Pasteboard* pasteboard, int* status) | Obtains data from the pasteboard. | 46| int OH_Pasteboard_SetData(OH_Pasteboard* pasteboard, OH_UdmfData* data) | Writes data to the pasteboard. | 47| int OH_Pasteboard_ClearData(OH_Pasteboard* pasteboard) | Clears data from the pasteboard. | 48| void (\*Pasteboard_Notify)(void\* context, Pasteboard_NotifyType type) | Notifies data changes in the pasteboard. | 49| void (\*Pasteboard_Finalize)(void\* context) | Releases context resources when the pasteboard observer object is destroyed.| 50 51## How to Develop 52 531. Add dynamic link libraries. 54 55 ```CMake 56 # Add the following libraries to **CMakeLists.txt**. 57 libudmf.so 58 libpasteboard.so 59 ``` 60 612. Include header files. 62 63 ```c 64 #include <cstdio> 65 #include <database/pasteboard/oh_pasteboard.h> 66 #include <database/udmf/udmf.h> 67 #include <database/udmf/uds.h> 68 ``` 69 703. Define the callback for listening for pasteboard data changes. 71 72 ```c 73 // Define the callback for notifying data changes in the pasteboard. 74 static void Pasteboard_Notify_impl2(void* context, Pasteboard_NotifyType type) 75 { 76 printf("Pasteboard_NotifyType, type: %d", type); 77 } 78 // Define the callback for notifying when the pasteboard observer object is destroyed. 79 static void Pasteboard_Finalize_impl2(void* context) 80 { 81 printf("callback: Pasteboard_Finalize"); 82 } 83 ``` 84 854. Subscribe to the pasteboard observer. 86 87 ```c 88 // 1. Create a pasteboard instance. 89 OH_Pasteboard* pasteboard = OH_Pasteboard_Create(); 90 // 2. Create a pasteboard observer instance. 91 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create(); 92 // 3. Set two callbacks to the pasteboard observer instance. 93 OH_PasteboardObserver_SetData(observer, (void* )pasteboard, Pasteboard_Notify_impl2, Pasteboard_Finalize_impl2); 94 // 4. Subscribe to local data changes on the pasteboard. 95 OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer); 96 ``` 97 985. Write data to the pasteboard. 99 100 ```c 101 // 1. Create a pasteboard instance. 102 OH_Pasteboard* pasteboard = OH_Pasteboard_Create(); 103 104 // 2. Create an OH_UdmfRecord object and add text data to it. 105 OH_UdsPlainText* plainText = OH_UdsPlainText_Create(); 106 OH_UdsPlainText_SetContent(plainText, "Hello world!"); 107 OH_UdmfRecord* record = OH_UdmfRecord_Create(); 108 OH_UdmfRecord_AddPlainText(record, plainText); 109 110 // 3. Create an OH_UdmfData object and add OH_UdmfRecord to it. 111 OH_UdmfData* data = OH_UdmfData_Create(); 112 OH_UdmfData_AddRecord(data, record); 113 114 // 4. Write data to the pasteboard. 115 OH_Pasteboard_SetData(pasteboard, data); 116 117 // 5. Destroy the pointer when the object is no longer required. 118 OH_UdsPlainText_Destroy(plainText); 119 OH_UdmfRecord_Destroy(record); 120 OH_UdmfData_Destroy(data); 121 OH_Pasteboard_Destroy(pasteboard); 122 ``` 123 1246. Read data from the pasteboard. 125 126 ```c 127 // 1. Create a pasteboard instance. 128 OH_Pasteboard* pasteboard = OH_Pasteboard_Create(); 129 // 2. Check whether the pasteboard contains text data. 130 bool hasPlainTextData = OH_Pasteboard_HasType(pasteboard, "text/plain"); 131 if (hasPlainTextData) { 132 // 3. Obtain the unified data OH_UdmfData from the pasteboard. 133 int ret = 0; 134 OH_UdmfData* udmfData = OH_Pasteboard_GetData(pasteboard, &ret); 135 // 4. Obtain the first data record from OH_UdmfData. 136 OH_UdmfRecord* record = OH_UdmfData_GetRecord(udmfData, 0); 137 // 5. Obtain the text data from the data record. 138 OH_UdsPlainText* plainText = OH_UdsPlainText_Create(); 139 OH_UdmfRecord_GetPlainText(record, plainText); 140 const char* content = OH_UdsPlainText_GetContent(plainText); 141 printf("Get plain text success. content: %s", content); 142 // 5. Destroy the pointer when the object is no longer required. 143 OH_UdsPlainText_Destroy(plainText); 144 OH_UdmfData_Destroy(udmfData); 145 } 146 OH_Pasteboard_Destroy(pasteboard); 147 ``` 148