1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.ui.input.key
18 
19 import androidx.compose.ui.Modifier
20 import androidx.compose.ui.node.DelegatableNode
21 
22 /**
23  * Implement this interface to create a [Modifier.Node] that can intercept hardware Key events.
24  *
25  * The event is routed to the focused item. Before reaching the focused item, [onPreKeyEvent]() is
26  * called for parents of the focused item. If the parents don't consume the event, [onPreKeyEvent]()
27  * is called for the focused item. If the event is still not consumed, [onKeyEvent]() is called on
28  * the focused item's parents.
29  */
30 interface KeyInputModifierNode : DelegatableNode {
31 
32     /**
33      * This function is called when a [KeyEvent] is received by this node during the upward pass.
34      * While implementing this callback, return true to stop propagation of this event. If you
35      * return false, the key event will be sent to this [KeyInputModifierNode]'s parent.
36      */
onKeyEventnull37     fun onKeyEvent(event: KeyEvent): Boolean
38 
39     /**
40      * This function is called when a [KeyEvent] is received by this node during the downward pass.
41      * It gives ancestors of a focused component the chance to intercept an event. Return true to
42      * stop propagation of this event. If you return false, the event will be sent to this
43      * [KeyInputModifierNode]'s child. If none of the children consume the event, it will be sent
44      * back up to the root using the [onKeyEvent] function.
45      */
46     fun onPreKeyEvent(event: KeyEvent): Boolean
47 }
48