如何在Safari中发送键盘事件(例如Backspace,Delete)
我正在实现一个JavaScript应用程序的屏幕键盘。 文本应该出现在textarea-Element中。 创建文本事件我没有问题,但感觉无法创建非文本事件,如Backspace。 以下是一些示例代码:
<html> <head> <title>Test Textarea Events</title> <script type="text/javascript"> function log(text) { var elem = document.getElementById("log"); if (elem) { elem.innerHTML += "<br/>" + text; } } function logEvent(e) { log("Type: " + e.type + ", which: " + e.which + ", keyCode: " + e.keyCode + ", charCode: " + e.charCode + ", keyIdentifier: " + e.keyIdentifier + ", data: " + e.data); } function logClear() { var elem = document.getElementById("log"); if (elem) { elem.innerHTML = ""; } } function sendBackEvent() { var textarea = document.getElementById("textarea"); if(!textarea) { return; } var ke1 = document.createEvent("KeyboardEvent"); ke1.initKeyboardEvent("keydown", true, true, window, "U+0008", 0, ""); // U+0008 --> Backspace // how to set "keyCode" and "which"? // 1. ke1.keyCode = 8; will be ignored due to "writable: false" // 2. delete ke1.keyCode will be ignored too // and Object.defineProperty(KeyboardEvent.prototype, "keyCode", ... whatever ...); // will result in Exception due to "configurable: false" // 3. generating a more general Event (e.g. "var e = document.createEvent("Events");") // and setting all necessary props (e.g. "e.initEvent("keydown", true, true);e.keyCode=8;e.which=8;") // will work, but the textarea will ignore the Event textarea.dispatchEvent(ke1); var ke2 = document.createEvent("KeyboardEvent"); ke2.initKeyboardEvent("keyup", true, true, window, "U+0008", 0, ""); // U+0008 --> Backspace // same question as above textarea.dispatchEvent(ke2); } function init() { var textarea = document.getElementById("textarea"); if(!textarea) { return; } textarea.addEventListener("keydown", logEvent, false); textarea.addEventListener("keypress", logEvent, false); textarea.addEventListener("keyup", logEvent, false); textarea.addEventListener ("textInput", logEvent, false); } </script> </head> <body onload="init()"> <textarea id="textarea" name="text" cols="100" rows="20"></textarea><br/><br/> <button onclick="sendBackEvent()">send BackEvent</button> <button onclick="logClear()">clear log</button><br/> <div id="log"></div> </body> </html>
这里有趣的功能是“sendBackEvent()”。 我努力得到完全相同的事件,例如按物理键盘上的Backspace-Button,但没有成功。 我知道有一个Webkit-Bug,但希望可以有其他方法来实现这个工作。 有没有人有同样的问题? 它能解决吗? 怎么样? 这里提出的解决方案(调用新的KeyboardEvent(...))无法正常工作,因为直接调用KeyboardEvent构造函数会导致以下异常:
Exception: TypeError: '[object KeyboardEventConstructor]' is not a constructor (evaluating 'new KeyboardEvent(...)')
我从2天开始就在这里挣扎,并没有更多的想法。
由于看起来没有出路,我发现自己不得不安装我自己的退格事件处理程序。 工作代码(现在不记录)在这里:
<html> <head> <title>Test Textarea Events</title> <script type="text/javascript"> function sendBackEvent() { var textarea = document.getElementById("textarea"); if(!textarea) { return; } var ke1 = document.createEvent("Events"); ke1.initEvent("keydown", true, true); ke1.keyCode = ke1.which = 8; // Backspace textarea.dispatchEvent(ke1); var ke2 = document.createEvent("Events"); ke2.initEvent("keyup", true, true); ke2.keyCode = ke2.which = 8; // Backspace textarea.dispatchEvent(ke2); } function handleBackEvent(e) { if(e.keyCode != 8) { return; } if (textarea.value.length == 0) { return; } var text = textarea.value; var selectionStart = textarea.selectionStart; var selectionEnd = textarea.selectionEnd; if (selectionStart < selectionEnd) { var text1 = (selectionStart > 0 ? text.slice(0, selectionStart) : ""); var text2 = (selectionEnd < text.length ? text.slice(selectionEnd) : ""); textarea.value = text1 + text2; } else if (selectionStart > 0) { var text1 = (selectionStart - 1 > 0 ? text.slice(0, selectionStart - 1) : ""); var text2 = (selectionStart < text.length ? text.slice(selectionStart) : ""); textarea.value = text1 + text2; selectionStart--; } textarea.selectionStart = textarea.selectionEnd = selectionStart; e.preventDefault(); e.stopPropagation(); return false; }; var textarea; function init() { textarea = document.getElementById("textarea"); if(!textarea) { return; } textarea.addEventListener("keydown", handleBackEvent, false); } </script> </head> <body onload="init()"> <textarea id="textarea" name="text" cols="100" rows="20"></textarea><br/><br/> <button onclick="sendBackEvent()">send BackEvent</button> </body> </html>
以防万一别人遇到同样的麻烦。
链接地址: http://www.djcxy.com/p/49177.html上一篇: How to send Keyboard events (e.g. Backspace, Delete) in Safari from javascript