使用SDL2 获取左摇杆输入 并转化为isleft, isright, isup, isdown
- User
使用SDL2 获取左摇杆输入 并转化为is_left, is_right, is_up, is_down
- Assistant
- 获取左摇杆的输入值:
使用SDL_JoystickGetAxis函数获取左摇杆在X和Y方向上的输入值。
Sint16 left_x = SDL_JoystickGetAxis(joystick, 0); Sint16 left_y = SDL_JoystickGetAxis(joystick, 1);- 判断方向:
根据左摇杆的输入值判断方向,在判断方向之前需要设定一个输入的阈值,当左摇杆的输入值大于或小于此阈值时,才被认为是输入了方向。
const int JOYSTICK_DEAD_ZONE = 8000; bool is_left = false; bool is_right = false; bool is_up = false; bool is_down = false; if (left_x < -JOYSTICK_DEAD_ZONE) is_left = true; else if (left_x > JOYSTICK_DEAD_ZONE) is_right = true; if (left_y < -JOYSTICK_DEAD_ZONE) is_up = true; else if (left_y > JOYSTICK_DEAD_ZONE) is_down = true;- 完整代码:
//打开手柄 SDL_Init(SDL_INIT_JOYSTICK); SDL_Joystick* joystick = SDL_JoystickOpen(0); //设定输入阈值 const int JOYSTICK_DEAD_ZONE = 8000; //获取左摇杆的输入值 Sint16 left_x = SDL_JoystickGetAxis(joystick, 0); Sint16 left_y = SDL_JoystickGetAxis(joystick, 1); //判断方向 bool is_left = false; bool is_right = false; bool is_up = false; bool is_down = false; if (left_x < -JOYSTICK_DEAD_ZONE) is_left = true; else if (left_x > JOYSTICK_DEAD_ZONE) is_right = true; if (left_y < -JOYSTICK_DEAD_ZONE) is_up = true; else if (left_y > JOYSTICK_DEAD_ZONE) is_down = true; //关闭手柄 SDL_JoystickClose(joystick); SDL_Quit(); - User
使用LT RT模拟 鼠标事件可以吗
- Assistant
- User
bool isMouseBtnDown = false, isMouseBtnUp = false, isLeftBtn = false, isRightBtn = false; float lt = Input::GetGamepadLeftTrigger(), rt = Input::GetGamepadRightTrigger(), trigger = 0.0f;
if (lt >= Input::DeadZone) { trigger = lt; isLeftBtn = true; } else if (rt >= Input::DeadZone) { trigger = rt; isRightBtn = true; } if (trigger >= Input::DeadZone) { isMouseBtnDown = true; } else if (trigger < (1.0f - Input::DeadZone)) { isMouseBtnUp = true; } 这样是否有错误 - Assistant
在代码中并没有重大错误,不过有些细节需要注意。
在这个代码中,如果输入的左摇杆和右摇杆的值都超过了死区的值,则只会模拟左鼠标键点击事件。如果要同时模拟左右鼠标键,需要增加相应的逻辑判断。
if (lt >= Input::DeadZone && rt >= Input::DeadZone) { // 判断左摇杆和右摇杆哪个数值大,将大的设置为点击右键,小的设置为点击左键 if (lt > rt) { trigger = lt; isLeftBtn = true; } else { trigger = rt; isRightBtn = true; } } else if (lt >= Input::DeadZone) { trigger = lt; isLeftBtn = true; } else if (rt >= Input::DeadZone) { trigger = rt; isRightBtn = true; }此外,由于把isMouseBtnDown和isMouseBtnUp设为true后没有重新将它们设为false,所以代码只能模拟一次点击或者一次松开事件。如果需要连续模拟事件,可以将isMouseBtnDown和isMouseBtnUp设为函数内的局部变量,并在每次输入轮询开始前将它们重新设为false。
- User
while (SDL_PollEvent(&event)) { switch (event.type) { // 处理 LT 触发的鼠标事件 case SDL_CONTROLLERAXISMOTION: if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { if (event.caxis.value > 0) { // LT 按下 if (!mouse_down) { SDL_Event mouse_event; mouse_event.type = SDL_MOUSEBUTTONDOWN; mouse_event.button.button = SDL_BUTTON_LEFT; mouse_event.button.state = SDL_PRESSED; mouse_event.button.x = x; mouse_event.button.y = y; SDL_PushEvent(&mouse_event); mouse_down = true; } } else { // LT 松开 if (mouse_down) { SDL_Event mouse_event; mouse_event.type = SDL_MOUSEBUTTONUP; mouse_event.button.button = SDL_BUTTON_LEFT; mouse_event.button.state = SDL_RELEASED; mouse_event.button.x = x; mouse_event.button.y = y; SDL_PushEvent(&mouse_event); mouse_down = false; } } } // 处理 RT 触发的鼠标事件 else if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT) { if (event.caxis.value > 0) { // RT 按下 if (!mouse_down) { SDL_Event mouse_event; mouse_event.type = SDL_MOUSEBUTTONDOWN; mouse_event.button.button = SDL_BUTTON_RIGHT; mouse_event.button.state = SDL_PRESSED; mouse_event.button.x = x; mouse_event.button.y = y; SDL_PushEvent(&mouse_event); mouse_down = true; } } else { // RT 松开 if (mouse_down) { SDL_Event mouse_event; mouse_event.type = SDL_MOUSEBUTTONUP; mouse_event.button.button = SDL_BUTTON_RIGHT; mouse_event.button.state = SDL_RELEASED; mouse_event.button.x = x; mouse_event.button.y = y; SDL_PushEvent(&mouse_event); mouse_down = false; } } } break; // 其他事件 case SDL_QUIT: quit = true; break; } } 把这个改为 LT模拟鼠标左键按下, RT模拟鼠标右键按下
- Assistant
你可以将LT模拟鼠标左键按下,RT模拟鼠标右键按下的代码修改为:
while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_CONTROLLERAXISMOTION: // 处理 LT 触发的鼠标事件 if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT && event.caxis.value > Input::DeadZone) { SDL_Event mouse_event; mouse_event.type = SDL_MOUSEBUTTONDOWN; mouse_event.button.button = SDL_BUTTON_LEFT; mouse_event.button.state = SDL_PRESSED; mouse_event.button.x = x; mouse_event.button.y = y; SDL_PushEvent(&mouse_event); } // 处理 RT 触发的鼠标事件 else if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT && event.caxis.value > Input::DeadZone) { SDL_Event mouse_event; mouse_event.type = SDL_MOUSEBUTTONDOWN; mouse_event.button.button = SDL_BUTTON_RIGHT; mouse_event.button.state = SDL_PRESSED; mouse_event.button.x = x; mouse_event.button.y = y; SDL_PushEvent(&mouse_event); } // 处理 LT/RT 松开事件 else if ((event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT && event.caxis.value <= Input::DeadZone) || (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT && event.caxis.value <= Input::DeadZone)) { SDL_Event mouse_event; mouse_event.type = SDL_MOUSEBUTTONUP; if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { mouse_event.button.button = SDL_BUTTON_LEFT; } else { mouse_event.button.button = SDL_BUTTON_RIGHT; } mouse_event.button.state = SDL_RELEASED; mouse_event.button.x = x; mouse_event.button.y = y; SDL_PushEvent(&mouse_event); } break; case SDL_QUIT: quit = true; break; } }