From 1dc8a005ed111463c22c17b487e5ec8a3e2d30f3 Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Wed, 4 Mar 2026 21:19:40 +0800 Subject: refactor: refactor KBM input code --- Minecraft.Client/Minecraft.cpp | 146 ++++++++++++++++++++++++----------------- 1 file changed, 86 insertions(+), 60 deletions(-) (limited to 'Minecraft.Client/Minecraft.cpp') diff --git a/Minecraft.Client/Minecraft.cpp b/Minecraft.Client/Minecraft.cpp index bd75a61a..19ee79bd 100644 --- a/Minecraft.Client/Minecraft.cpp +++ b/Minecraft.Client/Minecraft.cpp @@ -502,6 +502,13 @@ void Minecraft::setScreen(Screen *screen) this->screen->removed(); } +#ifdef _WINDOWS64 + if (screen != NULL && g_KBMInput.IsMouseGrabbed()) + { + g_KBMInput.SetMouseGrabbed(false); + } +#endif + //4J Gordon: Do not force a stats save here /*if (dynamic_cast(screen)!=NULL) { @@ -1184,11 +1191,11 @@ void Minecraft::applyFrameMouseLook() int iPad = localplayers[i]->GetXboxPad(); if (iPad != 0) continue; // Mouse only applies to pad 0 - if (!KMInput.IsCaptured()) continue; + if (!g_KBMInput.IsMouseGrabbed()) continue; if (localgameModes[iPad] == NULL) continue; float rawDx, rawDy; - KMInput.ConsumeMouseDelta(rawDx, rawDy); + g_KBMInput.ConsumeMouseDelta(rawDx, rawDy); if (rawDx == 0.0f && rawDy == 0.0f) continue; float mouseSensitivity = ((float)app.GetGameSettings(iPad, eGameSetting_Sensitivity_InGame)) / 100.0f; @@ -1450,14 +1457,54 @@ void Minecraft::run_middle() // Keyboard/mouse button presses for player 0 if (i == 0) { - if (KMInput.ConsumeKeyPress(VK_ESCAPE)) localplayers[i]->ullButtonsPressed |= 1LL<ullButtonsPressed |= 1LL<ullButtonsPressed |= 1LL<ullButtonsPressed |= 1LL<ullButtonsPressed |= 1LL<ullButtonsPressed|=1LL<ullButtonsPressed|=1LL<ullButtonsPressed|=1LL<ullButtonsPressed|=1LL<ullButtonsPressed|=1LL<inventory) + localplayers[i]->inventory->selected = slot; + } + } + } + + // Utility keys always work regardless of KBM active state + if(g_KBMInput.IsKeyPressed(KeyboardMouseInput::KEY_PAUSE) && !ui.IsTutorialVisible(i)) + { + localplayers[i]->ullButtonsPressed|=1LL<ullButtonsPressed|=1LL<ullButtonsPressed|=1LL<abilities.flying && KMInput.IsKeyDown(VK_SHIFT) && !ui.GetMenuDisplayed(i)) - localplayers[i]->ullButtonsPressed |= 1LL<abilities.flying && !ui.GetMenuDisplayed(i)) + localplayers[i]->ullButtonsPressed|=1LL< 0 && gameMode->isInputAllowed(MINECRAFT_ACTION_LEFT_SCROLL)) wheel += 1; - else if (kbWheel < 0 && gameMode->isInputAllowed(MINECRAFT_ACTION_RIGHT_SCROLL)) wheel -= 1; - - // 1-9 keys for direct hotbar selection - if (gameMode->isInputAllowed(MINECRAFT_ACTION_LEFT_SCROLL)) - { - for (int k = '1'; k <= '9'; k++) - { - if (KMInput.ConsumeKeyPress(k)) - { - player->inventory->selected = k - '1'; - app.SetOpacityTimer(iPad); - break; - } - } - } + wheel = g_KBMInput.GetMouseWheel(); } #endif if (wheel != 0) @@ -3485,33 +3528,20 @@ void Minecraft::tick(bool bFirst, bool bUpdateTextures) player->handleMouseClick(0); player->lastClickTick[0] = ticks; } + #ifdef _WINDOWS64 - else if (iPad == 0 && KMInput.IsCaptured() && KMInput.ConsumeMousePress(0)) - { - player->handleMouseClick(0); - player->lastClickTick[0] = ticks; - } + bool actionHeld = InputManager.ButtonDown(iPad, MINECRAFT_ACTION_ACTION) || (iPad == 0 && g_KBMInput.IsKBMActive() && g_KBMInput.IsMouseButtonDown(KeyboardMouseInput::MOUSE_LEFT)); +#else + bool actionHeld = InputManager.ButtonDown(iPad, MINECRAFT_ACTION_ACTION); #endif - - if (InputManager.ButtonDown(iPad, MINECRAFT_ACTION_ACTION) && ticks - player->lastClickTick[0] >= timer->ticksPerSecond / 4) + if (actionHeld && ticks - player->lastClickTick[0] >= timer->ticksPerSecond / 4) { //printf("MINECRAFT_ACTION_ACTION ButtonDown"); player->handleMouseClick(0); player->lastClickTick[0] = ticks; } -#ifdef _WINDOWS64 - else if (iPad == 0 && KMInput.IsCaptured() && KMInput.IsMouseDown(0) && ticks - player->lastClickTick[0] >= timer->ticksPerSecond / 4) - { - player->handleMouseClick(0); - player->lastClickTick[0] = ticks; - } -#endif - if(InputManager.ButtonDown(iPad, MINECRAFT_ACTION_ACTION) -#ifdef _WINDOWS64 - || (iPad == 0 && KMInput.IsCaptured() && KMInput.IsMouseDown(0)) -#endif - ) + if(actionHeld) { player->handleMouseDown(0, true ); } @@ -3530,25 +3560,21 @@ void Minecraft::tick(bool bFirst, bool bUpdateTextures) lastClickTick = ticks; } */ - if( player->isUsingItem() ) - { - if(!InputManager.ButtonDown(iPad, MINECRAFT_ACTION_USE) #ifdef _WINDOWS64 - && !(iPad == 0 && KMInput.IsCaptured() && KMInput.IsMouseDown(1)) + bool useHeld = InputManager.ButtonDown(iPad, MINECRAFT_ACTION_USE) || (iPad == 0 && g_KBMInput.IsKBMActive() && g_KBMInput.IsMouseButtonDown(KeyboardMouseInput::MOUSE_RIGHT)); +#else + bool useHeld = InputManager.ButtonDown(iPad, MINECRAFT_ACTION_USE); #endif - ) gameMode->releaseUsingItem(player); + if( player->isUsingItem() ) + { + if(!useHeld) gameMode->releaseUsingItem(player); } else if( gameMode->isInputAllowed(MINECRAFT_ACTION_USE) ) { -#ifdef _WINDOWS64 - bool useButtonDown = InputManager.ButtonDown(iPad, MINECRAFT_ACTION_USE) || (iPad == 0 && KMInput.IsCaptured() && KMInput.IsMouseDown(1)); -#else - bool useButtonDown = InputManager.ButtonDown(iPad, MINECRAFT_ACTION_USE); -#endif if( player->abilities.instabuild ) { // 4J - attempt to handle click in special creative mode fashion if possible (used for placing blocks at regular intervals) - bool didClick = player->creativeModeHandleMouseClick(1, useButtonDown ); + bool didClick = player->creativeModeHandleMouseClick(1, useHeld ); // If this handler has put us in lastClick_oldRepeat mode then it is because we aren't placing blocks - behave largely as the code used to if( player->lastClickState == LocalPlayer::lastClick_oldRepeat ) { @@ -3560,7 +3586,7 @@ void Minecraft::tick(bool bFirst, bool bUpdateTextures) else { // Otherwise just the original game code for handling autorepeat - if (useButtonDown && ticks - player->lastClickTick[1] >= timer->ticksPerSecond / 4) + if (useHeld && ticks - player->lastClickTick[1] >= timer->ticksPerSecond / 4) { player->handleMouseClick(1); player->lastClickTick[1] = ticks; @@ -3576,7 +3602,7 @@ void Minecraft::tick(bool bFirst, bool bUpdateTextures) bool firstClick = ( player->lastClickTick[1] == 0 ); bool autoRepeat = ticks - player->lastClickTick[1] >= timer->ticksPerSecond / 4; if ( player->isRiding() || player->isSprinting() || player->isSleeping() ) autoRepeat = false; - if (useButtonDown ) + if (useHeld ) { // If the player has just exited a bed, then delay the time before a repeat key is allowed without releasing if(player->isSleeping() ) player->lastClickTick[1] = ticks + (timer->ticksPerSecond * 2); -- cgit v1.2.3