Pasteboard System Ability | 剪贴板服务
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 1 年前 | ||
| 10 天前 | ||
| 9 天前 | ||
| 4 个月前 | ||
| 3 年前 | ||
| 3 天前 | ||
| 21 天前 | ||
| 7 个月前 | ||
| 2 天前 | ||
| 4 天前 | ||
| 1 个月前 | ||
| 4 个月前 | ||
| 1 年前 | ||
| 2 年前 | ||
| 1 年前 | ||
| 2 年前 | ||
| 4 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 4 天前 | ||
| 15 天前 | ||
| 3 年前 |
Clipboard Service
Introduction
As a functional component of the miscellaneous subsystem, the Clipboard Service provides the capability to manage the system clipboard, supporting copy and paste functionalities. The system clipboard handles operations for text, hypertext, URIs, and other content types.
Figure 1 Subsystem Architecture Diagram

The Clipboard Service offers functionalities to assist application developers in conveniently and efficiently utilizing clipboard-related operations. Its main components include the Clipboard Management Client and the Clipboard Service.
- The Clipboard Management Client is responsible for managing clipboard interfaces, providing northbound JS APIs to applications. On the application framework side, it creates clipboard data and requests the Clipboard SA (Service Ability) to perform operations such as creating, deleting, querying, converting text, and configuring the clipboard.
- The Clipboard Service manages clipboard events and oversees the lifecycle of the Clipboard SA (startup, destruction, multi-user support, etc.). It executes application requests, notifies clipboard data management, and returns results to the Clipboard Management Client.
Directory
/foundation/distributeddatamgr/pasteboard
├── etc # 组件包含的进程的配置文件
├── figures # 构架图
├── framework # innerKit接口
├── interfaces # 组件对外提供的接口代码
│ └── kits # 对应用提供的接口
├── profile # 组件包含的系统服务的配置文件
├── services # 剪贴板服务实现
│ └── core # 核心代码实现
│ └── test # native测试代码
│ └── zidl # 跨进程通信代码实现
├── utils # 测试或服务使用mock的数据
└──README_zh.md # 使用说明
Description
Interface Description
Table 1 Main Methods Exposed by PasteBoard
Table 2 Main Methods Exposed by SystemPasteboard
setData(data: PasteData, callback: AsyncCallback<void>): void; |
|
setPasteData(data: PasteData, callback: AsyncCallback<void>:void; |
|
表 3 PasteData核心方法一览
replaceRecord(index: number, record: PasteDataRecord): void; |
|
replaceRecordAt(index: number, record: PasteDataRecord): boolean; |
convertToTextV9(): Promise<string>; |
|
表 5 PasteDataProperty参数说明
[ShareOption](https://gitee.com/openharmony/docs/blob/a4ead7d2fe7ac381133ba47c70c9df8cdfa95325/zh-cn/application-dev/reference/apis/js-apis-pasteboard.md#shareoption9) |
表 6 剪贴板核心属性说明
Maximum number of Records that can be contained in a single PasteData. |
||
Table 7 Description of PasteDataRecord Key Attributes
Table 8 ShareOption Enumeration Description
InApp indicates that pasting is only allowed within the same application. |
|
LocalDevice indicates that pasting is only allowed on this device. |
Usage Instructions
Example of using the clipboard module:
// 导入模块
import pasteboard from '@ohos.pasteboard'
//文本拷贝
console.log('Get SystemPasteboard')
var systemPasteboard = pasteboard.getSystemPasteboard()
systemPasteboard.clear()
var textData = 'Hello World!'
console.log('createPlainTextData = ' + textData)
var pasteData = pasteboard.createPlainTextData(textData)
console.log('Writes PasteData to the pasteboard')
systemPasteboard.setPasteData(pasteData)
console.log('Checks there is content in the pasteboard')
assert.equal(systemPasteboard.hasPasteData(), true)
console.log('Checks the number of records')
pasteData = systemPasteboard.getPasteData()
assert.equal(pasteData.getRecordCount(), 1)
console.log('Checks the pasteboard content')
assert.equal(pasteData.getPrimaryText(), textData)
console.log('Checks there is a MIMETYPE_TEXT_PLAIN MIME type of data')
assert.equal(pasteData.hasMimeType(MIMETYPE_TEXT_PLAIN), true)
assert.equal(pasteData.getPrimaryMimeType(), MIMETYPE_TEXT_PLAIN)
//剪贴板变化监听
console.log('Off the content changes')
var systemPasteboard = pasteboard.getSystemPasteboard()
systemPasteboard.off(contentChanges)
systemPasteboard.clear()
var textData = 'Hello World!'
console.log('createUriData = ' + textData)
var pasteData = pasteboard.createUriData(textData)
console.log('Writes PasteData to the pasteboard')
systemPasteboard.setPasteData(pasteData)
console.log('Checks there is content in the pasteboard')
assert.equal(systemPasteboard.hasPasteData(), true)
console.log('Checks the number of records')
pasteData = systemPasteboard.getPasteData()
assert.equal(pasteData.getRecordCount(), 1)
console.log('On the content changes')
systemPasteboard.on(contentChanges)
console.log('Removes the Record')
assert.equal(pasteData.removeRecordAt(0), true)
console.log('Writes PasteData to the pasteboard')
systemPasteboard.setPasteData(pasteData)
console.log('Checks the number of records')
pasteData = systemPasteboard.getPasteData()
assert.equal(pasteData.getRecordCount(), 0)
console.log('Checks there is no content in the pasteboard')
assert.equal(systemPasteboard.hasPasteData(), false)
var textDataNew = 'Hello World!-New'
console.log('createUriData = ' + textDataNew)
var pasteData = pasteboard.createUriData(textDataNew)
console.log('Writes PasteData to the pasteboard')
systemPasteboard.setPasteData(pasteData)
console.log('Checks there is content in the pasteboard')
assert.equal(systemPasteboard.hasPasteData(), true)
console.log('Checks the number of records')
pasteData = systemPasteboard.getPasteData()
assert.equal(pasteData.getRecordCount(), 1)
console.log('Checks the pasteboard content')
assert.equal(pasteData.getRecordAt(0).plainText, textDataNew)
//构建一个自定义类型的剪贴板内容对象
var dataXml = new ArrayBuffer(256);
var pasteData = pasteboard.createData('app/xml', dataXml);
//创建一条自定义数据内容条目
var dataXml = new ArrayBuffer(256);
var pasteDataRecord = pasteboard.createRecord('app/xml', dataXml);
//将一个PasteData中的内容强制转换为文本内容,使用callback异步回调
var record = pasteboard.createUriRecord("dataability:///com.example.myapplication1/user.txt");
record.convertToTextV9((err, data) => {
if (err) {
console.error('Failed to convert to text. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in converting to text. Data: ' + JSON.stringify(data));
});
//将一个PasteData中的内容强制转换为文本内容,使用Promise异步回调
var record = pasteboard.createUriRecord("dataability:///com.example.myapplication1/user.txt");
record.convertToTextV9().then((data) => {
console.info('Succeeded in converting to text. Data: ' + JSON.stringify(data));
}).catch((err) => {
console.error('Failed to convert to text. Cause: ' + JSON.stringify(err));
});
//向当前剪贴板内容中添加一条自定义数据内容条目
var pasteData = pasteboard.createUriData("dataability:///com.example.myapplication1/user.txt");
var dataXml = new ArrayBuffer(256);
pasteData.addRecord('app/xml', dataXml);
//获取剪贴板内容中指定下标的条目
var pasteData = pasteboard.createPlainTextData("hello");
var record = pasteData.getRecord(0);
//检查剪贴板内容中是否有指定的MIME数据类型
var pasteData = pasteboard.createPlainTextData("hello");
var hasType = pasteData.hasType(pasteboard.MIMETYPE_TEXT_PLAIN);
//移除剪贴板内容中指定下标的条目
var pasteData = pasteboard.createPlainTextData("hello");
pasteData.removeRecord(0);
//替换剪贴板内容中指定下标的条目
var pasteData = pasteboard.createPlainTextData("hello");
var record = pasteboard.createUriRecord("dataability:///com.example.myapplication1/user.txt");
pasteData.replaceRecord(0, record);
//清空系统剪贴板内容,使用callback异步回调
systemPasteboard.clearData((err, data) => {
if (err) {
console.error('Failed to clear the pasteboard. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in clearing the pasteboard.');
});
//清空系统剪贴板内容,使用Promise异步回调
systemPasteboard.clearData().then((data) => {
console.info('Succeeded in clearing the pasteboard.');
}).catch((err) => {
console.error('Failed to clear the pasteboard. Cause: ' + JSON.stringify(err));
});
//读取系统剪贴板内容,使用callback异步回调
var systemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.getData((err, pasteData) => {
if (err) {
console.error('Failed to get PasteData. Cause: ' + err.message);
return;
}
var text = pasteData.getPrimaryText();
});
//读取系统剪贴板内容,使用Promise异步回调
var systemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.getData().then((pasteData) => {
var text = pasteData.getPrimaryText();
}).catch((err) => {
console.error('Failed to get PasteData. Cause: ' + err.message);
})
//判断系统剪贴板中是否有内容,使用callback异步回调
systemPasteboard.hasData((err, data) => {
if (err) {
console.error('Failed to check the PasteData. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in checking the PasteData. Data: ' + JSON.stringify(data));
});
//判断系统剪贴板中是否有内容,使用Promise异步回调
systemPasteboard.hasData().then((data) => {
console.info('Succeeded in checking the PasteData. Data: ' + JSON.stringify(data));
}).catch((err) => {
console.error('Failed to check the PasteData. Cause: ' + JSON.stringify(err));
});
//将数据写入系统剪贴板,使用callback异步回调
var pasteData = pasteboard.createPlainTextData("content");
var systemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.setData(pasteData, (err, data) => {
if (err) {
console.error('Failed to set PasteData. Cause: ' + err.message);
return;
}
console.info('Succeeded in setting PasteData.');
});
//将数据写入系统剪贴板,使用Promise异步回调
var pasteData = pasteboard.createPlainTextData("content");
var systemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.setData(pasteData).then((data) => {
console.info('Succeeded in setting PasteData.');
}).catch((err) => {
console.error('Failed to set PasteData. Cause: ' + err.message);
});
Related Repositories
Distributed Data Management Subsystem