Skip to content

Give opportunity to hook on keyup event #26

@tpoisseau

Description

@tpoisseau

With only react-kbs it is not possible to setup an alternative mode as long as a key is pressed.

We have to hook directly on addEventListener, ex:

  useKbsGlobal([
    {
      shortcut: 'Shift',
      handler: () => {
        actions.setMode('select');

        function resetMode(event: KeyboardEvent) {
          if (event.key !== 'Shift') return;

          actions.setMode('rotate_selected');
          document.body.removeEventListener('keyup', resetMode);
        }

        document.body.addEventListener('keyup', resetMode);
      },
    },
  ]);

Inspired from useEffect with cleanup function returned by the handler, we could support a cleanup method returned by the shortcut handler:

  useKbsGlobal([
    {
      shortcut: 'Shift',
      handler: () => {
        actions.setMode('select');

        return () => actions.setMode('rotate_selected');
      },
    },
  ]);

Or we can add onKeyUp option:

  useKbsGlobal([
    {
      shortcut: 'Shift',
      handler: () => {
        actions.setMode('select');
      },
      onKeyUp: () => {
        actions.setMode('rotate_selected');
      },
    },
  ]);

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions