The DOM Event API already provides all you need, assuming you need only to remember the modifier keys from a legitimate user action.
When using addEventListener
to catch an event, simply take the parameter, which is a DOMEvent
instance, that is passed to your handler function, and resend it to your target element with dispatchEvent
:)
Indeed, a DOMEvent
instance encapsulates its source environment. More specifically here, a MouseEvent
knows which keys were pressed when it was fired.
Demo: try clicking the link in this JSfiddle while holding down ⌘ (or ctrl if not on a Mac), for example.
For a complete reference, here is the used code:
var button = document.getElementById('source'),
target = document.getElementById('target');
function handler(evt) {
target.dispatchEvent(evt); // that's all the magic it takes
}
button.addEventListener(
'click', // listen to a click event
handler,
false // no capture, i.e. do not catch events before children
);
You may also find this complete reference on DOM events useful :)