グローバルショートカットとクリップボードへのアクセス
システム全体で使えるキーボードショートカットを登録し、Electronからシステムクリップボードを読み書きして、ネイティブOS連携を拡張します。
「グローバルショートカットとクリップボードへのアクセス」はCoddyKit上の無料Electron Desktop App Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
よくある質問
「グローバルショートカットとクリップボードへのアクセス」レッスンは無料ですか?
はい。「グローバルショートカットとクリップボードへのアクセス」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Electron Desktop App Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Electron Desktop App Developmentコースには全4レッスンが含まれています。
「グローバルショートカットとクリップボードへのアクセス」で何を学びますか?
システム全体で使えるキーボードショートカットを登録し、Electronからシステムクリップボードを読み書きして、ネイティブOS連携を拡張します。 ブラウザで直接実行するハンズオンコードでElectron Desktop App Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Electron Desktop App Developmentを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのElectron Desktop App Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「グローバルショートカットとクリップボードへのアクセス」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このElectron Desktop App Developmentレッスンでコードを書いて実行できますか?
はい。すべてのElectron Desktop App Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ネイティブメニューとコンテキストメニュー
- ダイアログと通知
- シェル連携
- グローバルショートカットとクリップボードへのアクセス