首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

剪贴板 | clipboard

在系统剪贴板上执行复制和粘贴操作。

过程:主要,渲染器

以下示例显示如何将字符串写入剪贴板:

代码语言:javascript
复制
const {clipboard} = require('electron')
clipboard.writeText('Example String')

在X Window系统上,还有一个选择剪贴板。要操纵它,你需要传递selection给每个方法:

代码语言:javascript
复制
const {clipboard} = require('electron')
clipboard.writeText('Example String', 'selection')
console.log(clipboard.readText('selection'))

Methods

clipboard模块具有以下方法:

注意:实验API将被标记为未来,并且可能会被删除。

clipboard.readText([type])

  • type String (optional)

返回String- 作为纯文本的剪贴板中的内容。

clipboard.writeText(text[, type])

  • text String
  • type String (optional)

text以纯文本的形式写入剪贴板。

clipboard.readHTML([type])

  • type String (optional)

返回String- 作为标记的剪贴板中的内容。

clipboard.writeHTML(markup[, type])

  • markup String
  • type String (optional)

写入markup剪贴板。

clipboard.readImage([type])

  • type String (optional)

返回NativeImage- 剪贴板中的图像内容。

clipboard.writeImage(image[, type])

  • image NativeImage
  • type String (optional)

写入image剪贴板。

clipboard.readRTF([type])

  • type String (optional)

返回String- 作为RTF的剪贴板中的内容。

clipboard.writeRTF(text[, type])

  • text String
  • type String (optional)

将其写入textRTF中的剪贴板。

clipboard.readBookmark() macOS Windows

Returns Object:

  • title String
  • url String

返回包含表示剪贴板中书签的键title和对象url。该titleurl值将是空的字符串时,书签不可用。

clipboard.writeBookmark(title, url[, type]) macOS Windows

  • title String
  • url String
  • type String (optional)

将书签titleurl书签写入剪贴板。

注意: Windows上的大多数应用程序不支持将书签粘贴到它们中,因此您可以使用clipboard.write书签和回退文本写入剪贴板。

代码语言:javascript
复制
clipboard.write({
  text: 'https://electron.atom.io',
  bookmark: 'Electron Homepage'
})

clipboard.readFindText() macOS

返回String- 查找粘贴板上的文本。该方法在从渲染器进程调用时使用同步IPC。无论何时激活应用程序,都会从查找粘贴板重新读取缓存的值。

clipboard.writeFindText(text) macOS

  • text String

text以纯文本形式将其写入查找粘贴板。该方法在从渲染器进程调用时使用同步IPC。

clipboard.clear([type])

  • type String (optional)

清除剪贴板内容。

clipboard.availableFormats([type])

  • type String (optional)

返回String[]- 剪贴板支持的格式数组type

clipboard.has(format[, type]) Experimental

  • format String
  • type String (optional)

返回Boolean- 剪贴板是否支持指定的format

代码语言:javascript
复制
const {clipboard} = require('electron')
console.log(clipboard.has('<p>selection</p>'))

clipboard.read(format) Experimental

  • format String

返回String- format从剪贴板中读取类型。

clipboard.readBuffer(format) Experimental

  • format String

返回Buffer- format从剪贴板中读取类型。

clipboard.writeBuffer(format, buffer[, type]) Experimental

  • format
  • buffer 缓冲
  • type 字符串(可选)

将其写入buffer剪贴板中format

clipboard.write(data[, type])

  • data 目的
    • text 字符串(可选)
    • html 字符串(可选)
    • image NativeImage(可选)
    • rtf 字符串(可选)
    • bookmark字符串(可选) - 网址at的标题text
  • type 字符串(可选)
代码语言:javascript
复制
const {clipboard} = require('electron')
clipboard.write({text: 'test', html: '<b>test</b>'})

写入data剪贴板。

扫码关注腾讯云开发者

领取腾讯云代金券