Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function to set key events #102

Open
YoavHortman opened this issue Oct 26, 2023 · 2 comments
Open

Add a function to set key events #102

YoavHortman opened this issue Oct 26, 2023 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@YoavHortman
Copy link

Is your feature request related to a problem? Please describe.
The application I'm building can be controlled both from the keyboard (or tv remote) and from a phone (connected via socket)

Describe the solution you'd like
I would like to be able to extend the events that are being listened to, not only the key codes.

Describe alternatives you've considered
For now I'm dispatching fake keyboard events when I receive a navigation command from the phone.

Additional context
I think it would make sense to do something similar to setKeyMap, but for events.

@predikament
Copy link
Collaborator

predikament commented Nov 16, 2023

Hello @YoavHortman!

Are you meaning something like this? https://github.com/NoriginMedia/Norigin-Spatial-Navigation#setkeymap

If not then perhaps you could provide some examples or snippets of what and how you are intending to achieve it, so we can better help you out?

It kind of sounds to me like you are wanting to simulate key events based on what is inherently virtual key events stemming from a tertiary device; Not entirely sure if that is within the scope of this library to handle, but let's see what you have.

@predikament predikament added the question Further information is requested label Nov 16, 2023
@YoavHortman
Copy link
Author

YoavHortman commented Nov 17, 2023

I think this code example would answer your question:

 const commandToEvent: Record<NavigationCommand['command'], KeyboardEvent> = {
      up: new KeyboardEvent('keydown', { bubbles: true, cancelable: true, key: 'ArrowUp', keyCode: 38 }),
      down: new KeyboardEvent('keydown', { bubbles: true, cancelable: true, key: 'ArrowDown', keyCode: 40 }),
      left: new KeyboardEvent('keydown', { bubbles: true, cancelable: true, key: 'ArrowLeft', keyCode: 37 }),
      right: new KeyboardEvent('keydown', {
        bubbles: true,
        cancelable: true,
        key: 'ArrowRight',
        keyCode: 39,
      }),
      enter: new KeyboardEvent('keydown', { bubbles: true, cancelable: true, key: 'Enter', keyCode: 13 }),
      back: new KeyboardEvent('keydown', { bubbles: true, cancelable: true, key: 'Backspace', keyCode: 8 }),
    };
    communication.receiveNavigationCommand((command) => {
      window.dispatchEvent(commandToEvent[command.command]);
    });

Essentially I'm being forced to simulate keyboard events when navigation comes from a different device,

I don't want to change the keymap, I want to be able trigger a navigation regardless of a key.

How that might look:

const myOwnEventFunction(e) => {
 if (e.data === "mySpecialReasonToNavigateUp") {
      norigin.triggerNavigation('up')
 }
}

A second option:

norigin.overrideEventListener = (triggableActions) => {
   recieveSpecialEvent((e) => {
      if (e.command === 'up') {
          triggableActions.up()
      }
   })
}

I think this would remove the need to have the keymap override and would add the flexibility to navigate for any reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants