inputModality()
Detect keyboard, mouse or touch input. Last input method.
Try it
Click or tap the sample button, then press Tab. The input card reflects the latest input type. Use this to adapt focus presentation without hiding useful feedback.
Click, tap or press a key.
<div id="your-component">
<div class="input-device" data-device="unknown"><span class="device-symbol" aria-hidden="true">⌘</span><strong data-value>Waiting for input</strong><span class="muted">Most recent input method</span></div><button>Try pointer or keyboard input</button><p class="demo-status" role="status">Click, tap or press a key.</p>
</div>import * as kit from 'subtly';
const el = document.querySelector('#your-component');
const listeners = new AbortController();
const { signal } = listeners;
const status = text => {
el.querySelector('.demo-status').textContent = text;
};
// Local simulation only. Replace with your real data operation.
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
function setup() {
const card = el.querySelector('.input-device');
const modality = kit.inputModality(value => {
card.dataset.device = value;
el.querySelector('[data-value]').textContent = value;
status('Latest input: ' + value);
});
el.querySelector('button').addEventListener('click', () => status('Activated using ' + modality.current), { signal });
return () => modality.destroy();
}
const stop = setup();
// Call when this component is removed.
function cleanup() {
listeners.abort();
stop?.();
}Add it to your project
- Copy the HTML. Use the HTML tab above and place the markup inside a dedicated component root. Layout classes belong to this documentation website; use your own styles.
- Connect the behavior. Copy the JavaScript tab, update
#your-componentand initialize it after the elements exist. Replace simulated operations with your own data. - Clean up on unmount. Call the example’s
cleanup()when the component is removed. It aborts application listeners and releases the utility’s timers or observers.
The preview and JavaScript tab use the same utility wiring. status() writes plain text into the example’s status element. wait() is a simulated local delay. For examples with data-ix attributes, start the declarative runtime once at application level, as shown in the code. Use your framework guide to place setup and cleanup correctly.
API and lifecycle
Create one tracker with an optional onChange callback. current begins as unknown, then reflects the last meaningful keyboard or pointer input. This describes the last interaction, not the visitor's device type.
- Interface
onChange; current, destroy.- Lifecycle
- Read current when needed. destroy() removes its captured pointerdown and keydown listeners; avoid creating a tracker for every button.
Accessibility and safety
Implementation notes
Initialize after the DOM is available. Keep native controls and keyboard interactions intact; release listeners and timers on unmount.
Prefer :focus-visible for focus rings. Do not hide essential controls or keyboard feedback based on an assumed input device.
What to check
Switch between keyboard, mouse and touch. Confirm focus remains visible when navigating with Tab and controls still work after switching methods.
View documented module source
/** Track last interaction method, not device type. Prefer :focus-visible for focus rings. */
export function inputModality(onChange){let current='unknown';function set(next){if(next!==current){current=next;onChange?.(next);}}function pointer(e){set(e.pointerType||'mouse');}function key(e){if(!e.metaKey&&!e.ctrlKey&&!e.altKey&&!['Shift','Control','Alt','Meta'].includes(e.key))set('keyboard');}window.addEventListener('pointerdown',pointer,true);window.addEventListener('keydown',key,true);return {get current(){return current;},destroy(){window.removeEventListener('pointerdown',pointer,true);window.removeEventListener('keydown',key,true);}};}