Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3315602
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:22:10+00:00 2026-05-17T22:22:10+00:00

I’m curious how i, with the following jQuery plugin code im writing at the

  • 0

I’m curious how i, with the following jQuery plugin code im writing at the bottom of this question, could implement key combos. How it’s working so far is it allows a user to create key commands simply by doing a normal jQuery like syntax and provide an event for the key command, like so:

$(window).jkey('a',function(){
   alert('you pressed the a key!');
});

or

$(window).jkey('b c d',function(){
   alert('you pressed either the b, c, or d key!');
});

and lastly what i want is the ability to do, but can’t figure out:

$(window).jkey('alt+n',function(){
   alert('you pressed alt+n!');
});

I know how to do this outside of the plugin (on keyup set a var false and on keydown set the var true and check if the var is true when you press the other key), but i don’t know how to do this when you dont know what keys are going to be pressed and how many. How do I add this support? I want to be able to allow them to do things like alt+shift+a or a+s+d+f if they wanted. I just can’t get my head around how to implement this. Any ideas?

I’m going to release this as an open source plugin and i’d love to give whoever gives me the right, working, answer some credit on the blog post and in the code it’s self. Thanks in advance!

(function($) {
  $.fn.jkey = function(keyCombo,callback) {
    if(keyCombo.indexOf(' ') > -1){ //If multiple keys are selected
        var keySplit = keyCombo.split(' ');
    }
    else{ //Else just store this single key
        var keySplit = [keyCombo];
    }
    for(x in keySplit){ //For each key in the array...

        if(keySplit[x].indexOf('+') > -1){
            //Key selection by user is a key combo... what now?
        }
        else{
            //Otherwise, it's just a normal, single key command
        }

        switch(keySplit[x]){
            case 'a':
                keySplit[x] = 65;
                break;
            case 'b':
                keySplit[x] = 66;
                break;
            case 'c':
                keySplit[x] = 67;
                break;
            //And so on for all the rest of the keys
        }
    }
    return this.each(function() {
        $this = $(this);
        $this.keydown(function(e){
            if($.inArray(e.keyCode, keySplit) > -1){ //If the key the user pressed is matched with any key the developer set a key code with...
                if(typeof callback == 'function'){ //and they provided a callback function
                    callback(); //trigger call back and...
                    e.preventDefault(); //cancel the normal
                }
            }
        });
    });
  }
})(jQuery);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:22:11+00:00Added an answer on May 17, 2026 at 10:22 pm

    Here’s what I came up with. Essentially what I did was created a JSON object that stores all the key codes. I then replace all the provided keys with the codes. If the keys are using the ‘+’ to make a key combo, I then create an array of the codes out of it.

    We then create another array that stores all the keys that are being pressed (keyDown add the item, keyUp removes it). On keyDown, we check if it’s a single key command or combo. If it’s a combo, we check it against all the currently active key presses. If they all match, we execute the callback.

    This will work with any number of key combos. Only time I saw that it wasn’t working is when you use the ‘alert()’ to display a message on the key combo because it will no longer remove the items from the active key press array.

    (function($) { 
      $.fn.jkey = function(keyCombo,callback) {
    
        // Save the key codes to JSON object
        var keyCodes = { 
          'a'   : 65,
          'b'   : 66,
          'c'   : 67,
          'alt' : 18
        };
    
        var x = '';
        var y = '';
    
        if(keyCombo.indexOf(' ') > -1){ //If multiple keys are selected
            var keySplit = keyCombo.split(' ');
        }
        else{ //Else just store this single key
            var keySplit = [keyCombo];
        }
    
        for(x in keySplit){ //For each key in the array...
    
          if(keySplit[x].indexOf('+') > -1){
            //Key selection by user is a key combo
            // Create a combo array and split the key combo
            var combo = Array();
            var comboSplit = keySplit[x].split('+');
    
            // Save the key codes for each element in the key combo
            for(y in comboSplit){
              combo[y] = keyCodes[ comboSplit[y] ];
            }
    
            keySplit[x] = combo;
    
          } else {
            //Otherwise, it's just a normal, single key command
            keySplit[x] = keyCodes[ keySplit[x] ];
          }
    
        }
    
        return this.each(function() {
            $this = $(this);
    
            // Create active keys array
            // This array will store all the keys that are currently being pressed
            var activeKeys = Array();
    
            $this.keydown(function(e){
    
              // Save the current key press
              activeKeys[ e.keyCode ] = e.keyCode;
    
              if($.inArray(e.keyCode, keySplit) > -1){ // If the key the user pressed is matched with any key the developer set a key code with...
    
                if(typeof callback == 'function'){ //and they provided a callback function
                  callback(); //trigger call back and...
                  e.preventDefault(); //cancel the normal
                }
    
              } else { // Else, the key did  not match which means it's either a key combo or just dosn't exist
    
                // Check if the individual items in the key combo match what was pressed
                for(x in keySplit){
                  if($.inArray(e.keyCode, keySplit[x]) > -1){
    
                    // Initiate the active variable
                    var active = 'unchecked';
    
                    // All the individual keys in the combo with the keys that are currently being pressed
                    for(y in keySplit[x]) {
                      if(active != false) {
                        if($.inArray(keySplit[x][y], activeKeys) > -1){
                          active = true;
                        } else {
                          active = false;
                        }
                      }
                    }
    
                    // If all the keys in the combo are being pressed, active will equal true
                    if(active === true){
                      if(typeof callback == 'function'){ //and they provided a callback function
                        callback(); //trigger call back and...
                        e.preventDefault(); //cancel the normal
                      }
                    }
                  }
                }
    
              } // end of if in array
    
            }).keyup(function(e) {
              // Remove the current key press
              activeKeys[ e.keyCode ] = '';
            });
    
        });
    
      }
    })(jQuery);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.