My Custom Rack I Built
Reply to topic
 
to get the code of the key pressed



Joined:31 Mar 2007
Posts:4
Reply with quote
helo

lets say i have a text box. I want to call a function on keydown event to get the code of the key pressed. How can i achieve that

<input type="text" .......... onkeypress="display()">

that display() function should take the event; if the character pressed is in the printable ascii range then it should print the ascii code of it. All other keys (backspace, space, alt ...) should work normally.

Please help me out... i'm in greate need of it...

thanks a lot
View user's profileFind all posts by deepusrpSend private message
to get the code of the key pressed

Moderator

Moderator

Joined:25 Jan 2003
Posts:1312
Location:England
Reply with quote
See if this code is what you are after. I would guess you just need to add a if statement to check the chars are between ascii 32 and 127 if that's what you want. i.e NOT the extended ascii codes.
Code:
<html>
<head>
<script language="javascript">
function ck(e)
{
  var _ret=false;
  var key;
  /* IE */
  if(window.event)
  {
    key = e.keyCode;
  }
  /* NS/FF/OPERA */
  else if(e.which)
  {
    key = e.which;
  }
  keychar = String.fromCharCode(key);
  /* use this next line to see which key was hit */
  document.getElementById("showcode").innerHTML=key;
  return _ret;
}

</script>
</head>
<body>
<form>
<input id="t1" onkeypress="(ck(event))"><BR>
<div id="showcode"></div>

</form>
</body>
</html>
View user's profileFind all posts by batfinkSend private message
to get the code of the key pressed



Joined:31 Mar 2007
Posts:4
Reply with quote
Helo sir,

Thank you very much for replying my question.. That is what exactly i wanted.

There is some devition from what u have given... You are printing the output in different location using <DIV> tag. But my requirement is i need the out put on the same text box...

So I tried that, Its coming but even the character pressed is also displaying which i dont want. How can i solve that.

Now i'll tell u why i need this. I am working on some web based project there they will have many text lines and text boxes. I want to put that function to all those. In that function instead of printing the ascii code i'll print the unicode of my language as and when the key is pressed.

So that the user can type the text directly in their regional language with out the help of soft keyboard.

Hope u'll guide me in the entire project

Thanks a lot

Deepu Very Happy
View user's profileFind all posts by deepusrpSend private message
to get the code of the key pressed

Moderator

Moderator

Joined:25 Jan 2003
Posts:1312
Location:England
Reply with quote
This displays one keypress only:
Code:
<html>
<head>
<script language="javascript">
function ck(e)
{
  var _ret=false;
  var key;
  /* IE */
  if(window.event)
  {
    key = e.keyCode;
  }
  /* NS/FF/OPERA */
  else if(e.which)
  {
    key = e.which;
  }
  /* use this next line to see which key was hit */
  word = document.getElementById("t1").value;
  wl = word.length;
  document.getElementById("t1").value=document.getElementById("t1").value.substr(wl)+key;
  return _ret;
}

</script>
</head>
<body>
<form>
<input id="t1" onkeyUp="(ck(event))"><BR>
</form>
</body>
</html>
View user's profileFind all posts by batfinkSend private message
to get the code of the key pressed

Moderator

Moderator

Joined:25 Jan 2003
Posts:1312
Location:England
Reply with quote
This displays one keypress after another:
Code:
<html>
<head>
<script language="javascript">
function ck(e)
{
  var key;
  /* IE */
  if(window.event)
  {
    key = e.keyCode;
  }
  /* NS/FF/OPERA */
  else if(e.which)
  {
    key = e.which;
  }
  var word = document.getElementById("t1").value;
  var result=word.replace(new RegExp(/\D/),"");
  document.getElementById("t1").value = result + key;
}

</script>
</head>
<body>
<form>
<input id="t1" onkeyUp="(ck(event))">
</form>
</body>
</html>
View user's profileFind all posts by batfinkSend private message
to get the code of the key pressed



Joined:31 Mar 2007
Posts:4
Reply with quote
Helo,

Thanks a lot.... Very Happy

Can u please tell me, how to display the unicode instead of ascii.

I'll tell u what i have done.

I have kept an array, in which i have stored the unicode values as strings. When ever a key is pressed, it should refer that array and print the corresponding values.

Code:

 var data = new array();

 data[65] = "ಕ";   // a  -- You might be displayed with the actual font... but i am storing it as  (&# 3221) space is to avoid the processing.  i have not given any space;
 data[66] = "ಖ";   // b

 .
 .
 .

// in function ck

 document.getElementById("t1").value = data[key];
 
 


My problem is, instead of our language character it is printing the value as it is.(&# 3221;) I have set the correct font to the textarea object.

please help me Confused

thanks a lot once again
View user's profileFind all posts by deepusrpSend private message
to get the code of the key pressed
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum
All times are GMT - 6 Hours  
Page 1 of 1  


  
  
 Reply to topic