33

I want to make a BBS forum that using many Keyboard events, so which is the best Keyboard event library?

Farid Rn
  • 3,167
  • 5
  • 39
  • 66
zjm1126
  • 63,397
  • 81
  • 173
  • 221
  • "Best" is a hard criteria to match. You might try giving some more detail on what you are trying to accomplish. That way, we can be more specific. – Stephano Mar 01 '10 at 01:55

8 Answers8

34

Just another to throw in the mix. I recently released one called Mousetrap. You can check out examples at http://craig.is/killing/mice

Craig
  • 2,684
  • 27
  • 20
  • It is very easy to implement and works like a charm. How is the license for using it in commercial projects? – Jarne W. Beutnagel Aug 07 '13 at 15:51
  • 2
    It uses the Apache 2.0 license. – Craig Aug 07 '13 at 19:08
  • It's a great library, but with one major weakness: It does not let you specify the container to bind the events to. It always binds to document. Which is why I had to add some extra focus checks into my callbacks. (My site contains two elements, each with different bindings.) – Domi Apr 24 '14 at 12:41
  • 1
    @Domi - As of 1.5 this is now fixed, I believe – sifriday Apr 29 '15 at 12:41
  • Simple to use. Elegant functionality. Small. Plugins. Awesome! – Daghall Jul 28 '15 at 14:05
  • Mousetrap has poor code quality and can't be required in a modern javascript build (https://github.com/ccampbell/mousetrap/issues/283) - highly suggest using keyboardjs – Andy Ray Dec 16 '15 at 18:37
  • in 2019 this Library is broken for Firefox on Windows Systems. – mondjunge Mar 20 '19 at 09:29
13

Try KeyboardJS

its as simple as

KeyboardJS.on('a', function(){ alert('hello!'); });

yet as flexible as

var bindInstance = KeyboardJS.on('ctrl + a, ctrl + b, c', function(event, keysPressedArray, keyComboString){

    //you get the event object
    console.log('event object', event);

    //you get the keys pressed array
    console.log('keys pressed', keysPressedArray);

    //you get the key combo string
    console.log('combo pressed', keyComboString);

    console.log('I will fire when \'ctrl + a\' or \'ctrl +b\' or \'c\' is pressed down');

    //block event bubble
    return false;

}, function(event, keysPressedArray, keyComboString) {

    console.log('I will fire on key up');

    //block event bubble
    return false;

});

you can clear a binding by calling

bindInstance.clear();

you can clear all binds with specific keys with

KeyboardJS.clear('a, b');

Its open source and available on Github. Its comes in ether a global library or an AMD module for RequireJS.

Here's an introduction video.

There, now stop worrying about the keyboard and code your app. ;)

Robert Hurst
  • 8,902
  • 5
  • 42
  • 66
  • Seems like a really great script, but as best I can tell it is not working in IE8, which is sadly a problem for me (on several levels). When I go to http://robertwhurst.github.com/KeyboardJS/ with IE8 it just says "JS is required". – Matthew Nichols Aug 08 '12 at 17:56
  • The demo may not work in IE but the library should. It did a couple builds ago. Did you try the library itself? – Robert Hurst Aug 09 '12 at 18:02
  • I did not. I will give it a try. Is there tweaking that the demo needs to show off that is can handle all of the browsers? I would be happy to fork and tweak and send a pull request on the demo page. – Matthew Nichols Aug 09 '12 at 18:13
  • That would be helpful. I'm really swamped with my current project and barley have time to sleep. If you fix the demo I'm accept your pull request. – Robert Hurst Aug 09 '12 at 22:45
  • This is the only library of the 4-5 I tested that actually worked for my situation (a `div` with `contenteditable="true"`). Thanks @RobertHurst! – duma Aug 07 '13 at 03:07
  • Very nice library but I think the API has changed from "KeyboardJS.bind.key" to "KeyboardJS.on". – Benny Code Mar 06 '14 at 14:53
  • We are using the KeyboardJS lib and it is nice library but it made a HELL to us when we are using dev tools, and it is constantly taking over and REFIRING the old triggered key event, even we do not press it anymore?! :) but some other key, etc, etc => be careful. P.S. that was a bit older version, but still in my opinion really annoying for our clients – mPrinC May 02 '16 at 11:44
  • Also, @RobertHurst it is a good policy that you note in your post that you are author of the library that you are suggesting :) – mPrinC May 02 '16 at 11:45
  • @mPrinC KeyboardJS cannot re-fire DOM events. It does not contain any logic to do so. It simply listens for events. Anything before that is controlled by your browser or other JS code. – Robert Hurst May 05 '16 at 04:24
  • @mPrinC I haven't obfuscated that it's my library, and I'm not sure why that's important. It's my personal solution, and this is my personal answer. I'm certainly not forcing it on anyone, and in fact, I didn't have to share it to begin with. That said, if you are having trouble with it, you're welcome to contact me. I will help you. – Robert Hurst May 05 '16 at 04:33
3

KEYPRESS is focused on game input and supports any key as a modifier, among other features. It's also pre-packaged for Meteor.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
2

From what I have seen Mousetrap is the only library that let's you specify key sequences rather than combinations. This came in very handy for my application.

Michael Cohen
  • 21
  • 1
  • 1
1

This one is pretty good for jquery.

https://github.com/jeresig/jquery.hotkeys

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

Lots of JavaScript libraries come replete with ways of capturing key input and using it to your advantage. It's a good bet that you will be able to find a library to do just that, and nothing else. I don't have a lot of experience there, however.

I've been using Ext for a while now, and their KeyMap class is really easy to work with. Here is a simple example using it.

new Ext.KeyMap(Ext.getDoc(), {
        key: 'abc',
        alt: true,
        handler: function(k, e) {
            var t = Ext.getCmp('tabpanel');
            switch(k) {
                case 65:
                    t.setActiveTab(0);
                    break;
                case 66:
                    t.setActiveTab(1);
                    break;
                case 67:
                    t.setActiveTab(2);
                    break;
            }
        },
        stopEvent: true
    });

This takes class, Ext.TabPanel, and allows the user to push a keyboard key to change tabs, rather than clicking the tabs themselves.

It can, of course, do much more than this. This is a simple example of how it works though.

Stephano
  • 5,716
  • 7
  • 41
  • 57
-1

This one is better if you are starting and if you want to understand how hot-keys work.

https://github.com/UthaiahBollera/HotkeysInJavascriptLibrary

-3

You can try this one...

http://unixpapa.com/js/key.html

NET Experts
  • 1,525
  • 17
  • 35