compose-multiplatform
compose-multiplatform copied to clipboard
ShortcutsModifier - pressedKeys stuck when a child window is opened from a shortcut handler
If a keyboard shortcut is assigned to an AppWindow and its handler leads to opening of another window, the pressed key stays in the ShortcutsInstance's pressedKeys set.
import androidx.compose.desktop.AppWindow
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import javax.swing.SwingUtilities
fun main() {
SwingUtilities.invokeLater {
var aIsPressed by mutableStateOf<Boolean>(false)
AppWindow(title = "Parent").apply {
show {
var dialogShown by remember { mutableStateOf(false) }
if (aIsPressed) {
dialogShown = true
aIsPressed = false
}
if (dialogShown) {
Dialog({ dialogShown = false }, DialogProperties(title = "Child")) { }
}
}
keyboard.setShortcut(Key.A) {
println("A was pressed.")
aIsPressed = true
}
}
}
}
- In the Parent window, press
A. - The Child window is opened.
- Close the Child window.
- Click the Parent window so it gets the focus.
- Press any key other than
A. The keyboard shortcut handler is erroneously fired which leads to another opening of the Child window.
There is a clumsy workaround for this bug: delay the opening of the Child window for a sufficient (100 ms) amount of time:
val composeScope = rememberCoroutineScope()
...
if (aIsPressed) {
aIsPressed = false
composeScope.launch {
delay(100)
dialogShown = true
}
}
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.