Enhancing Angular Navigation with GlobalKeydown: A Guide to Keyboard-Driven Routes

Davide Di Francesco
3 min readJan 18, 2024
Keyboard-Driven Routes

If you’re unable to view this article, no worries! You can read it for free on my personal blog at https://davdifr.com ❤️

In my previous article, “Dynamic Navigation in Angular: Building a Navbar Using Route Configurations”, we explored how to create a dynamic navigation bar in Angular. Building upon that foundation, this article introduces an innovative approach to navigation — using keyboard inputs to trigger route changes. This method not only enhances accessibility but also provides a more efficient way for users to navigate through the application.

Introduction to GlobalKeydownService

Angular’s flexibility allows us to extend our application’s functionality with custom services. In this case, we’ll create GlobalKeydownService. This service listens to global keydown events and filters out those triggered within input elements - ensuring that typing in a text field does not inadvertently trigger navigation.

The Service Code

import { Injectable } from '@angular/core';
import { fromEvent, Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';

@Injectable({
providedIn: 'root',
})
export class GlobalKeydownService {
keydownEvents$…

--

--