I have a keylistener that receives a function and calls it when a key is pressed:
function addKeyListener(functionToCall) {
addKeyEvent("keypress", function() { functionToCall(arguments[0]) }, false);
}
Wha开发者_StackOverflowt can I pass to that function to let my window be a keylistener?
I'm not quite sure what your code snippet is trying to achieve, but if you have a reference to another window, and you want to listen to key events on that window, simply use otherWindow.addEventListener("keypress", eventHandler, false);
.
OK, so given your clarification, it looks as if you need to do the following:
- Locate the focused document in the other window:
otherWindow.document.commandDispatcher.focusedWindow.document
- Create a key event:
otherDocument.createEvent('KeyEvents')
- Initialise the event (type, code, modifiers etc.) from your key event
- Locate the focused element, if any, in the other window:
otherWindow.document.commandDispatcher.focusedElement
- Dispatch the event to the focused element, or document if no element has focus.
精彩评论