Defined in: key-state-tracker.ts:63
Singleton tracker for currently held keyboard keys.
This class maintains a list of all keys currently being pressed, which is useful for:
State Management:
const tracker = KeyStateTracker.getInstance()
// Access state directly
console.log(tracker.store.state.heldKeys) // ['Control', 'Shift']
// Subscribe to changes with TanStack Store
const unsubscribe = tracker.store.subscribe(() => {
console.log('Currently held:', tracker.store.state.heldKeys)
})
// Check current state
console.log(tracker.getHeldKeys()) // ['Control', 'Shift']
console.log(tracker.isKeyHeld('Control')) // true
// Cleanup
unsubscribe()const tracker = KeyStateTracker.getInstance()
// Access state directly
console.log(tracker.store.state.heldKeys) // ['Control', 'Shift']
// Subscribe to changes with TanStack Store
const unsubscribe = tracker.store.subscribe(() => {
console.log('Currently held:', tracker.store.state.heldKeys)
})
// Check current state
console.log(tracker.getHeldKeys()) // ['Control', 'Shift']
console.log(tracker.isKeyHeld('Control')) // true
// Cleanup
unsubscribe()readonly store: Store<KeyStateTrackerState>;readonly store: Store<KeyStateTrackerState>;Defined in: key-state-tracker.ts:70
The TanStack Store instance containing the tracker state. Use this to subscribe to state changes or access current state.
areAllKeysHeld(keys): boolean;areAllKeysHeld(keys): boolean;Defined in: key-state-tracker.ts:228
Checks if all of the given keys are currently held.
string[]
Array of key names to check
boolean
True if all of the keys are currently held
destroy(): void;destroy(): void;Defined in: key-state-tracker.ts:235
Destroys the tracker and removes all listeners.
void
getHeldKeys(): string[];getHeldKeys(): string[];Defined in: key-state-tracker.ts:197
Gets an array of currently held key names.
string[]
Array of key names currently being pressed
isAnyKeyHeld(keys): boolean;isAnyKeyHeld(keys): boolean;Defined in: key-state-tracker.ts:218
Checks if any of the given keys are currently held.
string[]
Array of key names to check
boolean
True if any of the keys are currently held
isKeyHeld(key): boolean;isKeyHeld(key): boolean;Defined in: key-state-tracker.ts:207
Checks if a specific key is currently being held.
string
The key name to check (case-insensitive)
boolean
True if the key is currently held
static getInstance(): KeyStateTracker;static getInstance(): KeyStateTracker;Defined in: key-state-tracker.ts:87
Gets the singleton instance of KeyStateTracker.
KeyStateTracker
static resetInstance(): void;static resetInstance(): void;Defined in: key-state-tracker.ts:97
Resets the singleton instance. Useful for testing.
void