faqts : Computers : Programming : Languages : JavaScript : DHTML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

23 of 24 people (96%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How can I write from keyboard to the page ?

May 27th, 2000 08:36
Martin Honnen, Rogerio Vianna,


Capture one of the key events and write the captured key to another 
page element (which needs to be positioned in NN4 to do so):
<HTML>
<HEAD>
<STYLE>
#output { 
  position: absolute;
  color: white;
  background-color: orange;
  layer-background-color: orange;
}
</STYLE>
<SCRIPT>
var text = '';
function keyHandling (evt) {
  var key = document.layers ? evt.which :
            document.all ? event.keyCode :
            evt.keyCode ? evt.keyCode :
            evt.which ? evt.which :
            evt.charCode;
  var ch = String.fromCharCode(key);
  text += ch;
  if (document.layers) {
    var d = document.output.document;
    d.open();
    d.write(text);
    d.close();
  }
  else if (document.all)
    document.all.output.innerText = text;
  else if (document.getElementById) {
    var l = document.getElementById('output');
    if (l.firstChild)
      l.firstChild.nodeValue = text;
    else 
      l.appendChild(document.createTextNode(text));
  }
}
document.onkeypress = keyHandling;
</SCRIPT>
</HEAD>
<BODY>
Please type in text:
<BR>
<DIV ID="output"></DIV>
</BODY>
</HTML>