Release all buttons and axes on DInput device disconnection

This commit is contained in:
Henrik Rydgård
2026-01-25 16:26:05 +01:00
parent 0543b34a27
commit 9c582e4d45
2 changed files with 44 additions and 0 deletions
+43
View File
@@ -174,6 +174,7 @@ DinputDevice::DinputDevice(int devnum) {
DinputDevice::~DinputDevice() {
KeyMap::NotifyPadDisconnected(DEVICE_ID_PAD_0 + pDevNum);
ReleaseAllKeys();
if (pJoystick) {
pJoystick = nullptr;
@@ -190,6 +191,48 @@ DinputDevice::~DinputDevice() {
}
}
void DinputDevice::ReleaseAllKeys() {
KeyInput key;
key.deviceId = DEVICE_ID_PAD_0 + pDevNum;
key.flags = KeyInputFlags::UP;
for (int i = 0; i < ARRAY_SIZE(dinput_buttons); ++i) {
if (lastButtons_[i] != 0) {
key.keyCode = dinput_buttons[i];
NativeKey(key);
lastButtons_[i] = 0;
}
}
// Release DPad
static const InputKeyCode dpadCodes[] = {
NKCODE_DPAD_UP,
NKCODE_DPAD_DOWN,
NKCODE_DPAD_LEFT,
NKCODE_DPAD_RIGHT
};
for (int i = 0; i < ARRAY_SIZE(dpadCodes); ++i) {
key.keyCode = dpadCodes[i];
NativeKey(key);
}
// Release axes
static const InputAxis axes[] = {
JOYSTICK_AXIS_X,
JOYSTICK_AXIS_Y,
JOYSTICK_AXIS_Z,
JOYSTICK_AXIS_RX,
JOYSTICK_AXIS_RY,
JOYSTICK_AXIS_RZ
};
for (int i = 0; i < ARRAY_SIZE(axes); ++i) {
AxisInput axis;
axis.deviceId = DEVICE_ID_PAD_0 + pDevNum;
axis.axisId = axes[i];
axis.value = 0.0f;
NativeAxis(&axis, 1);
}
}
void SendNativeAxis(InputDeviceID deviceId, int value, int &lastValue, InputAxis axisId) {
if (value != lastValue) {
AxisInput axis;
+1
View File
@@ -45,6 +45,7 @@ public:
}
private:
void ReleaseAllKeys();
void ApplyButtons(DIJOYSTATE2 &state);
//unfortunate and unclean way to keep only one DirectInput instance around
static LPDIRECTINPUT8 getPDI();