avatarSasha

Summary

Frontend developers may encounter issues with keyboard event handling on Mac OS due to the "command" key suppressing the "keyup" event, necessitating workarounds or library usage for web applications.

Abstract

The article addresses a specific problem faced by frontend developers on Mac OS, where the "keyup" event is not triggered when the "command" key is held down, leading to unexpected behavior in web browsers. This issue complicates the handling of keyboard events, which are typically straightforward with "keydown" and "keyup" events. The author suggests using the "keydown" event as a solution in most cases, but also provides a detailed example of how to manually trigger "keyup" events when the "command" key is released, using JavaScript to record pressed keys. The article also recommends keyboard event libraries like KeyboardJS, Mousetrap, or Keypress for more complex scenarios. The problem is particularly relevant for developers targeting Mac OS users, and the article encourages sharing of similar unusual problems encountered in web development.

Opinions

  • The author believes that the "command" key issue can significantly impact web application development, especially for those targeting Mac OS users.
  • The preferred method for handling the issue is to subscribe to the "keydown" event, which may resolve the problem without further complexity.
  • For more intricate situations, the author suggests that using existing keyboard event libraries can be beneficial, indicating a preference for reusable solutions over reinventing the wheel.
  • The author values the sharing of knowledge within the developer community, as evidenced by the invitation to discuss other unusual problems in the comments.
  • There is an underlying assumption that developers appreciate modular and reusable components, as indicated by the promotion of Bit's tools for building apps with reusable components.

KeyUp Event and ⌘(cmd) Problem

Introduction

Frontend developers often face the task of handling keyboard events. Recently, I encountered an interesting problem that prompted me to explore the issue more deeply. This article will help you avoid unwanted behaviour in your application or correct it.

The problem arises on Mac OS when the “command” key is held down, which can cause some web browsers to interpret keyboard events unexpectedly. Usually, pressing a key triggers the “keydown” event, and releasing a key triggers the “keyup” event. But, when the “command” key is pressed, the browser ignores the “keyup” event.

How to check

We can verify this behaviour by using the key event viewer tool available at https://w3c.github.io/uievents/tools/key-event-viewer.html.

Checking usual events behaviour

First, let’s test the normal behaviour of the events. Let’s press and release the “a” key, as shown in the first screenshot. We can see that the event handling is working as expected.

Checking ignoring of “keyup” event

Now let’s try pressing and releasing the “a” key with the “command” key held down, as shown in the second screenshot. The “keyup” event for the “a” key does not work.

How to deal with it

The difficulty of working around this limitation depends on the situation. In most cases, subscribing to the “keydown” event instead of the “keyup” event will be enough, and the problem will not be noticed again. But, in some situations, it may be necessary to react only to the “keyup” event. In such cases, you can try to use popular keyboard event libraries like KeyboardJS, Mousetrap, or Keypress, or write your solution. The basic idea is to listen for the “keydown” event and if the “command” key is being held down, record the pressed keys and manually trigger the “keyup” event for each of them when the “command” key is released:

const typedText = document.getElementById('typed-text');
const pressedKeys = new Set();

document.addEventListener('keydown', event => {
 if (event.metaKey) {
  pressedKeys.add(event.key);
 }
});

document.addEventListener('keyup', event => {
 if (event.metaKey) {
  pressedKeys.delete(event.key);

  // Manually fire "keyup" event for all pressed keys
  pressedKeys.forEach(key => {
   const keyupEvent = new KeyboardEvent('keyup', {key});
   typedText.dispatchEvent(keyupEvent);
  });
 }
});

typedText.addEventListener('keyup', event => {
 console.log(`You typed: ${event.key}`);
});

Conclusion

The “Command” + “keyup” event problem can be a significant issue for developers working on web applications, particularly those that are targeted at Mac OS users. You can avoid it subscribing on “keydown” event, using specific libraries our writing your own solution.

What unusual problems have you encountered in your work? Write in the comments, I’d love to hear more similar cases!

Build apps with reusable components like Lego

Bit’s open-source tool help 250,000+ devs to build apps with components.

Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.

Learn more

Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:

→ Micro-Frontends

→ Design System

→ Code-Sharing and reuse

→ Monorepo

Learn more

JavaScript
Front End Development
Frontend
Programming
Software Development
Recommended from ReadMedium