Angular’s focus events (like focusin) don’t indicate whether the focus came from a keyboard (Tab) or a mouse (click). To distinguish between the two, you have to track the user’s actions yourself. One common solution is to listen for both the mousedown event (to catch a click) and the keydown event (to catch when the user presses Tab) and then use a flag that you check when the input receives focus.
Below is an example of how you might implement this in an Angular component.
Example Implementation
import { Component, HostListener } from '@angular/core';
@Component({
selector: 'app-my-input',
template: `
<input
type="text"
(focusin)="onFocusIn()"
placeholder="Click or Tab into me" />
`,
})
export class MyInputComponent {
// This flag will store the source of the last user interaction.
private lastInteraction: 'mouse' | 'keyboard' | null = null;
// Listen for mousedown events on the component.
// These indicate that an element was clicked.
@HostListener('mousedown', ['$event'])
onMouseDown(event: MouseEvent) {
this.lastInteraction = 'mouse';
}
// Listen for keydown events.
// We check if the Tab key was pressed.
@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent) {
if (event.key === 'Tab') {
this.lastInteraction = 'keyboard';
}
}
// Handle the focusin event on the input.
onFocusIn() {
if (this.lastInteraction === 'keyboard') {
console.log('Focused via Tab (keyboard)');
} else if (this.lastInteraction === 'mouse') {
console.log('Focused via mouse click');
} else {
// Fallback case: if no interaction was recorded
console.log('Focused but no previous interaction recorded.');
}
// Optionally, reset the flag if you want to avoid reusing the same flag
// for future focus events that might otherwise carry stale data.
this.lastInteraction = null;
}
}
Explanation
-
Tracking Mouse Events:
We register a mousedown event handler via the @HostListener decorator. When the input (or any child element of the component) is clicked, this handler sets lastInteraction to 'mouse'.
-
Tracking Keyboard Events:
Similarly, we register a keydown event handler that checks if the key pressed is Tab (event.key === 'Tab'). If it is, we set lastInteraction to 'keyboard'.
-
Determining the Focus Source:
When the input receives focus (using the focusin event), the onFocusIn method is called. This method checks the flag and logs whether the focus came from a mouse click or the Tab key.
Things to Consider
-
Edge Cases & Timing:
Depending on your use case, you may need to refine this logic—for example, if there’s another way of focusing an element or if there’s a delay between the triggering event and the focus event. In some cases, using a timeout to reset the flag may be necessary.
-
Global Event Handling:
If you have multiple inputs or more complex requirements, you might consider using a service to manage the last interaction state globally.
Using this approach, you can effectively differentiate between a focus triggered by a Tab key press versus a click on an Angular input element.