Giter VIP home page Giter VIP logo

jquery.hotkeys's Introduction

#About jQuery Hotkeys is a plug-in that lets you easily add and remove handlers for keyboard events anywhere in your code supporting almost any key combination.

This plugin is based off of the plugin by Tzury Bar Yochay: jQuery.hotkeys

The syntax is as follows:

$(expression).bind(types.keys, handler);
$(expression).unbind(types.keys, handler);

$(document).bind('keydown.ctrl_a', fn);

// e.g. replace '$' sign with 'EUR'
$('input.foo').bind('keyup.$', function(){
  this.value = this.value.replace('$', 'EUR');
});

Types

Supported types are 'keydown', 'keyup' and 'keypress'

Notes

If you want to use more than one modifiers (e.g. ctrl+alt+z) you should define them by an alphabetical order e.g. alt_ctrl_z

Hotkeys aren't tracked if you're inside of an input element (unless you explicitly bind the hotkey directly to the input). This helps to avoid conflict with normal user typing.

All keys that have special meaning in RegExp strings (\.+*?^$[](){}/'#) can't be removed by using the namespaced event until this jQuery bug get's fixed: http://bugs.jquery.com/ticket/11458 the colon (.) will never work (even if bug is fixed) since it is used to separate multiple namespaces. To remove them you will need to remove events to the whole event type:

// won't work
$('#foo').unbind('keyup.$');
$('#foo').unbind('keyup.+');

//will work
$('#foo').unbind('keyup');
$('#foo').unbind('keyup.a');
$('#foo').unbind('keyup.ctrl_s');

jQuery Compatibility

Works with jQuery 1.4.2 and newer.

It known to be working with all the major browsers on all available platforms (Win/Mac/Linux)

  • IE 6/7/8
  • FF 1.5/2/3
  • Opera-9
  • Safari-3
  • Chrome-0.2

Addendum

Firefox is the most liberal one in the manner of letting you capture all short-cuts even those that are built-in in the browser such as Ctrl_t for new tab, or Ctrl_a for selecting all text. You can always bubble them up to the browser by returning true in your handler.

Others, (IE) either let you handle built-in short-cuts, but will add their functionality after your code has executed. Or (Opera/Safari) will not pass those events to the DOM at all.

So, if you bind Ctrl_Q or Alt_F4 and your Safari/Opera window is closed don't be surprised.

jquery.hotkeys's People

Contributors

brunowinck avatar chustar avatar madpin avatar millermedeiros avatar sprsquish avatar tzuryby avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery.hotkeys's Issues

Can't work when use it in Input and there typed Chinese chars.

for example:


<textarea id="cell_comment"></textarea>

<script type="text/javascript">
$("#cell_comment").bind("keyup", "ctrl+return", function(){
$("#cell_comments_form").submit();
});
</script>

When the "cell_comment" textarea typed English chars, it can work fine. But if I put the Chinese text like "某某某", this can't be work.

Uncaught TypeError: Cannot read property 'keys' of undefined

first up, thanks for this great plugin

I was getting the above error in a project and added this to the top of the keyhandler function

if (typeof handleObj.data == 'undefined') {
return;
}

problem went away, plugin still worked, didn't look any deeper

Function called on all events

The plugin is calling the callback when I press any of the following keys: CTRL, SHIFT or ALT. The plugin is not working as expected by only calling the callback when I press CTRL + L.

This is my code: jQuery(document).bind('keydown', 'ctrl+l', function(){alert("HotKey");});

IE8 still propagates events

I can't seem to get the shortcuts mapped to browser commands (alt+h, alt+d) to stop firing after my event is handled. I am having this issue in IE8, but not Firefox. I was having the issue in a project I was working on, so I stepped back and tried the test-static-01.html demo on two of my home machines and they both behaved the same (events still fired). Here is my bind logic:

jQuery(document).bind('keydown', 'Alt+h',function (evt){
jQuery('#_Alt_h').addClass('dirty'); evt.cancelBubble=true; evt.returnValue=false; evt.keyCode=0; return false; }); });

User Customizable Hotkeys

This code looks like a really nice beginning to allow people to add their own customizable hotkeys for various elements of a web page. Things like searching a site or getting to a sitemap.

It's been a problem in the past with accessibility issues and access keys that it is difficult to define one set of access keys that

There's some controversy around this:
http://www.wats.ca/show.php?contentid=47
http://en.wikipedia.org/wiki/Access_key

Some have recommended using number keys like:

S: Skip navigation
1: Home Page
2: Blog
3: Site map
5: FAQs
9: Contact us
0: Accessibility (this page)

But ultimately if you allow users to choose whatever combinations they want to access certain common parts of your site, you're going to be away to avoid the pitfalls of choosing them for your users.

So am I right here that this tool should be able to help move us in that direction?

Not working with jQuery 1.11.x

By pressing Ctrl key, the following function executes:

$(document).on('keydown', null, 'Ctrl+s', function(){
    console.log('Ctrl+S');
    return false;
});

it should work by pressing Ctrl+S at same time, but it's working by pressing Ctrl key only. any clue?

Error hotkeys

why every time I press a button (other than the key hotkeys on purpose) stattement still called, in keydown bind hotkeys Ctrl + s in write but every time I press any button command runs? Why
details http://jsfiddle.net/6WJKu/1046/

Unbind now work

$(window).bind('keydown',"Alt+2",function() {
alert(1);
$(window).unbind('keydown',"Alt+2");
});

keypress doesn't fire on special keys

Plugin handles special keys only on keydown/keyup. This is invalid behavior because special key triggers all kinds of events with valid keyCode. Though alphanumeric keys valid charCode available only on keypress events. Here is patch:

                return;
            }

-           // Keypress represents characters, not special keys
-           var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ],
-               character = String.fromCharCode( event.which ).toLowerCase(),
-               key, modif = "", possible = {};
+           var special = jQuery.hotkeys.specialKeys[ event.keyCode ],

+               // characters are represented only in keypress
+               character = event.type === "keypress" && String.fromCharCode( event.which ).toLowerCase(),
+               modif = "", possible = {};
+
            // check combinations (alt|ctrl|shift+anything)
            if ( event.altKey && special !== "alt" ) {
                modif += "alt+";
@@ -73,8 +74,9 @@

            if ( special ) {
                possible[ modif + special ] = true;
+           }

-           } else {
+           if ( character ) {
                possible[ modif + character ] = true;
                possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true;

Don't use namespaces for key identification

Why was this changed in the last version? It is much less error-prone to use eventData for this like:

$(el).on( 'keypress', { key: 'Alt+a' }, fn );

// Or:

$(el).on( 'keypress', [ 'Alt+a' ], fn );

Lots of options, but why namespaces?

Add git tag

Hello @tzuryby ,
I am the member of cdnjs project.
We want to host this library.
Please help us add git tag.
Git tag can help us to know your release version.
And we can auto-update the version when you have new release.
Or you could publish on npm?
Thanks for your help!

cdnjs/cdnjs#6196

No tags on this repo / cannot bower install

This git repo dont have any tags.

According to http://bower.io/docs/creating-packages/ 'Your package should use semver Git tags.'

According to https://www.versioneye.com/javascript/tzuryby:jquery.hotkeys/jeresig:jquery.hotkeys this git repo someday ago had version '1.0'. Is it possible that someone nuked tags of this repo?

Lack of tags is a showstopper for us, as we cannot do bower install on our project, as one of our bower dependencies depends on jquery.hotkeys#1.0 so bower install gives us:

bower ENORESTARGET  No tag found that was able to satisfy >= 1.0

Additional error details:
No versions found in git://github.com/tzuryby/jquery.hotkeys.git

Stack trace:
Error: No tag found that was able to satisfy >= 1.0

Could please someone bring tags back?

Can any one fix the unbind bug?

I download many versions, but the "ctrl+s" bug exists in every version, it can not use unbind method, can you great guys fix the bug? I need the methed for some of my actions.

Enable in Input

This might be a really noob question, but is there a way to enable a bound key to work even when inside input?

I looked all over for this, and found references to a disableInInput function somewhere, though I haven't been able to find how to disable that setting. I'd prefer not to have to rebind every key to the inputs.

Thanks for the help.

JShint complaints

You should use the 'use strict' string on top of the init function as a matter of better js coding practice. This will make the test on line 55 (...prop('contenxteditable') == 'true') complain with jshint because of the == that should be ===

Also, the variable key declared at line 61 is never used in the code. It can be removed.

readme says plugin works with jQuery 1.4.2+ but .prop() only works on 1.6+

$(event.target).prop('contenteditable') == 'true' will fail on jQuery prior to 1.6, it should be written as $(event.target).attr('contenteditable') == 'true' in older versions.

I added a note in the source code about it:

// important to note that $.fn.prop is only available on jquery 1.6+

Even thought about adding some conditional check for the version but decided not to.

Anyway, just documenting it here in case anyone is having this problem (main reason why I updated the test files to use jQuery 1.7.1)

cheers.

Foreign keyboard problem

Just noticed that the test number 4 doesn't work on my german keyboard. I have no dedicated slash key but have to type SHIFT+7 to get it. The event setup for slash doesn't work then...

Please bring back the 'global' option

Please bring back the 'global' option. It was quite useful when creation web applications with shortcuts combinations like 'ctrl+s' to save a form's content even when inside an input field.

Also, the latest version (retrieved from NuGet) breaks compatibility with previous versions.

Is there a provision for dealing with Mac/PC differences? IE, Ctrl vs CMD?

Related: refactory-id/bootstrap-markdown#329

On Mac many/most common hotkeys are CMD based vs Ctrl based...

  • Mac: CMD-B for bold, CMD-I for bold, etc.
  • Windows: Ctrl-B for bold, Ctrl-I for bold, etc.

Does the library have anything built-in to handle this or is it the responsibility of every consumer to handle these differences on their own?

We're using bootstrap-markdown which hard codes "Ctrl", ie: hotkey: 'Ctrl+B', which I'm told does not "just work" on Mac but instead requires Ctrl to be used. I'm asking because this feels like something that would have already come up many times and I wonder if I'm just missing something.

IE10

Does jquery.hotkeys plugin work in IE10? It works in compatibility mode but not otherwise.

Some keys don't work.

This code doesn't work, because the punctuation keys to the right of the numerics above the QWERTY can't be bound:

chars = ['-', '_', 'shift+=', '+'];
for (i in chars) {
    var this_char = chars[i];
    $(document).bind('keyup', chars[i], function(event) {
        console.debug("Pressed " + this_char);
    });
}

IE8 still propagates events

I can't seem to get the shortcuts mapped to browser commands (alt+h, alt+d) to stop firing after my event is handled. I am having this issue in IE8, but not Firefox. I was having the issue in a project I was working on, so I stepped back and tried the test-static-01.html demo on two of my home machines and they both behaved the same (events still fired). Here is my bind logic:

jQuery(document).bind('keydown', 'Alt+h',function (evt){
jQuery('#_Alt_h').addClass('dirty');
evt.cancelBubble=true;
evt.returnValue=false;
evt.keyCode=0;
return false;
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.