It is necessary to add the event handler globally (attach it to the document element). Attaching it to the menu element would only work after calling focus(), and you would also have to handle focus loss.
If you don’t want to add a permanent global handler, you can add it in onShow, and remove it in onHide:
const escHandler = (ev) => {
if (e.key === "Escape") {
DayPilot.Menu.hide();
}
};
const menu = new DayPilot.Menu({
items: [...],
onShow: args => {
document.addEventListener("keydown", escHandler);
},
onHide: args => {
document.removeEventListener("keydown", escHandler);
},
// ...
});