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 8943087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:41:17+00:00 2026-06-15T11:41:17+00:00

Any ideas what here’s wrong? Can’t figure it out… It says that arr is

  • 0

Any ideas what here’s wrong? Can’t figure it out… It says that arr is not equal to [17, 67]

var key = function(keys, fn) {
    var arr = [];
    $(document).on({
        keydown: function(e) {
            arr.push(e.which);
            if (arr === keys) {
                fn();
            }
        },
        keyup: function() {
            arr = [];
        }
    });
};

// ctrl + c
key([17, 67], function() {
    alert(true);
});

Here’s a fiddle

Update

Since Cymen‘s answer it works, however if you press ctrl + a and then ctrl + alt + a or vice versa you have to fire the second shortcut twice to work, any ideas?

New fiddle.

var key = function(keys, fn) {
    var arr = [];
    $(document).on({
        keydown: function(e) {
            arr.push(e.which);
            for (var i = 0; keys[i]; i++) {
                if (arr[i] !== keys[i]) {
                    return false;
                }
            }
            fn(e);
            arr = [];
        },
        keyup: function() {
            arr = [];
        }
    });
};

// ctrl + c
key([17, 67], function() {
    alert(true);
});

// ctrl + alt + c
key([17, 18, 67], function() {
    alert('ctrl+alt+c');
});
  • 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-06-15T11:41:19+00:00Added an answer on June 15, 2026 at 11:41 am

    You can’t use === to check if arrays are equal by content. Check it out with a JavaScript console:

    [17, 35] === [17, 35]
    false
    
    a = [17, 35]
    [17, 35]
    a === a
    true
    

    So === on arrays is checking the reference not the value. You can use something else like underscore do the comparison or write one yourself if it is some sort of challenge. Or since you’re only comparing two elements, do this:

    var key = function(keys, fn) {
        var arr = [];
        $(document).on({
            keydown: function(e) {
                arr.push(e.which);
                if (arr[0] === keys[0] && arr[1] === keys[1]) {
                    fn();
                    arr = [];  // added this to avoid buggyness
                }
                console.log(arr);
                console.log(keys);
            },
            keyup: function() {
                arr = [];
            }
        });
    };
    
    // ctrl + c
    key([17, 67], function() {
        alert(true);
    });​
    

    http://jsfiddle.net/9LbsD/

    For more than two keys like CTRL+ALT+C

    You need an array comparison that works for variable lengths like this one:
    
    var equalArrays = function(array1, array2) {
        if (array1.length != array2.length) {
            return false;
        }
        for (var i=0; i < array1.length; i++) {
            if (array1[i] !== array2[i]) {
                return false;
            }
        }
        return true;    
    }
    
    var key = function(keys, fn) {
        var arr = [];
        $(document).on({
            keydown: function(e) {
                arr.push(e.which);
                console.log('arr', arr, 'keys', keys, 'match', equalArrays(arr, keys));
                if (equalArrays(arr, keys)) {
                    fn();
                    arr = [];                
                }
                console.log(arr);
                console.log(keys);
            },
            keyup: function() {
                arr = [];
            }
        });
    };
    
    // ctrl + c
    key([17, 67], function() {
        alert(true);
    });
    
    // ctrl + alt + c
    key([17, 18, 67], function() {
        alert('ctrl+alt+c');
    });
    

    http://jsfiddle.net/WZLut/

    A less buggy approach

    We need to keep track of which keys were pressed however a single key can only be pressed once in one combination (I’m assuming) and on keyup we should only remove the key going up not all the keys. So here is a working approach to that:

    var equalArrays = function(array1, array2) {
        return _.isEqual(array1, array2);
    }
    
    var key = function(keys, fn) {
        var arr = [];
        $(document).on({
            keydown: function(e) {
                arr = _.without(arr, e.which);
                arr.push(e.which);
                if (equalArrays(arr, keys)) {
                    fn();             
                }
            },
            keyup: function(e) {
                arr = _.without(arr, e.which);
                arr = _.uniq(arr);
            }
        });
    };
    
    // ctrl + c
    key([17, 67], function() {
        alert('ctrl+c');
    });
    
    // ctrl + alt + c
    key([17, 18, 67], function() {
        alert('ctrl+alt+c');
    });
    

    ​

    http://jsfiddle.net/BgQUA/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code, any ideas why I get this error? private SQLiteDataAdapter DA_Webfiles;
Any ideas what I am doing wrong??? INSERT INTO LondonFixes ('id', 'Metal', 'AmPm', 'GBP',
Any ideas how I can create a join on a table retrieved from data
Any ideas of how best i can implement article revision history for a Java
Any ideas on the correct method of typecasting an Object out of a getAttribute
Anyone have any ideas as to what is going on here? I have a
I'm totally stumped here, so any ideas would be appreciated. I have a RichFaces
Any ideas what's wrong with this? @contacts = current_user.contacts.where(fname = ? OR lname =
Any ideas about escape analysis in dalvik? Or when and if it's planned to
Any ideas how I would go about writing a javascript method to insert an

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.