mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-06 08:42:32 -08:00
Better keybinds
This commit is contained in:
1 parent
a9f252f59f
commit
1608c3ea91
16 files changed
+758
-292
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package dev.slimevr.keybind
|
||||
|
||||
val MODIFIER_ORDER = listOf("CTRL", "ALT", "SHIFT", "SUPER")
|
||||
private val NON_SHIFT_MODIFIERS = setOf("CTRL", "ALT", "SUPER")
|
||||
|
||||
private val LETTERS = ('A'..'Z').map { it.toString() }
|
||||
private val DIGITS = ('0'..'9').map { it.toString() }
|
||||
private val FUNCTION_KEYS = (1..24).map { "F$it" }
|
||||
private val ARROW_KEYS = listOf("UP", "DOWN", "LEFT", "RIGHT")
|
||||
private val NAV_KEYS = listOf("HOME", "END", "PAGE_UP", "PAGE_DOWN", "INSERT", "DELETE")
|
||||
private val CONTROL_KEYS = listOf("SPACE", "ENTER", "TAB", "BACKSPACE", "ESCAPE")
|
||||
private val NUMPAD_KEYS = (0..9).map { "NUMPAD_$it" } +
|
||||
listOf("NUMPAD_ADD", "NUMPAD_SUBTRACT", "NUMPAD_MULTIPLY", "NUMPAD_DIVIDE", "NUMPAD_DECIMAL", "NUMPAD_ENTER")
|
||||
private val PUNCTUATION_KEYS = listOf(
|
||||
"MINUS", "EQUAL", "BRACKET_LEFT", "BRACKET_RIGHT", "BACKSLASH",
|
||||
"SEMICOLON", "QUOTE", "BACKQUOTE", "COMMA", "PERIOD", "SLASH",
|
||||
)
|
||||
private val SYSTEM_KEYS = listOf("PRINT_SCREEN", "PAUSE", "SCROLL_LOCK", "NUM_LOCK", "CAPS_LOCK")
|
||||
val MEDIA_KEYS = listOf(
|
||||
"MEDIA_PLAY_PAUSE",
|
||||
"MEDIA_STOP",
|
||||
"MEDIA_NEXT",
|
||||
"MEDIA_PREVIOUS",
|
||||
"VOLUME_UP",
|
||||
"VOLUME_DOWN",
|
||||
"VOLUME_MUTE",
|
||||
)
|
||||
|
||||
/** Keys meaningfully bindable without a modifier: function, media/volume and standalone system keys. */
|
||||
val MODIFIER_OPTIONAL_KEYS: Set<String> =
|
||||
(FUNCTION_KEYS + MEDIA_KEYS + listOf("PRINT_SCREEN", "PAUSE", "SCROLL_LOCK")).toSet()
|
||||
|
||||
val KEYBIND_MAIN_KEYS: Set<String> = (
|
||||
LETTERS +
|
||||
DIGITS +
|
||||
FUNCTION_KEYS +
|
||||
ARROW_KEYS +
|
||||
NAV_KEYS +
|
||||
CONTROL_KEYS +
|
||||
NUMPAD_KEYS +
|
||||
PUNCTUATION_KEYS +
|
||||
SYSTEM_KEYS +
|
||||
MEDIA_KEYS
|
||||
).toSet()
|
||||
|
||||
fun canonicalKeybind(binding: String): String {
|
||||
val parts = binding.split('+').map { it.trim().uppercase() }.filter { it.isNotEmpty() }
|
||||
return (MODIFIER_ORDER.filter { it in parts } + parts.filterNot { it in MODIFIER_ORDER }).joinToString("+")
|
||||
}
|
||||
|
||||
fun isValidKeybind(binding: String): Boolean {
|
||||
val parts = binding.split('+').map { it.trim().uppercase() }.filter { it.isNotEmpty() }
|
||||
val key = parts.filterNot { it in MODIFIER_ORDER }.singleOrNull() ?: return false
|
||||
if (key !in KEYBIND_MAIN_KEYS) return false
|
||||
if (key in MODIFIER_OPTIONAL_KEYS) return true
|
||||
return parts.any { it in NON_SHIFT_MODIFIERS }
|
||||
}
|
||||
@@ -5,7 +5,10 @@ import dev.slimevr.config.Settings
|
||||
import dev.slimevr.config.SettingsActions
|
||||
import dev.slimevr.config.defaultKeybinds
|
||||
import dev.slimevr.keybind.KeybindActions
|
||||
import dev.slimevr.keybind.KeybindEvent
|
||||
import dev.slimevr.keybind.KeybindManager
|
||||
import dev.slimevr.keybind.canonicalKeybind
|
||||
import dev.slimevr.keybind.isValidKeybind
|
||||
import dev.slimevr.solarxr.SolarXRBridge
|
||||
import dev.slimevr.solarxr.SolarXRBridgeBehaviour
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
@@ -15,6 +18,7 @@ import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import solarxr_protocol.rpc.ChangeKeybindRequest
|
||||
import solarxr_protocol.rpc.Keybind
|
||||
import solarxr_protocol.rpc.KeybindActivatedResponse
|
||||
import solarxr_protocol.rpc.KeybindId
|
||||
import solarxr_protocol.rpc.KeybindRequest
|
||||
import solarxr_protocol.rpc.KeybindResponse
|
||||
@@ -29,22 +33,6 @@ private fun keybindConfigToProto(config: KeybindConfig): Keybind = Keybind(
|
||||
keybindDelay = config.delay,
|
||||
)
|
||||
|
||||
private val MODIFIER_ORDER = listOf("CTRL", "ALT", "SHIFT", "SUPER")
|
||||
|
||||
private val NON_SHIFT_MODIFIERS = setOf("CTRL", "ALT", "SUPER")
|
||||
|
||||
fun canonicalKeybind(binding: String): String {
|
||||
val parts = binding.split('+').map { it.trim().uppercase() }.filter { it.isNotEmpty() }
|
||||
return (MODIFIER_ORDER.filter { it in parts } + parts.filterNot { it in MODIFIER_ORDER }).joinToString("+")
|
||||
}
|
||||
|
||||
fun isValidKeybind(binding: String): Boolean {
|
||||
val parts = binding.split('+').map { it.trim().uppercase() }.filter { it.isNotEmpty() }
|
||||
val key = parts.filterNot { it in MODIFIER_ORDER }.singleOrNull() ?: return false
|
||||
if (!key.matches(Regex("[A-Z0-9]"))) return false
|
||||
return parts.any { it in NON_SHIFT_MODIFIERS }
|
||||
}
|
||||
|
||||
class KeybindsBehaviour(
|
||||
private val settings: Settings,
|
||||
private val keybindManager: KeybindManager,
|
||||
@@ -54,6 +42,10 @@ class KeybindsBehaviour(
|
||||
keybindManager.context.dispatch(KeybindActions.SetRecording(req.recording))
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
keybindManager.events.on<KeybindEvent.Fired> { event ->
|
||||
if (keybindManager.recording) receiver.sendRpc(KeybindActivatedResponse(keybindId = event.id))
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
fun buildResponse(keybinds: List<KeybindConfig>) = KeybindResponse(
|
||||
keybind = keybinds.map { keybindConfigToProto(it) },
|
||||
defaultKeybinds = defaultKeybinds().map { keybindConfigToProto(it) },
|
||||
|
||||
Reference in new issue
Block a user