Any good implementations of Fogbugz-style keyboard shortcuts?
Asked Answered
T

2

8

Fogbugz has a very nice implementation of keyboard shortcuts.

You hit CTRL+; and the next key you hit will correspond to a user interface element on the current page.

This neatly avoids conflicting with existing browser keyboard shortcuts (as would be the case it you simply tried to add CTRL+A, CTRL+B.. style shortcuts).

Even better, after you hit CTRL+; "Little yellow tags will appear over each action with its shortcut." So you have an instant keyboard shortcut reference at all times.

Full detail here: http://fogbugz.stackexchange.com/questions/4310

Has anyone seen a jQuery based (or other) implementation lying around that we could use?

Ternan answered 2/11, 2010 at 5:49 Comment(0)
G
1

There are a few things you can do to achieve this. To clarify, you would like to be able to use the keyboard combo Ctrl + ; to allow the next keydown to trigger a piece of code? If this is what you are looking for, you could do something like this: (in jQuery)

// For getting the function corresponding to the key
    function getPosition(arrayName,arrayItem)
    {
      for(var i=0;i<arrayName.length;i++){ 
       if(arrayName[i]==arrayItem)
            return i;
      }
    }
        // Written By Ben Bales


    var ctrlDown = false; // Tells if the cotrol button is currently down
    var semicolonDown = false; // Tells if the semicolon button is down
    var allowShortcut = false; // Allows the key following ctrl+; to run a function
    var keyTimeout = 800; // Allows the following keystroke within this amount of milliseconds after ctrl+;

    // reset allowShortcut
    function disableShortcut()
    {
         allowShortcut = false;
        console.log("dead");

    }    

    $(document).keydown(function(e){
        if(e.keyCode == 17) ctrlDown = true;
        if(e.keyCode == 186 || e.keyCode == 59) semicolonDown = true;
         //console.log("Semicolon: "+semicolonDown);
         //console.log("Ctrl: "+ctrlDown);


        // If both keys are down, allow a following key to do something


        if(allowShortcut == true)
        {
            var key = e.keyCode;
            //alert(key);

                var actionPos = getPosition(keyArray,key);
                actionArray[actionPos]();
                allowShortcut = false;

        }

        if(ctrlDown == true && semicolonDown == true)
        {
            allowShortcut = true;
            // after a certian amount of time dont allow the following key to do something
            setTimeout(disableShortcut,keyTimeout);
        }

    });

    $(document).keyup(function(e){
        if(e.keyCode == 17) ctrlDown = false;
        if(e.keyCode == 186 || e.keyCode == 59) semicolonDown = false;
    });

    // You may want to put your shortcuts in arrays with corresponding keyCodes like so:

    var actionArray = new Array(doSomething,doSomethingelse);

    var keyArray = new Array(65,66);

    // sample actions

    function doSomething()
    {
        alert("did something");
    }

    function doSomethingelse()
    {
       alert("did something else");
    }

I just made this quickly and haven't tested it but hopefully you will understand what it is trying to do. But then again it's only a 15 year olds solution. If you set it up in an html template and try it out, try Ctrl + ; and then hit the A or B key to run their corresponding functions.

Let me know if you need any more help!

Godfree answered 19/1, 2011 at 22:3 Comment(1)
ignore the console.log()'s (i used them for debugging)Godfree
K
0

The JQ Hotkeys plugin might be a good match...

http://code.google.com/p/js-hotkeys/

Kelson answered 2/11, 2010 at 8:31 Comment(1)
It doesn't appear to do much except bind key presses to event handlers, unless I'm missing something?Ternan

© 2022 - 2024 — McMap. All rights reserved.