Answers for "javascript get arrow key pressed"

4

javascript detect arrow key press

//detecting arrow key presses
document.addEventListener('keydown', function(e) {
    switch (e.keyCode) {
        case 37:
            alert('left');
            break;
        case 38:
           alert('up');
            break;
        case 39:
            alert('right');
            break;
        case 40:
            alert('down');
            break;
    }
});
Posted by: Guest on August-05-2019
1

js detect right key click

//detecting arrow key presses - supports both new and depricated methods
document.addEventListener('keydown', function(e) {
	var key = e.key || e.keyCode;
	switch (key) {
		case 'ArrowLeft': case 37:
			alert('left');
			break;
		case 'ArrowUp': case 38:
			alert('up');
			break;
		case 'ArrowRight': case 39:
			alert('right');
			break;
		case 'ArrowDown': case 40:
			alert('down');
			break;
	}
});
Posted by: Guest on November-08-2020
0

arrow keys function javascript

document.addEventListener('keydown', function(e) {
    switch (e.keyCode) {
        case 37:
            alert('left');
            break;
        case 38:
           alert('up');
            break;
        case 39:
            alert('right');
            break;
        case 40:
            alert('down');
            break;
    }
});
document.onkeydown = checkKey;

function checkKey(e) {

    e = e || window.event;

    if (e.keyCode == '38') {
        // up arrow
    }
    else if (e.keyCode == '40') {
        // down arrow
    }
    else if (e.keyCode == '37') {
       // left arrow
    }
    else if (e.keyCode == '39') {
       // right arrow
    }

}
Posted by: Guest on November-29-2020
2

JAVASCRIPT KEYDOWN AROW

document.onkeydown = checkKey;

function checkKey(e) {

    e = e || window.event;

    if (e.keyCode == '38') {
        // up arrow
    }
    else if (e.keyCode == '40') {
        // down arrow
    }
    else if (e.keyCode == '37') {
       // left arrow
    }
    else if (e.keyCode == '39') {
       // right arrow
    }

}
Posted by: Guest on May-27-2020

Code answers related to "javascript get arrow key pressed"

Code answers related to "Javascript"

Browse Popular Code Answers by Language