Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 07-08-2009
Member
 
Join Date: Aug 2009
Posts: 6
How to use get key codes for javascript?

Hello,

I am learning Javascript. I want to know how can we get the key codes of characters using javascript.

Thanks,
superbK
Reply With Quote
  #2  
Old 07-08-2009
Member
 
Join Date: Apr 2008
Posts: 40
Re: How to use get key codes for javascript?

Use get key codes for javascript

Code:
<script type="text/javascript">

document.onkeyup = KeyCheck;       

function KeyCheck()

{

   var KeyID = event.keyCode;


   switch(KeyID)

   {

      case 16:

      document.Form1.KeyName.value = "Shift";

      break; 

      case 17:

      document.Form1.KeyName.value = "Ctrl";

      break;

      case 18:

      document.Form1.KeyName.value = "Alt";

      break;

      case 19:

      document.Form1.KeyName.value = "Pause";

      break;

      case 37:

      document.Form1.KeyName.value = "Arrow Left";

      break;

      case 38:

      document.Form1.KeyName.value = "Arrow Up";

      break;

      case 39:

      document.Form1.KeyName.value = "Arrow Right";

      break;

      case 40:

      document.Form1.KeyName.value = "Arrow Down";

      break;
   }

}
</script>
put this code on form
Code:
<form name="Form1">

<input type="text" name="KeyName" value="" />

</form>
I hope this helps.
Reply With Quote
  #3  
Old 07-08-2009
Member
 
Join Date: Apr 2008
Posts: 21
Re: How to use get key codes for javascript?

Try this:

Code:
function KeyCode(e){

     if(e){
          e = e
     } else {
          e = window.event
     }

     if(e.which){
          var keycode = e.which
     } else {
          var keycode = e.keyCode
     }

     document.all.t2.value = keycode
}
Reply With Quote
  #4  
Old 07-08-2009
Member
 
Join Date: May 2008
Posts: 24
Re: How to use get key codes for javascript?

Get key char

Code:
<html>
<head>
<title>Key Events Example</title>
<script type="text/javascript">
function handleEvent(oEvent) {
    var oTextbox = document.getElementById("txt1");
    oTextbox.value += "\n>" + oEvent.type;
    oTextbox.value += "\n    target is " + (oEvent.target || oEvent.srcElement).id;
    oTextbox.value += "\n    charCode is " + oEvent.charCode;
}
</script>
</head>
<body>
    <P>Type some characters into the first textbox.</p>
    <P><textarea id="txtInput" rows="15" cols="50"
        onkeypress="handleEvent(event)"></textarea></p>
    <P><textarea id="txt1" rows="15" cols="50"></textarea></p>
</body>
</html>
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use get key codes for javascript?"
Thread Thread Starter Forum Replies Last Post
BlackBerry Storm 9530: Not able to dial neither USSD codes / nor GSM codes Satruijt Portable Devices 7 17-09-2011 11:35 AM
JavaScript codes for making Web Page Attractive Solitario Software Development 4 09-02-2010 03:06 AM
Wii Console Codes Reece Technology & Internet 2 10-03-2009 11:49 PM
Nokia Codes tifosi Portable Devices 6 11-08-2008 04:50 PM
PUK codes bluediamond Portable Devices 5 03-06-2004 04:37 PM


All times are GMT +5.5. The time now is 01:25 AM.