Skip to content

Commit

Permalink
Fix browser clipboard events handling on Windows (#1329)
Browse files Browse the repository at this point in the history
**Solution:** When Ctrl is pressed, isTypedEvent should return false

Fixes JetBrains/compose-multiplatform#4728

## Testing
Added two more unit tests.

## Release Notes
### Fixes - Web

- _(prerelease fix)_ Fix Browser Clipboard Events and other shortcuts
handling on Windows
  • Loading branch information
eymar committed May 1, 2024
1 parent a82ef0f commit a78ba34
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ package androidx.compose.foundation.text
import androidx.compose.ui.dom.domEventOrNull
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.isCtrlPressed
import androidx.compose.ui.input.key.isMetaPressed
import androidx.compose.ui.input.key.type
import org.w3c.dom.events.KeyboardEvent

actual val KeyEvent.isTypedEvent: Boolean
get() = type == KeyEventType.KeyDown && !isMetaPressed && domEventOrNull?.isPrintable() == true
get() = type == KeyEventType.KeyDown
&& !isMetaPressed
&& !isCtrlPressed
&& domEventOrNull?.isPrintable() == true

private fun KeyboardEvent.isPrintable(): Boolean {
return key.firstOrNull()?.toString() == key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ class IsTypedEventTests {
keyDownEvents.forEach { event -> event.assertIsNotTyped() }
}

@Test
fun shortcutsWithCtrlOnlyAreNotTyped() {
val keyDownEvents = listOf(
keyDownEvent('c', metaKey = false, ctrlKey = true),
keyDownEvent('p', metaKey = false, ctrlKey = true),
keyDownEvent('v', metaKey = false, ctrlKey = true)
)

keyDownEvents.forEach { event -> event.assertIsNotTyped() }
}

@Test
fun shortcutsWithMetaOnlyAreNotTyped() {
val keyDownEvents = listOf(
keyDownEvent('c', metaKey = true, ctrlKey = false),
keyDownEvent('p', metaKey = true, ctrlKey = false),
keyDownEvent('v', metaKey = true, ctrlKey = false)
)

keyDownEvents.forEach { event -> event.assertIsNotTyped() }
}

@Test
fun altProducesATypedEvent() {
val keyDownEvents = listOf(
keyDownEvent('c', altKey = true),
keyDownEvent('p', altKey = true),
keyDownEvent('v', altKey = true)
)

keyDownEvents.forEach { event -> event.assertIsTyped() }
}

@Test
fun functionalsAreNotTyped() {
val keyDownEvents = listOf(
Expand Down

0 comments on commit a78ba34

Please sign in to comment.