There are three methods to detect the key codes of keyboard.
If we are talking about the cross-browser compatible then keyup and keydown are the best solution for this.
Take a look for a quick example:
var code = (e.keyCode || e.which);
if( code == 38 ) alert('Up Arrow');
if( code == 40 ) alert('Down Arrow');
<input name="set_text" size="30" />
And here you can found the Keyboard Key code: Link