TypeError: Attempting to change value of a readonly property. defineProperty
TypeError: Attempting to change value of a readonly property. defineProperty
我正在尝试 window.parent.document.dispatchEvent(keyboardEvent)。
我这样做是为了捕获 keyboardevent 的 键码值,并将根据按下的键执行一些操作。如果没有这个键码,即使我在浏览器中 运行ning 也会显示为 0。
我的代码类似于
window.setTimeout(function () {
var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
keyboardEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
666, // keyCodeArg : unsigned long the virtual key code, else 0
null // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
delete keyboardEvent.keyCode;
Object.defineProperty(keyboardEvent, "keyCode", {"value" : 666});
window.parent.document.dispatchEvent(keyboardEvent);
}, 10000);
我给值 666 只是为了测试。
当我 运行 它在 chrome 中时,这工作正常。
但是这行代码
Object.defineProperty(keyboardEvent, "keyCode", {"value" : 666});
thorws "TypeError: Attempting to change value of a readonly property. defineProperty." when i 运行 code in set top box.
这是 plnkr
link 以上代码 http://plnkr.co/edit/my8HkFpRqZRDrFYgIxXL?p=preview
如何在机顶盒中捕获正确的键码以便自动执行按键事件?
上面的代码可以添加任何额外的行 added/modified 以在机顶盒上提供正确的密钥代码吗?
我正在尝试 window.parent.document.dispatchEvent(keyboardEvent)。
我这样做是为了捕获 keyboardevent 的 键码值,并将根据按下的键执行一些操作。如果没有这个键码,即使我在浏览器中 运行ning 也会显示为 0。
我的代码类似于
window.setTimeout(function () { var keyboardEvent = document.createEvent("KeyboardEvent"); var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent"; keyboardEvent[initMethod]( "keydown", // event type : keydown, keyup, keypress true, // bubbles true, // cancelable window, // viewArg: should be window false, // ctrlKeyArg false, // altKeyArg false, // shiftKeyArg false, // metaKeyArg 666, // keyCodeArg : unsigned long the virtual key code, else 0 null // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0 ); delete keyboardEvent.keyCode; Object.defineProperty(keyboardEvent, "keyCode", {"value" : 666}); window.parent.document.dispatchEvent(keyboardEvent); }, 10000);
我给值 666 只是为了测试。
当我 运行 它在 chrome 中时,这工作正常。
但是这行代码
Object.defineProperty(keyboardEvent, "keyCode", {"value" : 666});thorws "TypeError: Attempting to change value of a readonly property. defineProperty." when i 运行 code in set top box.
这是 plnkr
link 以上代码 http://plnkr.co/edit/my8HkFpRqZRDrFYgIxXL?p=preview
如何在机顶盒中捕获正确的键码以便自动执行按键事件? 上面的代码可以添加任何额外的行 added/modified 以在机顶盒上提供正确的密钥代码吗?