0

Is there a way to schedule an event when a key is pressed on the keyboard. I can't seem to find that in the event triggers.

fmsf
  • 36,317
  • 49
  • 147
  • 195
rsajdak
  • 93
  • 12

1 Answers1

2

There are two possible answers to this.

First one is that slate is just javascript, with sandboxing layers (for security and functionality) so to an extent if you know the dom element ID could just try to reach it with something similar to this: https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event#examples

const log = document.getElementById('log');

document.addEventListener('keypress', logKey);

function logKey(e) {
  log.textContent += ` ${e.code}`;
}

Due to the sandboxing layers it may not be possible depending on what you are trying to achieve. If you want to do this natively, then the trick to effectively designing apps with slate is to think that the whole world gets recomputed whenever a variable changes. Similar to what happens with the lifecycle refreshes on Angular.js

There isn't a native global event for a key press in slate, so if you want to listen for any keypress that bubbles to the document, that won't work. But for most use cases you can work around it. For example by setting a variable from an input and then just read it from somewhere else.

1 - Go to the variables tab and create a new variable.

2 - Create an input box.

3 - Set the variable to update every time the variable changes.

4 - Read the variable from some widget or function.

enter image description here

fmsf
  • 36,317
  • 49
  • 147
  • 195