Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Capture Keys and replace key in javascript

Status
Not open for further replies.

rammi07

Programmer
Aug 1, 2006
20
0
0
NL
hi all. could any one show me how i can capture all the keys and do some replace for keys. For example if key K is pressed i want in textbox it shows arabic unicode equivelent for letter k which is ?. Similerly for remaining keys.Thanks
 
How's this?
Code:
<script>
var keyReplacements = [
  ['A','X'],
  ['B','Y'],
  ['C','Z']
];

function switchKey(txt){
  for(var i=0;i<keyReplacements.length;i++){
    if(keyReplacements[i][0].charCodeAt(0) == event.keyCode){
      insertAtCursor(txt,keyReplacements[i][1]);
      return false;
    }
  }
  return true;
}

function insertAtCursor(myField, myValue) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }

  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
    + myValue
    + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
}
</script>

<input type="text" onkeydown="return switchKey(this)">

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top