+function init_keyboard(target)
+{
+ if (target.addEventListener)
+ {
+// target.addEventListener("keydown",keydown,false);
+ target.addEventListener("keypress",keypress,false);
+// target.addEventListener("keyup",keyup,false);
+// target.addEventListener("textInput",textinput,false);
+ }
+ else if (target.attachEvent)
+ {
+// target.attachEvent("onkeydown", keydown);
+ target.attachEvent("onkeypress", keypress);
+// target.attachEvent("onkeyup", keyup);
+// target.attachEvent("ontextInput", textinput);
+ }
+ else
+ {
+// target.onkeydown= keydown;
+ target.onkeypress= keypress;
+// target.onkeyup= keyup;
+// target.ontextinput= textinput; // probably doesn't work
+ }
+
+}
+
+function keyval(n)
+{
+ if (n == null) return 'undefined';
+ var s= '' + n;
+ if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
+ while (s.length < 9) s+= ' ';
+ return s;
+}
+
+function string_of_key(n)
+{
+ if (n == null) return 'undefined';
+ return String.fromCharCode(n);
+}
+
+function pressmesg(w,e)
+{
+ debug(w + ' keyCode=' + keyval(e.keyCode) +
+ ' which=' + keyval(e.which) +
+ ' charCode=' + keyval(e.charCode) +
+ '\n shiftKey='+e.shiftKey
+ + ' ctrlKey='+e.ctrlKey
+ + ' altKey='+e.altKey
+ + ' metaKey='+e.metaKey);
+}
+
+function suppressdefault(e,flag)
+{
+ if (flag)
+ {
+ if (e.preventDefault) e.preventDefault();
+ if (e.stopPropagation) e.stopPropagation();
+ }
+ return !flag;
+}
+
+function keypress(e)
+{
+ if (!e) e= event;
+ pressmesg('keypress',e);
+ var s = string_of_key(e.charCode);
+ if (s == " ") {
+ j = getCursorPos();
+ i = unlocked.innerHTML.lastIndexOf('\\',j);
+ if (i >= 0) {
+ match = unlocked.innerHTML.substring(i,j);
+ pre = unlocked.innerHTML.substring(0,i);
+ post = unlocked.innerHTML.substring(j);
+ if (match == '\\to') {
+ unlocked.innerHTML = pre + "-> " + post;
+ unlocked.focus();
+ if (savedRange != null) {
+ if (window.getSelection)//non IE and there is already a selection
+ {
+ var s = window.getSelection();
+ if (s.rangeCount > 0)
+ s.removeAllRanges();
+ s.addRange(savedRange);
+ }
+ else
+ if (document.createRange)//non IE and no selection
+ {
+ window.getSelection().addRange(savedRange);
+ }
+ else
+ if (document.selection)//IE
+ {
+ savedRange.select();
+ }
+ }
+
+ return suppressdefault(e,true);
+ }
+ else return suppressdefault(e,false);
+ }
+ else return suppressdefault(e,false);
+ } else {
+ return suppressdefault(e,false);
+ }
+}
+