[a11y] Let keyboard shortcut Shift + F10 trigger the same action than a long click

This commit is contained in:
Benoit Marty 2025-08-05 09:41:08 +02:00
parent 27ad6217ec
commit d988ae3082
15 changed files with 104 additions and 30 deletions

View file

@ -0,0 +1,42 @@
/*
* Copyright 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.libraries.designsystem.modifiers
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.isShiftPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.input.key.type
/**
* Modifier to handle Shift + F10 key events.
* This is typically used to trigger context menus in desktop applications.
*
* @param onShiftF10Press The callback to invoke when Shift + F10 is pressed.
*/
fun Modifier.onShiftF10(
onShiftF10Press: (() -> Unit)?,
): Modifier = then(
if (onShiftF10Press == null) {
Modifier
} else {
Modifier.onKeyEvent { keyEvent ->
// invoke the callback when the user presses Shift + F10
if (keyEvent.type == KeyEventType.KeyUp &&
keyEvent.isShiftPressed &&
keyEvent.key == Key.F10) {
onShiftF10Press()
true
} else {
false
}
}
}
)