anormalmouse
anormalmouse

Reputation: 39

Exclude key in "keydown" function

So I want to exclude the escape key from my key down code because i have another "keydown" function that closes the nav by pressing escape.

document.addEventListener('keydown', openNav);

and this is the close code:

document.addEventListener('keydown', function(e){
     if(e.keyCode === 27) {
        closeNav();
    }
});

so basically I want to be able to close the nav using escape and be able to use any other key to open the navigation

Upvotes: 2

Views: 379

Answers (2)

anormalmouse
anormalmouse

Reputation: 39

Thanks for your help everyone I found the answer here is what I did to fix my problem:

document.addEventListener('keydown',function(e){
//27 = escape
    if(e.keyCode === 27) {
        closeNav();
    } else if(e.keyCode !== 27) {
        openNav();
    }
});

Upvotes: 1

hubman
hubman

Reputation: 149

Try with this code, I used if and else if

document.addEventListener('keydown', function(e){
    if(e.keyCode === 27) {
        closeNav();
    } else if(e.keyCode === 32) {//key space
        openNav();
    }
});

Upvotes: 0

Related Questions