fix(samples): keep hidden input focused for keyboard input

This commit is contained in:
2026-05-15 21:03:16 +02:00
parent ef0e0c8dd2
commit 75fe72412e
2 changed files with 28 additions and 6 deletions
+27 -5
View File
@@ -72,8 +72,19 @@ const trigoKeys = [
];
const isTouchDevice = window.matchMedia('(pointer: coarse)').matches || 'ontouchstart' in window;
function focusInput() {
hiddenInput.focus();
if (!hiddenInput || isTouchDevice) return;
hiddenInput.focus({ preventScroll: true });
window.requestAnimationFrame(() => {
if (document.activeElement !== hiddenInput) {
hiddenInput.focus({ preventScroll: true });
}
if (typeof hiddenInput.select === 'function') {
hiddenInput.select();
}
});
}
let statusTimer = null;
@@ -218,7 +229,6 @@ function createKeyButton({ label, input, action, spacer, className }) {
button.textContent = label;
button.className = className;
button.addEventListener('click', () => {
focusInput();
if (input) {
inputToX(input);
render();
@@ -235,7 +245,7 @@ function buildGrid(container, keys) {
}
function handleKeyboard(event) {
if (event.target === hiddenInput) return;
if (event.defaultPrevented) return;
const key = event.key;
if (/^[0-9.]$/.test(key)) {
event.preventDefault();
@@ -405,17 +415,28 @@ rightButton.addEventListener('click', () => {
});
window.addEventListener('keydown', handleKeyboard);
window.addEventListener('keydown', handleKeyboard, { capture: true });
window.addEventListener('load', focusInput);
window.addEventListener('pageshow', focusInput);
window.addEventListener('focus', focusInput);
window.addEventListener('pointerdown', focusInput, true);
window.addEventListener('mousedown', focusInput, true);
window.addEventListener('click', focusInput, true);
hiddenInput.setAttribute('inputmode', 'none');
hiddenInput.setAttribute('readonly', 'readonly');
hiddenInput.addEventListener('focus', () => {
if (isTouchDevice) {
hiddenInput.blur();
return;
}
window.requestAnimationFrame(() => {
hiddenInput.select();
});
});
document.addEventListener('click', (event) => {
if (!event.target.closest('.calculator')) {
if (!isTouchDevice && !event.target.closest('.mode-menu')) {
focusInput();
}
});
@@ -424,3 +445,4 @@ buildGrid(keypadGrid, keypadKeys);
buildGrid(functionsGrid, functionKeys);
buildGrid(trigoGrid, trigoKeys);
render();
focusInput();