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?

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.