fix: some modifier keybinds not working

fixes https://github.com/nea89o/Firmament/issues/107
This commit is contained in:
Linnea Gräf
2025-06-07 23:28:02 +02:00
parent 029fd15278
commit d8472888c0
2 changed files with 82 additions and 80 deletions

View File

@@ -48,7 +48,7 @@ object PriceData : FirmamentFeature {
} }
val sbId = it.stack.skyBlockId val sbId = it.stack.skyBlockId
val stackSize = it.stack.count val stackSize = it.stack.count
val isShowingStack = TConfig.stackSizeKey.isPressed(atLeast = true) val isShowingStack = TConfig.stackSizeKey.isPressed()
val multiplier = if (isShowingStack) stackSize else 1 val multiplier = if (isShowingStack) stackSize else 1
val multiplierText = val multiplierText =
if (isShowingStack) if (isShowingStack)

View File

@@ -1,5 +1,3 @@
package moe.nea.firmament.keybindings package moe.nea.firmament.keybindings
import org.lwjgl.glfw.GLFW import org.lwjgl.glfw.GLFW
@@ -11,102 +9,106 @@ import moe.nea.firmament.util.MC
@Serializable @Serializable
data class SavedKeyBinding( data class SavedKeyBinding(
val keyCode: Int, val keyCode: Int,
val shift: Boolean = false, val shift: Boolean = false,
val ctrl: Boolean = false, val ctrl: Boolean = false,
val alt: Boolean = false, val alt: Boolean = false,
) : IKeyBinding { ) : IKeyBinding {
val isBound: Boolean get() = keyCode != GLFW.GLFW_KEY_UNKNOWN val isBound: Boolean get() = keyCode != GLFW.GLFW_KEY_UNKNOWN
constructor(keyCode: Int, mods: Triple<Boolean, Boolean, Boolean>) : this( constructor(keyCode: Int, mods: Triple<Boolean, Boolean, Boolean>) : this(
keyCode, keyCode,
mods.first && keyCode != GLFW.GLFW_KEY_LEFT_SHIFT && keyCode != GLFW.GLFW_KEY_RIGHT_SHIFT, mods.first && keyCode != GLFW.GLFW_KEY_LEFT_SHIFT && keyCode != GLFW.GLFW_KEY_RIGHT_SHIFT,
mods.second && keyCode != GLFW.GLFW_KEY_LEFT_CONTROL && keyCode != GLFW.GLFW_KEY_RIGHT_CONTROL, mods.second && keyCode != GLFW.GLFW_KEY_LEFT_CONTROL && keyCode != GLFW.GLFW_KEY_RIGHT_CONTROL,
mods.third && keyCode != GLFW.GLFW_KEY_LEFT_ALT && keyCode != GLFW.GLFW_KEY_RIGHT_ALT, mods.third && keyCode != GLFW.GLFW_KEY_LEFT_ALT && keyCode != GLFW.GLFW_KEY_RIGHT_ALT,
) )
constructor(keyCode: Int, mods: Int) : this(keyCode, getMods(mods)) constructor(keyCode: Int, mods: Int) : this(keyCode, getMods(mods))
companion object { companion object {
fun getMods(modifiers: Int): Triple<Boolean, Boolean, Boolean> { fun getMods(modifiers: Int): Triple<Boolean, Boolean, Boolean> {
return Triple( return Triple(
modifiers and GLFW.GLFW_MOD_SHIFT != 0, modifiers and GLFW.GLFW_MOD_SHIFT != 0,
modifiers and GLFW.GLFW_MOD_CONTROL != 0, modifiers and GLFW.GLFW_MOD_CONTROL != 0,
modifiers and GLFW.GLFW_MOD_ALT != 0, modifiers and GLFW.GLFW_MOD_ALT != 0,
) )
} }
fun getModInt(): Int { fun getModInt(): Int {
val h = MC.window.handle val h = MC.window.handle
val ctrl = if (MinecraftClient.IS_SYSTEM_MAC) { val ctrl = if (MinecraftClient.IS_SYSTEM_MAC) {
InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_LEFT_SUPER) InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_LEFT_SUPER)
|| InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_RIGHT_SUPER) || InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_RIGHT_SUPER)
} else InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_LEFT_CONTROL) } else InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_LEFT_CONTROL)
|| InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_RIGHT_CONTROL) || InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_RIGHT_CONTROL)
val shift = isShiftDown() val shift = isShiftDown()
val alt = InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_LEFT_ALT) val alt = InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_LEFT_ALT)
|| InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_RIGHT_ALT) || InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_RIGHT_ALT)
var mods = 0 var mods = 0
if (ctrl) mods = mods or GLFW.GLFW_MOD_CONTROL if (ctrl) mods = mods or GLFW.GLFW_MOD_CONTROL
if (shift) mods = mods or GLFW.GLFW_MOD_SHIFT if (shift) mods = mods or GLFW.GLFW_MOD_SHIFT
if (alt) mods = mods or GLFW.GLFW_MOD_ALT if (alt) mods = mods or GLFW.GLFW_MOD_ALT
return mods return mods
} }
private val h get() = MC.window.handle private val h get() = MC.window.handle
fun isShiftDown() = InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_LEFT_SHIFT) fun isShiftDown() = shiftKeys.any { InputUtil.isKeyPressed(h, it) }
|| InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_RIGHT_SHIFT)
fun unbound(): SavedKeyBinding = fun unbound(): SavedKeyBinding =
SavedKeyBinding(GLFW.GLFW_KEY_UNKNOWN) SavedKeyBinding(GLFW.GLFW_KEY_UNKNOWN)
val controlKeys = if (MinecraftClient.IS_SYSTEM_MAC) {
listOf(GLFW.GLFW_KEY_LEFT_SUPER, GLFW.GLFW_KEY_RIGHT_SUPER)
} else {
listOf(GLFW.GLFW_KEY_LEFT_CONTROL, GLFW.GLFW_KEY_RIGHT_CONTROL)
}
val shiftKeys = listOf(GLFW.GLFW_KEY_LEFT_SHIFT, GLFW.GLFW_KEY_RIGHT_SHIFT)
val altKeys = listOf(GLFW.GLFW_KEY_LEFT_ALT, GLFW.GLFW_KEY_RIGHT_ALT)
} }
fun isPressed(atLeast: Boolean = false): Boolean { fun isPressed(atLeast: Boolean = false): Boolean {
if (!isBound) return false if (!isBound) return false
val h = MC.window.handle val h = MC.window.handle
if (!InputUtil.isKeyPressed(h, keyCode)) return false if (!InputUtil.isKeyPressed(h, keyCode)) return false
val ctrl = if (MinecraftClient.IS_SYSTEM_MAC) { // These are modifiers, so if the searched keyCode is a modifier key, then that key does not count as the modifier
InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_LEFT_SUPER) val ctrl = keyCode !in controlKeys && controlKeys.any { InputUtil.isKeyPressed(h, it) }
|| InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_RIGHT_SUPER) val shift = keyCode !in shiftKeys && isShiftDown()
} else InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_LEFT_CONTROL) val alt = keyCode !in altKeys && altKeys.any { InputUtil.isKeyPressed(h, it) }
|| InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_RIGHT_CONTROL) if (atLeast)
val shift = isShiftDown() return (ctrl >= this.ctrl) &&
val alt = InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_LEFT_ALT) (alt >= this.alt) &&
|| InputUtil.isKeyPressed(h, GLFW.GLFW_KEY_RIGHT_ALT) (shift >= this.shift)
if (atLeast)
return (ctrl >= this.ctrl) &&
(alt >= this.alt) &&
(shift >= this.shift)
return (ctrl == this.ctrl) && return (ctrl == this.ctrl) &&
(alt == this.alt) && (alt == this.alt) &&
(shift == this.shift) (shift == this.shift)
} }
override fun matches(keyCode: Int, scanCode: Int, modifiers: Int): Boolean { override fun matches(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
if (this.keyCode == GLFW.GLFW_KEY_UNKNOWN) return false if (this.keyCode == GLFW.GLFW_KEY_UNKNOWN) return false
return keyCode == this.keyCode && getMods(modifiers) == Triple(shift, ctrl, alt) return keyCode == this.keyCode && getMods(modifiers) == Triple(shift, ctrl, alt)
} }
override fun toString(): String { override fun toString(): String {
return format().string return format().string
} }
fun format(): Text { fun format(): Text {
val stroke = Text.literal("") val stroke = Text.literal("")
if (ctrl) { if (ctrl) {
stroke.append("CTRL + ") stroke.append("CTRL + ")
} }
if (alt) { if (alt) {
stroke.append("ALT + ") stroke.append("ALT + ")
} }
if (shift) { if (shift) {
stroke.append("SHIFT + ") // TODO: translations? stroke.append("SHIFT + ") // TODO: translations?
} }
stroke.append(InputUtil.Type.KEYSYM.createFromCode(keyCode).localizedText) stroke.append(InputUtil.Type.KEYSYM.createFromCode(keyCode).localizedText)
return stroke return stroke
} }
} }