Closed igordmn closed 1 month ago
There should be a simple way to handle key presses on window/screen/dialog level.
Desktop currently have and additional API for that:
Window(onPreviewKeyEvent = )
There are issues with it:
Modifier.onPreviewKeyEvent
Alternatively, we can use this approach, but it is cumbersome:
import androidx.compose.foundation.focusable import androidx.compose.foundation.layout.Box import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusProperties import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.input.key.key import androidx.compose.ui.input.key.onPreviewKeyEvent @OptIn(ExperimentalComposeUiApi::class) @Composable fun App() { val focusRequester = remember { FocusRequester() } Box( Modifier .focusRequester(focusRequester) .focusProperties{ exit = { focusRequester } } // make LocalFocusManager.current.clear() to focus this node instead of the root window node .focusable() .onPreviewKeyEvent { println(it.key) true } ) LaunchedEffect(Unit) { focusRequester.requestFocus() } }
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.
There should be a simple way to handle key presses on window/screen/dialog level.
Desktop currently have and additional API for that:
There are issues with it:
Modifier.onPreviewKeyEvent
)Alternatively, we can use this approach, but it is cumbersome: