剪切板
使用剪贴板插件读取和写入系统剪贴板。
Supported Platforms
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | Only plain-text content support |
ios | | Only plain-text content support |
设置
从安装剪贴板插件开始。
使用项目的包管理器来添加依赖:
npm run tauri add clipboard-manager
yarn run tauri add clipboard-manager
pnpm tauri add clipboard-manager
bun tauri add clipboard-manager
cargo tauri add clipboard-manager
-
在
src-tauri
文件夹中运行以下命令,将插件添加到Cargo.toml
中的项目依赖项中。cargo add tauri-plugin-clipboard-manager -
修改
lib.rs
来初始化插件。src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_clipboard_manager::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
如果你想用 JavaScript 管理剪贴板,还需要安装 npm 包。
npm install @tauri-apps/plugin-clipboard-manageryarn add @tauri-apps/plugin-clipboard-managerpnpm add @tauri-apps/plugin-clipboard-managerdeno add npm:@tauri-apps/plugin-clipboard-managerbun add @tauri-apps/plugin-clipboard-manager
用法
剪贴板插件有 JavaScript 和 Rust 两种版本。
import { writeText, readText } from '@tauri-apps/plugin-clipboard-manager';// 当设置 `"withGlobalTauri": true` 时, 你可以使用// const { writeText, readText } = window.__TAURI__.clipboardManager;
// 将内容写到剪贴板await writeText('Tauri is awesome!');
// 从剪贴板读取内容const content = await readText();console.log(content);// 将 "Tauri is awesome!" 输出到控制台
use tauri_plugin_clipboard_manager::ClipboardExt;
app.clipboard().write_text("Tauri is awesome!".to_string()).unwrap();
// 从剪贴板读取内容let content = app.clipboard().read_text();println!("{:?}", content.unwrap());// 将 "Tauri is awesome!" 输出到终端
Default Permission
No features are enabled by default, as we believe the clipboard can be inherently dangerous and it is application specific if read and/or write access is needed.
Clipboard interaction needs to be explicitly enabled.
This default permission set includes the following:
Permission Table
Identifier | Description |
---|---|
|
Enables the clear command without any pre-configured scope. |
|
Denies the clear command without any pre-configured scope. |
|
Enables the read_image command without any pre-configured scope. |
|
Denies the read_image command without any pre-configured scope. |
|
Enables the read_text command without any pre-configured scope. |
|
Denies the read_text command without any pre-configured scope. |
|
Enables the write_html command without any pre-configured scope. |
|
Denies the write_html command without any pre-configured scope. |
|
Enables the write_image command without any pre-configured scope. |
|
Denies the write_image command without any pre-configured scope. |
|
Enables the write_text command without any pre-configured scope. |
|
Denies the write_text command without any pre-configured scope. |
© 2025 Tauri Contributors. CC-BY / MIT