Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any good implementations of Fogbugz-style keyboard shortcuts?

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?

like image 305
J22 Avatar asked Nov 02 '10 05:11

J22


People also ask

How useful are keyboard shortcuts?

Using keyboard shortcuts instead of the mouse enables you to work faster, more efficiently and with increased precision, thereby saving you time and improving your productivity. As well, for those with mobility or vision disabilities, keyboard shortcuts are indispensable.


1 Answers

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!

like image 56
BBales Avatar answered Oct 07 '22 13:10

BBales