<p>I'm not sure if there's an "official" way yet but I've looked through the source a little bit and I think the most Craft-like way I would do it, following plugin conventions, is to extend Garnish.Base with a new object that attaches the listener. </p>

<pre><code>(function($) {

        var PluginName_KeyCapture = Garnish.Base.extend({

            init: function()
            {
                this.addListener(Garnish.$doc, 'keydown', function(ev)
                {
                    if ((ev.metaKey || ev.ctrlKey) &amp;&amp; ev.keyCode == 66)
                    {
                        ev.preventDefault();
                        console.log('You hit CTRL + B! Good work.');
                    }
                });
            }
        });

        Craft.PluginName_KeyCapture = new PluginName_KeyCapture();
    })(jQuery);
</code></pre>

CC BY-SA 3.0
2014-07-25T08:59:21.013Z
2014-07-25T08:59:21.013Z
Mike Pepper
<p>For example, when editing an entry <code>cmd + s</code> saves the entry. Is there an easy way to extend Garnish to allow for my plugin to listen for custom keyboard shortcuts?</p>

<plugin-development><javascript><garnish>
Can Garnish be extended to allow plugins to use custom keyboard shortcuts?
# Can Garnish be extended to allow plugins to use custom keyboard shortcuts?

(Asked by: username_0 on 2014-07-25)

For example, when editing an entry `cmd + s` saves the entry. Is there an easy way to extend Garnish to allow for my plugin to listen for custom keyboard shortcuts?

**Tags:** plugin-development, javascript, garnish


---

## 1 answers

### Answer by username_1 on 2014-07-25. Score: 3

I'm not sure if there's an "official" way yet but I've looked through the source a little bit and I think the most Craft-like way I would do it, following plugin conventions, is to extend Garnish.Base with a new object that attaches the listener. 
(function($) {

        var PluginName_KeyCapture = Garnish.Base.extend({

            init: function()
            {
                this.addListener(Garnish.$doc, 'keydown', function(ev)
                {
                    if ((ev.metaKey || ev.ctrlKey) && ev.keyCode == 66)
                    {
                        ev.preventDefault();
                        console.log('You hit CTRL + B! Good work.');
                    }
                });
            }
        });

        Craft.PluginName_KeyCapture = new PluginName_KeyCapture();
    })(jQuery);


---

