Jump to content

Javascript:OnKeyDown function;issues receiving output of KeyCode value

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
atheium

atheium

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 298 posts
I was looking to create a document which would output which key i was pressing, as well as its keycode.
one, just to learn more about javascript, two for future use as a quick reference tool.

however im having problems getting the KeyCode to return in an alert box, and im unsure as to why.

While searching for an answer the closest thing i found was this. Look on the bottom of the page to find something similar to what i was looking to make:

JavaScript - Detecting keystrokes

i did read his source and found the appropriate function and form however i still dont understand why My attempt failed. while it would be easier just to copy/paste his code into an offline document and call it done i wouldnt truely understand where or why i failed.

i repeatedly searched to find the correct code to tell javascript to give me the keyvalue back but nothing came of it that worked, or rather that i understood fully i suppose.



<html>
<head>
<script type="text/javascript">

function getkey(){
key=keyCode;
alert(key);
}

</script>
</head>
<body onKeyDown=getkey();>

</body>
</html>


i know everything works except javascript returning the keycode to the alert.
11ism.com <my meaningless empty website, HORAH now with link! (thx gamemaker)

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
Why did you not use the event handler to get the character code? KeyCode isn't defined in your script.

<html>
<body>
<script type="text/javascript">
function displaykey(e){
    //Charcode = ASCII, keycode = ASCII + Key codes such as SHIFT/CTRL etc.
    //Key codes: http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
    var keyCode = e.keyCode ? e.keyCode : e.charCode
    alert(keyCode)
}
</script>
<form>
    <input type="text" size="2" maxlength="1" onkeyup="displaykey(event); this.select()" />
</form>

</html>
There are limitations in some browsers which cannot tell you which key is actually being pressed, (Firefox and Opera always return "o") so you will need to convert the ASCII code yourself to a key (such as 32 -> space, 65 -> "A", etc.)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
atheium

atheium

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 298 posts
short answer to your question would be: ive no idea what im doing, and i dont yet have a book on javascript, so learning by trial and error.

thanks for the reply! il poke around with your example and try to google everything i get confused on.
11ism.com <my meaningless empty website, HORAH now with link! (thx gamemaker)