AnitaTang / js-hotkeys

Automatically exported from code.google.com/p/js-hotkeys
0 stars 0 forks source link

adding acceskey support #3

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
im currently using this code to enable my accesskeys and allowing my forms
to be submitted by ctrl+return if u like it please include it (just give me
some credit...) hope its readable in this form :)

    $.hotkeys = $.extend($.hotkeys,{
        /**
         * @param {Object} on - selector or jQuery Object - 'div' <-> $('div')
         * @param {Object} options
         *  modifier: Ctrl / Alt / '' ? !Shift = Capital letter! -> Ctrl + A =
Ctrl + Shift + A
         *  target: in which area is this hotkey valid ? -> html <-> div#my_form
(not recommended since it makes users crazy)
         *  type: 'keyup' / 'keypress'
         *  propagate: ??? TODO
         *  disableInInput: will not be accessible while in a input field
         * @param {Object} callback - function to call
         */
        enableAccesskeys : function(on,options,callback){
            //sanitize options
            if(!options)options={};
            options = $.extend({modifier:'Alt'},options);
            if(!options.modifier &&
options.disableInInput==undefined)options.disableInInput = true;//normal
chars work in inputs...
            if(options.modifier)options.modifier+='+';

            //create hotkeys
            $(on).each(function(){
                var key;
                var that = this;
                var self=$(this);
                if(key = self.attr('accesskey')) {
                    var combo = options.modifier+key;
                    self.attr('title',self.attr('title')||'' +'(shortcut: '+combo+')');
                    $.hotkeys.add(combo,options,function(){
                        callback.apply(that);
                    });
                };
            });
        },

        /**
         * @param {Object} modifier Ctrl / Alt / ''
         * @param {Object} inside jQuery selected elements or html element 
         */
        enableStdAccesskeys : function(modifier,inside){
            //1 or 0 parameters
            if(typeof modifier == 'object' || modifier==undefined){
                inside=inside || modifier;
                modifier='Alt';
            }

            //accesskey for inputs

$.hotkeys.enableAccesskeys($(':input',inside),{modifier:modifier},this.inputCall
back);

            //accesskey for links
            //TODO blank/self etc

$.hotkeys.enableAccesskeys($('a',inside),{modifier:modifier},this.linkCallback);
        },
        /**
         * @param {Object} inside jQuery selected elements or html element
         */
        enableFormSubmit: function(inside){
            $('form',inside).each(function(){
                var that = this;
                //so we can have multiple form callbacks
                this.toString=function(){
                    return 'Form: '+$(this).attr('action');
                }
                $.hotkeys.add('Ctrl+Return',{target:this},function(){
                    $(that).submit();
                });
            });
        },

        inputCallback:function(){
            $(this).focus();
        },

        linkCallback:function(){
            $(this).click();
            window.location=$(this).attr('href');
        }
    });

(grosser.michael.....gmail.com - pragmatiker.net)

Original issue reported on code.google.com by grosser....@gmail.com on 25 Sep 2007 at 11:18

GoogleCodeExporter commented 8 years ago
Is this all about adding Modifiers + Special Keys Such as Ctrl+Enter?
If so then I might have a light and simpler approach (will update library soon 
with a
patch for this).
If not please let me know what is missing.

Original comment by Afro.Sys...@gmail.com on 1 Oct 2007 at 3:49

GoogleCodeExporter commented 8 years ago
its about using the accesskey attribute as hotkeys, since most of the hotkeys i
needed where "follow that link" and strl+enter to submit forms. So now there is 
no
need to write a $.hotkeys.add... for each link/input.
Its kind of a making-the-accesskey-work as it should do since this is what this
attribute is for.

Original comment by grosser....@gmail.com on 2 Oct 2007 at 6:58

GoogleCodeExporter commented 8 years ago

Original comment by Afro.Sys...@gmail.com on 21 Sep 2008 at 5:36