4

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?

Jason McCallister
  • 2,336
  • 14
  • 27

1 Answers1

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);
Mike Pepper
  • 4,391
  • 17
  • 26