捷徑
Shortcut
代表一個全域鍵盤捷徑,也稱為系統範圍的熱鍵。如果註冊成功,即使您的應用程式沒有焦點,它也能運作。
捷徑
繼承自 EventEmitter
。每次使用者按下已註冊的捷徑時,您的應用程式將會收到捷徑物件的 active
事件。
概要
var option = { key : "Ctrl+Shift+A", active : function() { console.log("Global desktop keyboard shortcut: " + this.key + " active."); }, failed : function(msg) { // :(, fail to register the |key| or couldn't parse the |key|. console.log(msg); } }; // Create a shortcut with |option|. var shortcut = new nw.Shortcut(option); // Register global desktop shortcut, which can work without focus. nw.App.registerGlobalHotKey(shortcut); // If register |shortcut| successfully and user struck "Ctrl+Shift+A", |shortcut| // will get an "active" event. // You can also add listener to shortcut's active and failed event. shortcut.on('active', function() { console.log("Global desktop keyboard shortcut: " + this.key + " active."); }); shortcut.on('failed', function(msg) { console.log(msg); }); // Unregister the global desktop shortcut. nw.App.unregisterGlobalHotKey(shortcut);
new Shortcut(選項)
選項
{物件}
鍵
{字串}
捷徑的按鍵組合,例如"ctrl+shift+a"
。有關詳細資訊,請參閱 shortcut.key 屬性。active
{函式}
選用 當熱鍵被觸發時的回呼函式。有關詳細資訊,請參閱 shortcut.active 屬性。failed
{函式}
選用 當無法註冊熱鍵時的回呼函式。有關詳細資訊,請參閱 shortcut.failed 屬性。
建立新的 捷徑
,選項
是包含 捷徑
初始設定的物件。
shortcut.key
取得 捷徑
的 鍵
。它是一個字串,用於指定捷徑鍵,例如 "Ctrl+Alt+A"
。此鍵由零個或多個修飾鍵和鍵盤上的按鍵組成。只支援一個按鍵代碼。按鍵代碼不區分大小寫。
支援的修飾鍵清單
Ctrl
Alt
Shift
Command
:Command
修飾鍵對應到 Mac 上的 Apple 鍵 (⌘),並對應到 Windows 和 Linux 上的 Windows 鍵。
支援的按鍵清單
- 字母:
A
-Z
- 數字:
0
-9
- 功能鍵:
F1
-F24
逗號
句點
Tab
Home
/End
/PageUp
/PageDown
/Insert
/Delete
Up
/Down
/Left
/Right
MediaNextTrack
/MediaPlayPause
/MediaPrevTrack
/MediaStop
逗號
或,
句點
或.
Tab
或\t
反引號
或`
Enter
或\n
減號
或-
等於
或=
反斜線
或\
分號
或;
引號
或'
左括號
或[
右括號
或]
跳脫
- DOM Level 3 W3C KeyboardEvent 程式碼值
單一按鍵,無修改鍵
API App.registerGlobalHotKey()
可以支援應用程式攔截單一按鍵(例如 { key: "A"}
)。但使用者將無法再正常使用「A」,直到應用程式註銷為止。不過,API 並未限制此用法,如果應用程式想要監聽媒體按鍵,這將會很有用。
只有在知道自己在做什麼時,才使用零修改鍵。
shortcut.active
取得或設定 Shortcut
的 active
回呼。當使用者按下捷徑鍵時,將會呼叫它。
shortcut.failed
*取得或設定 Shortcut
的 failed
回呼。當應用程式傳遞無效按鍵,或無法註冊按鍵時,將會呼叫它。
事件:active
與 shortcut.active
相同
事件:failed
與 shortcut.failed
相同