apps/docs/extend/sensors/keyboard-sensor.mdx
The Keyboard sensor enables keyboard-based drag and drop interactions. It is enabled by default in the DragDropManager.
import {DragDropManager} from '@dnd-kit/dom';
import {KeyboardSensor} from '@dnd-kit/dom/sensors';
const manager = new DragDropManager({
sensors: [
KeyboardSensor.configure({
keyboardCodes: {
start: ['Space', 'Enter'],
cancel: ['Escape'],
end: ['Space', 'Enter'],
up: ['ArrowUp'],
down: ['ArrowDown'],
left: ['ArrowLeft'],
right: ['ArrowRight'],
},
}),
],
});
The Keyboard sensor comes with these default key bindings:
const defaultKeyboardCodes = {
start: ['Space', 'Enter'], // Start dragging
cancel: ['Escape'], // Cancel drag operation
end: ['Space', 'Enter'], // End dragging
up: ['ArrowUp'], // Move up
down: ['ArrowDown'], // Move down
left: ['ArrowLeft'], // Move left
right: ['ArrowRight'], // Move right
};
You can customize which keys trigger different actions:
KeyboardSensor.configure({
keyboardCodes: {
// Use Tab to start/end dragging
start: ['Tab'],
end: ['Tab'],
// Use WASD for movement
up: ['KeyW'],
down: ['KeyS'],
left: ['KeyA'],
right: ['KeyD'],
// Additional cancel keys
cancel: ['Escape', 'KeyQ'],
},
});
By default, each key press moves the dragged item by 10 pixels. Hold <kbd>Shift</kbd> to move by 50 pixels instead.
interface KeyboardCodes {
start: KeyCode[]; // Start dragging
cancel: KeyCode[]; // Cancel operation
end: KeyCode[]; // End dragging
up: KeyCode[]; // Move up
down: KeyCode[]; // Move down
left: KeyCode[]; // Move left
right: KeyCode[]; // Move right
}
type KeyCode = KeyboardEvent['code'];
The Keyboard sensor handles these events:
keydown: Key press detectionkeyup: Key release detectionThe sensor activates when:
Provide clear focus indicators:
Support screen readers:
Consider key combinations: