-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle key events at a platform level instead of Modifier
This is done so that we can track key events without the requirement of having a component focused. This is needed because when the Popup is displayed, no component have focus and we need to focus on the first item when KEY_DOWN is pressed.
- Loading branch information
Showing
8 changed files
with
154 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import android.view.KeyEvent | ||
import android.view.View.OnKeyListener | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.ui.input.key.Key | ||
import androidx.compose.ui.platform.LocalView | ||
|
||
@Composable | ||
internal actual fun KeyDownHandler(onEvent: (KeyDownEvent) -> Boolean) { | ||
val view = LocalView.current | ||
|
||
DisposableEffect(Unit) { | ||
val listener = OnKeyListener { _, keyCode, event -> | ||
if (event.action == KeyEvent.ACTION_DOWN) { | ||
onEvent(KeyDownEvent(Key(keyCode))) | ||
} else { | ||
false | ||
} | ||
} | ||
view.setOnKeyListener(listener) | ||
onDispose { | ||
view.setOnKeyListener(null) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import androidx.compose.runtime.Composable | ||
|
||
@Composable | ||
internal actual fun KeyDownHandler(onEvent: (KeyDownEvent) -> Boolean) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.input.key.Key | ||
|
||
internal data class KeyDownEvent(val key: Key) | ||
|
||
@Composable | ||
internal expect fun KeyDownHandler(onEvent: (KeyDownEvent) -> Boolean) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.ui.input.key.Key | ||
import java.awt.KeyEventDispatcher | ||
import java.awt.KeyboardFocusManager | ||
import java.awt.event.KeyEvent | ||
|
||
@Composable | ||
internal actual fun KeyDownHandler(onEvent: (KeyDownEvent) -> Boolean) { | ||
DisposableEffect(Unit) { | ||
val dispatcher = KeyEventDispatcher { keyEvent -> | ||
if (keyEvent.id == KeyEvent.KEY_PRESSED) { | ||
val keyDownEvent = KeyDownEvent(Key(keyEvent.keyCode)) | ||
onEvent(keyDownEvent) | ||
} else { | ||
false | ||
} | ||
} | ||
val keyboardFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager() | ||
keyboardFocusManager.addKeyEventDispatcher(dispatcher) | ||
onDispose { | ||
keyboardFocusManager.removeKeyEventDispatcher(dispatcher) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.ui.input.key.Key | ||
import kotlinx.browser.document | ||
import org.w3c.dom.events.Event | ||
import org.w3c.dom.events.KeyboardEvent | ||
|
||
@Composable | ||
internal actual fun KeyDownHandler(onEvent: (KeyDownEvent) -> Boolean) { | ||
DisposableEffect(Unit) { | ||
// workaround until KT-64565 is fixed: https://youtrack.jetbrains.com/issue/KT-64565/Kotlin-wasm-removeEventListener-function-did-not-remove-the-event-listener | ||
var isDisabled = false | ||
|
||
val listener: (Event) -> Unit = { e -> | ||
if (isDisabled.not()) { | ||
val keyboardEvent = e as KeyboardEvent | ||
val result = onEvent(KeyDownEvent(Key(keyboardEvent.keyCode.toLong()))) | ||
if (result) { | ||
e.preventDefault() | ||
} | ||
} | ||
} | ||
document.addEventListener("keydown", listener, true) | ||
onDispose { | ||
document.removeEventListener("keydown", listener, true) | ||
isDisabled = true | ||
} | ||
} | ||
} |