Khalob C
Khalob C

Reputation: 147

UnityScript: How can I detect arrow key presses (up/down)

I'm pretty new with UnityScript and I need help. I'm creating a menu for a game we made in unity and i don't know how to retrieve the up/down arrow key presses from my keyboard. I want to scroll through the menu by pressing up and down. So i'd like to do something like: "if up is pressed, then subtract 1 from CurrentNumberOfMenu." Anyways, hopefully I've given enough information about my problem; If you need some more information just ask.

Upvotes: 2

Views: 2386

Answers (4)

CrazyRedDude
CrazyRedDude

Reputation: 11

if(Input.GetKeyDown(KeyCode.UpArrowKey)) {
     //Example
     print("You pressed the up arrow key!")
}

//I hope that you like this!

Upvotes: 1

Sunil Verma
Sunil Verma

Reputation: 2500

document.attachEvent("onkeypress", win_onkeydown_handler);
function win_onkeydown_handler() {
    switch (event.keyCode) {
        case 116 : // 'F5'
            event.returnValue = false;
            event.keyCode = 0;
            break;  

        case 27: // 'F5'
            event.returnValue = false;
            event.keyCode = 0;
            break;

        case 08: // 'BackSpace'
            if (event.srcElement.tagName == "INPUT"
                || event.srcElement.tagName == "TEXTAREA") {
            } else {
                event.returnValue = false;
                event.keyCode = 0;
            }
            break;
    }
}

use event code as needed, i.e replace codes with the ascii value of desired key, you can get this by alert in the function above.

Upvotes: 0

Bala
Bala

Reputation: 510

try this

$(document).keydown(function(e){
    if (e.keyCode == 38) { 
       alert( "up pressed" );
    }
    else if (e.keyCode == 40) { 
       alert( "down pressed" );
    }
    else if (e.keyCode == 37) { 
       alert( "left pressed" );
    }
    else if (e.keyCode == 39) { 
       alert( "right pressed" );
    }
    return false;
});

Upvotes: 2

Wyatt
Wyatt

Reputation: 2519

The window.onkeypress event is the answer, look for specific charcodes (up arrow is 38, down is 40).

Upvotes: 0

Related Questions