1. Technology

JavaScript By Example

Events: 8. Character Entered


With keyboard events there is a character on the keyboard that is associated with the event. Of course some of the events are triggered before the key that triggered it has been sent by the keyboard and so you cannot tell what key it is that triggered for example a keydown event.

As with most event related processing there are different ways that the event is made available which we can resolve through the way we name the argument in our function. The other concern here is that rather than the character itself being available we only have the code corresponding to the key that was pressed available to us. We can however use a static method belonging to the String object to convert the code back into a character for us as this example demonstrates.

whichChar = function(event) {
return String.fromCharCode( (event.charCode) ? event.charCode : event.keyCode);

Reader Submissions: Help Others By Providing Your Own JavaScript Examples

JavaScript By Example
  1. About.com
  2. Technology
  3. JavaScript
  4. Javascript Tutorials
  5. JavaScript By Example
  6. Events
  7. JavaScript By Example - Events: Character Entered

©2014 About.com. All rights reserved.