]> khamidou — GitHub mirrors - electron.git/commitdiff
Update sample code in global-shortcut.md
authorHank Bao <hankbao84@gmail.com>
Thu, 9 Jul 2015 09:03:58 +0000 (17:03 +0800)
committerHank Bao <hankbao84@gmail.com>
Thu, 9 Jul 2015 09:03:58 +0000 (17:03 +0800)
docs/api/global-shortcut.md

index 95fd5caf1fe8f8f3c96b953a5d2db275b40dabcf..d9a77ce6ce309cf25be31963a7772322a5e15304 100644 (file)
@@ -6,23 +6,30 @@ Note that the shortcut is global, even if the app does not get focused, it will
 You should not use this module until the ready event of app module gets emitted.
 
 ```javascript
+var app = require('app');
 var globalShortcut = require('global-shortcut');
 
-// Register a 'ctrl+x' shortcut listener.
-var ret = globalShortcut.register('ctrl+x', function() { console.log('ctrl+x is pressed'); })
+app.on('ready', function() {
+    // Register a 'ctrl+x' shortcut listener.
+    var ret = globalShortcut.register('ctrl+x', function() {
+        console.log('ctrl+x is pressed');
+    })
 
-if (!ret) {
-  console.log('registration failed');
-}
+    if (!ret) {
+      console.log('registration failed');
+    }
 
-// Check whether a shortcut is registered.
-console.log(globalShortcut.isRegistered('ctrl+x'));
+    // Check whether a shortcut is registered.
+    console.log(globalShortcut.isRegistered('ctrl+x'));
+});
 
-// Unregister a shortcut.
-globalShortcut.unregister('ctrl+x');
+app.on('will-quit', function() {
+    // Unregister a shortcut.
+    globalShortcut.unregister('ctrl+x');
 
-// Unregister all shortcuts.
-globalShortcut.unregisterAll();
+    // Unregister all shortcuts.
+    globalShortcut.unregisterAll();
+});
 ```
 
 ## globalShortcut.register(accelerator, callback)