0

in my html, I've got an event binding as below:

document.addEventListener('keydown', onKeyDown);

Now that I'd like to hook on the keydown event, i.e., when keydown happens, call another function and trigger the onKeyDown manually.

the document.addEventListener('keydown', onKeyDown); is already there(upstream) and I don't want to change it. so to sum up:

`keydown` -> `do something` -> `onKeyDown`

Is there a way to achieve this?

4
  • 1
    You want to execute the onKeyDown() on a specific key press ? Commented Feb 4, 2013 at 3:02
  • 2
    No parenthesis! Commented Feb 4, 2013 at 3:04
  • @Bergi thanks I've edited the question to fix that. @ubercooluk, yep, the document.addEventListener is already there I don't want to change it(upstream), I want to add hook during keydown: keydown-> do something -> onKeydown. Commented Feb 4, 2013 at 3:12
  • Do you mean dispatch an event manually? Commented Feb 4, 2013 at 3:14

1 Answer 1

1

If your onKeyDown method could be accessed, you can remove it from event listeners and reattach a modified version:

function myKeyDown() {
    // your logic
    onKeyDone.apply(this, arguments);
}
document.addEventListener('keydown', myKeyDown);
document.removeEventListener('keydown', onKeyDown);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.