Hi,
I will get straight to the point. I am new to javascript and trying to create a custom element so I can create hotkeys. I got it to work if I use alert or console.log() in the element code. Now I just want it to send a trigger to the Velo/page code.
I was using the dispatch event like this but it does not seem to work.
The this here looks wrong.
The .dispatchEvent() is a method of custom elements, but you put it inside another function so the this refers to the function(e).
You should use a web component and dispatch the custom event from the web component only. And don’t forget to register your web component with customElements . define
Hi, thank you so much for your response. Since I have seen some other people that are interested in this as well I will continue to ask one more question.
I have copied your code exactly but it does no longer seem to get triggered on keyup. Even if I place an alert or console log in the element to this it does not seem to work.
My code:
// in the element (also tried with .key but did not fix the isseu)
class MyWebComponent extends HTMLElement {
connectedCallback() {
document.addEventListener('keyup', (event) => {
if (event.wich == 77) {
alert("INTERN")
this.dispatchEvent(new CustomEvent('my-event'));
}
});
}
}
customElements.define('my-webcomponent', MyWebComponent);
//page code
$w('#MyWebComponent').on('my-event', (event) => {
console.log('The event triggered successfully.')
});
I hope I am not being too rude asking all these questions
@joriswillemborgers2 The little details. You should register the name of web component with UI panel Tag Name === customElements . define(‘YOUR-COMPONENT-NAME’, Compoent)
// I update the example it work for me 😀
// FYI: https://npm.runkit.com/tinykeys/dist/tinykeys.js?t=1611582484255
// Docs: https://github.com/jamiebuilds/tinykeys
tinykeys(window, {
"Shift+D": () => {
alert("The 'Shift' and 'd' keys were pressed at the same time")
},
"y e e t": () => {
alert("The keys 'y', 'e', 'e', and 't' were pressed in order")
},
"$mod+KeyD": event => {
event.preventDefault()
alert("Either 'Control+d' or 'Meta+d' were pressed")
},
})