// Force-capture the spacebar before the theme can scroll the page
window.addEventListener('keydown', function(e) {
// 32 is the keycode for Space
if (e.keyCode === 32 || e.code === 'Space') {
const activeEl = document.activeElement;
// Check if the focus is currently on the Botpress iframe or the page body
// On WordPress, when you click into the bot, the 'iframe' becomes the active element
if (activeEl && (activeEl.tagName === 'IFRAME' || activeEl === document.body)) {
// Only stop the scroll if the user is likely interacting with the bot
// Botpress iframes usually contain 'botpress' in the src or have specific IDs
if (activeEl.id.includes('bp-web-widget') || (activeEl.src && activeEl.src.includes('botpress'))) {
e.stopImmediatePropagation(); // Kills the event for all other scripts
// We don't call preventDefault() here because the iframe
// needs the spacebar to actually type the space inside the chat.
}
}
}
}, true); // 'true' enables the "Capture" phase, catching the event early