全局快捷键与剪贴板访问
通过注册系统级键盘快捷键,以及从 Electron 中读取或写入系统剪贴板,扩展原生 OS 集成能力。
全局快捷键与剪贴板访问 是 CoddyKit 上的免费 Electron Desktop App Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Electron Desktop App Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Electron Desktop App Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Reaching Beyond the Window
Native apps often respond even when not focused. Global shortcuts and clipboard access make your Electron app feel truly native.
The globalShortcut Module
Use globalShortcut in the main process to listen for key combos system-wide, even when your app is in the background.
const { globalShortcut } = require('electron');
app.whenReady().then(() => {
globalShortcut.register('CommandOrControl+Shift+K', () => {
console.log('shortcut fired');
});
});Accelerator Strings
Shortcuts are described with accelerator strings like CommandOrControl+Shift+I, which map correctly across macOS and Windows.
function normalize(accel) {
return accel.replace('CommandOrControl', process.platform === 'darwin' ? 'Cmd' : 'Ctrl');
}
console.log(normalize('CommandOrControl+S'));Checking Registration
Use isRegistered() to avoid conflicts and verify your shortcut actually bound.
if (globalShortcut.isRegistered('CommandOrControl+Shift+K')) {
console.log('active');
}Unregistering Shortcuts
Always clean up on quit with globalShortcut.unregisterAll() in the will-quit event.
app.on('will-quit', () => {
globalShortcut.unregisterAll();
});The clipboard Module
The clipboard module reads and writes text, HTML, and images to the system clipboard.
const { clipboard } = require('electron');
clipboard.writeText('Copied from Electron');
console.log(clipboard.readText());Reading Clipboard Text
clipboard.readText() grabs whatever text the user last copied, enabling paste-aware features.
function preview(text) {
return text.length > 20 ? text.slice(0, 20) + '...' : text;
}
console.log(preview('A very long clipboard string here'));Clipboard Formats
Beyond text, you can read readHTML() and readImage(). Check availableFormats() first.
Combining Both Features
A common pattern: a global shortcut captures the current clipboard text and processes it instantly, like a quick-translate tool.
globalShortcut.register('CommandOrControl+Alt+T', () => {
const text = clipboard.readText();
process(text);
});Respecting the User
Global shortcuts are powerful but intrusive. Make them configurable and avoid hijacking common OS combos.
Security Note
The clipboard may hold sensitive data like passwords. Read it only when the user explicitly triggers an action.
Quick Check
Test your knowledge of global shortcuts.
Recap
You learned to register system-wide shortcuts with globalShortcut, read and write the system clipboard, combine them for native workflows, and handle cleanup and security responsibly.
用 AI 导师学习 JavaScript — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 47
常见问题解答
「全局快捷键与剪贴板访问」课时是免费的吗?
是的 — 「全局快捷键与剪贴板访问」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Electron Desktop App Development 课程的其余内容,请升级到 CoddyKit PRO。 Electron Desktop App Development 课程共包含 4 节课。
「全局快捷键与剪贴板访问」这节课中我会学到什么?
通过注册系统级键盘快捷键,以及从 Electron 中读取或写入系统剪贴板,扩展原生 OS 集成能力。 你通过在浏览器中直接运行的动手代码来练习 Electron Desktop App Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Electron Desktop App Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Electron Desktop App Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「全局快捷键与剪贴板访问」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Electron Desktop App Development 课中编写并运行代码吗?
能。每节 Electron Desktop App Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 原生菜单与上下文菜单
- 对话框与通知
- Shell 集成
- 全局快捷键与剪贴板访问