DEV Community

Omri
Omri

Posted on

4 2

How to manage keyboard shortcuts in Angular application (The easy way)

I have always liked the way Visual Studio Code enabled you to modify/add key binding in a JSON object, which is a pretty nice interface in my opinion.
Taken as an inspiration, I wanted to create a similar and scalable solution for Angular application.
To achieve a pretty scale-able solution keeping mind memory leakage and garbage collection of registered shortcut in a large app. Thus a component based architecture was chosen.

The library supports both Windows and Mac, has a nice built in help screen to show all registered shortcuts and service to create a customize help screen in need.

You can find the library here:
And play with a working example here

How it works

  1. To optimize for performance, only one event listener is registered to the document body.
  2. Each shortcut is then checked when keydown is pressed (this part can still be optimized and will be in the future, but the filtering is pretty quick at the moment so even with 100 registered shortcuts no slowdown are noticeable)
  3. You can use a directive to target a shortcut to work only when a specific element is in focus
  4. Or use a component that will add a global shortcut to your application, and will be cleaned out automatically when the component is destroyed.

Feedback

This is the first library I publish to NPM, any feedback, code related, documentation or anything at all will be greatly appreciated.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (1)

Collapse
 
omri_katz_83a2260e81038ff profile image
Omri Katz

Great one.

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay