Skip to main content

Mouse and keyboard input

Desktop apps are driven by a mouse or trackpad and a keyboard. This page describes the events a view receives from them.

Mouse buttons​

Mouse buttons use the W3C numbering:

buttonButton
0Left (primary)
1Middle
2Right (secondary)
3Back
4Forward

The left button is a press. It drives React Native's responder system, so Pressable, Button and the Touchable components react to it, and the touch events (onTouchStart, onTouchMove, onTouchEnd) fire.

The other buttons never start a press: a right click does not make a Pressable fire onPress. They are only reported by the pointer events below. A right click also opens a context menu, and the back button fires BackHandler.

Pointer events​

View takes the pointer events of the web. Each has a capturing counterpart, such as onPointerDownCapture.

PropFires
onPointerDownA button is pressed over the view.
onPointerUpA button is released.
onPointerMoveThe pointer moves over the view.
onPointerCancelThe press is interrupted.
onPointerEnterThe pointer enters the view. It does not bubble.
onPointerLeaveThe pointer leaves the view. It does not bubble.
onPointerOverThe pointer enters the view or one of its children.
onPointerOutThe pointer leaves the view or one of its children.
React TSX
<View
onPointerEnter={() => setHovered(true)}
onPointerLeave={() => setHovered(false)}
onPointerDown={event => {
if (event.nativeEvent.button === 2) openMenu();
}}
style={{ backgroundColor: hovered ? '#eee' : 'white' }}
/>

The event's nativeEvent has these fields:

FieldMeaning
pointerTypeAlways 'mouse'. Trackpads report as a mouse.
pointerId, isPrimaryOne pointer exists, and it is primary.
buttonThe button that changed, or -1 for a move.
buttonsA mask of the buttons held: 1 left, 2 right, 4 middle, 8 back, 16 forward.
ctrlKey, shiftKey, altKey, metaKeyThe modifier keys held. metaKey is Command on macOS and the Windows key elsewhere.
clientX, clientY (also x, y, pageX, pageY, screenX, screenY)The pointer position in the window, in points.
offsetX, offsetYThe position inside the view that the event is sent to.
targetThe tag of the view under the pointer.

Scrolling​

The mouse wheel and the trackpad scroll a ScrollView and the lists built on it. There is no wheel event for other views. For scrolling in your own components, wrap the content in a ScrollView.

Keyboard​

A view receives key events while it has keyboard focus. Make a view focusable with the focusable prop, or use a component that takes focus itself, such as TextInput.

PropFires
onKeyDownA key is pressed, and while it repeats.
onKeyUpA key is released.
onFocusThe view gets focus.
onBlurThe view loses focus.
React TSX
<View
focusable
onKeyDown={event => {
if (event.nativeEvent.key === 'Escape') close();
}}
/>

The key event's nativeEvent has:

FieldMeaning
keyThe key as the web names it: 'a', 'Enter', 'ArrowLeft'.
codeThe physical key, such as 'KeyA'.
keyCodeThe legacy numeric key code.
repeattrue while the key is held down.
ctrlKey, shiftKey, altKey, metaKeyThe modifier keys held.

onFocus events carry focusVisible, which is true when focus arrived through the keyboard. Use it to draw a focus ring only for keyboard users.

For shortcuts that should work wherever the user is in the app, use keyboard shortcuts instead of onKeyDown. They are resolved natively, and take precedence over a text input and over menu accelerators.

Text input​

TextInput handles typing, selection with the mouse and the keyboard, the clipboard, and input methods for you. See TextInput.