Add switchable hardware input backend

This commit is contained in:
王鹏
2026-04-20 15:53:32 +08:00
parent 66b8edf8e4
commit beb1375d71
5 changed files with 278 additions and 68 deletions

View File

@@ -323,6 +323,7 @@ class GameLoopWorker(QThread):
release_spirit_key=None,
resurrect_key=None,
enable_mouse_loot=True,
use_hardware_input=True,
):
super().__init__()
self.mode = mode # 'monitor' | 'patrol' | 'combat' | 'quest_follow' | 'flight' | 'record'
@@ -362,14 +363,25 @@ class GameLoopWorker(QThread):
self.release_spirit_key = release_spirit_key
self.resurrect_key = resurrect_key
self.enable_mouse_loot = enable_mouse_loot
self.use_hardware_input = bool(use_hardware_input)
def run(self):
try:
from game_state import parse_game_state, format_game_state_line
from hardware_control import hw_ctrl
except ImportError:
self.log_signal.emit("❌ 无法导入 game_state 模块")
return
hw_ctrl.configure(use_hardware_input=self.use_hardware_input)
backend_text = {
"hardware": "硬件",
"hardware_unavailable": "硬件(不可用)",
"directinput": "pydirectinput",
"directinput_unavailable": "pydirectinput(不可用)",
}.get(hw_ctrl.backend_label(), "未知")
self.log_signal.emit(f"⌨️ 控制方式: {backend_text}")
if self.mode == 'patrol':
try:
from auto_bot_move import AutoBotMove, load_waypoints
@@ -1076,6 +1088,8 @@ class WoWMultiKeyGUI(QMainWindow):
self.gs_resurrect_key.setText("0")
self.gs_enable_mouse_loot = QCheckBox("启用扫雷拾取")
self.gs_enable_mouse_loot.setChecked(True)
self.gs_use_hardware_input = QCheckBox("启用硬件控制(关闭则用 pydirectinput")
self.gs_use_hardware_input.setChecked(True)
# 网格填充
game_grid.addWidget(QLabel("剥皮等待时间:"), 0, 0)
@@ -1103,9 +1117,10 @@ class WoWMultiKeyGUI(QMainWindow):
game_grid.addWidget(QLabel("释放灵魂按键:"), 4, 2)
game_grid.addWidget(self.gs_release_spirit_key, 4, 3)
game_grid.addWidget(self.gs_bag_full_hearthstone, 5, 1)
game_grid.addWidget(self.gs_use_hardware_input, 5, 0, 1, 2)
game_grid.addWidget(QLabel("复活按键:"), 5, 2)
game_grid.addWidget(self.gs_resurrect_key, 5, 3)
game_grid.addWidget(self.gs_bag_full_hearthstone, 6, 1)
params_layout.addWidget(game_group)
@@ -1158,12 +1173,14 @@ class WoWMultiKeyGUI(QMainWindow):
self.eat_hp_threshold_spin.setValue(int(bot_cfg.get('eat_hp_threshold', 30)))
self.eat_max_wait_spin.setValue(float(bot_cfg.get('eat_max_wait_sec', 30.0)))
self.gs_enable_mouse_loot.setChecked(bool(bot_cfg.get('enable_mouse_loot', True)))
self.gs_use_hardware_input.setChecked(bool(bot_cfg.get('use_hardware_input', True)))
except Exception:
self.skinning_wait_spin.setValue(1.5)
self.food_key_edit.setText('f1')
self.eat_hp_threshold_spin.setValue(30)
self.eat_max_wait_spin.setValue(30.0)
self.gs_enable_mouse_loot.setChecked(True)
self.gs_use_hardware_input.setChecked(True)
def _save_params_config(self):
"""保存「参数配置」界面到 game_state_config.json多分辨率并写入 wow_multikey_qt.jsonbot 参数)"""
@@ -1192,7 +1209,10 @@ class WoWMultiKeyGUI(QMainWindow):
self.config['bot']['eat_hp_threshold'] = int(self.eat_hp_threshold_spin.value())
self.config['bot']['eat_max_wait_sec'] = float(self.eat_max_wait_spin.value())
self.config['bot']['enable_mouse_loot'] = self.gs_enable_mouse_loot.isChecked()
self.config['bot']['use_hardware_input'] = self.gs_use_hardware_input.isChecked()
self._save_main_config()
from hardware_control import hw_ctrl
hw_ctrl.configure(use_hardware_input=self.gs_use_hardware_input.isChecked())
self.log(f"✅ 参数配置已保存至 {path},并更新 bot 参数")
QMessageBox.information(self, "保存成功", f"参数配置已保存至:\n{path}\n\nBot 参数已写入:\n{self.config_path}")
@@ -1640,6 +1660,7 @@ class WoWMultiKeyGUI(QMainWindow):
resurrect_key = '0'
enable_mouse_loot = self.gs_enable_mouse_loot.isChecked()
use_hardware_input = self.gs_use_hardware_input.isChecked()
self.game_worker = GameLoopWorker(
mode, waypoints_path=waypoints_path, vendor_path=vendor_path,
@@ -1660,6 +1681,7 @@ class WoWMultiKeyGUI(QMainWindow):
release_spirit_key=release_spirit_key,
resurrect_key=resurrect_key,
enable_mouse_loot=enable_mouse_loot,
use_hardware_input=use_hardware_input,
)
self.game_worker.state_signal.connect(self.state_label.setText)
self.game_worker.log_signal.connect(self.log)