Kenci
Kenci

Reputation: 4882

How to check if a user is holding a key down while he pushes another key?

I have a function that directs a user to another page if the user pushes the right or left key on the keyboard. I need it to check if the user is holding down the CTRL key at the same time, while he pushes left or right, to redirect him. How can i do this?

My code:

$(document).keydown(function (e) {
    var code = (e.keyCode ? e.keyCode : e.which);

    var currentPage = $("#CurrentPage").val();
    var numberOfPges = $("#NumberOfPages").val();

    if (code == 39) {

        if (currentPage < numberOfPges) {
            // Næste side
            changePage(parseInt(currentPage) + 1);
            $(document).focus();
        }
        else {            
        // Send user to the next category 
        // $("#nextCategory")
        }

    }
    else if (code == 37) {
        // Tilbage
        if ((parseInt(currentPage) - 1) != 0) {
            changePage(parseInt(currentPage) - 1);
            $(document).focus();
        }
        else {
        // Send user to the previous category
        // $("#previousCategory")
        }

    }
});

Thanks!

Upvotes: 1

Views: 401

Answers (1)

Tetaxa
Tetaxa

Reputation: 4403

You can check the e.ctrlKey property

Upvotes: 2

Related Questions