I've a service that detects if a CTRL/CMD key is pressed. It is used in various components to check (among other things) if links should open in a new tab or not.
Service:
keyDownListenerCTRL = fromEvent<KeyboardEvent>(window, 'keydown').pipe(
tap((event: KeyboardEvent) => {
if (
event.metaKey ||
event.ctrlKey ||
event.key === 'Meta' ||
event.key === 'Ctrl'
) {
this.isControlButtonHeld = true;
}
})
);
constructor() {
this.keyDownListenerCTRL.subscribe();
}
I don't know how to test this. There is no HTML element that I could simulate a click on, so using a dispatchEvent (like suggested here) does not work.