Skip to content

Commit

Permalink
fix Win11 null VKey polling
Browse files Browse the repository at this point in the history
  • Loading branch information
xan1242 committed Jan 15, 2023
1 parent 086d038 commit 4bba70d
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ struct CONTROLLER_STATE
// TODO: buffered raw input

RAWINPUTDEVICE Rid;
BYTE VKeyStates[2][255];
BYTE VKeyStates[2][256];
bool KeyboardState = true; // connection status
bool bAllowTwoPlayerKB = false; // allow for 2 players to use their own KB devices (nothing to do with gamepads)
unsigned int KeyboardReadingMode = 0; // 0 = buffered synchronous, 1 = unbuffered asynchronous, 2 = unbuffered raw
Expand Down Expand Up @@ -603,30 +603,30 @@ int Scanner_DigitalUpOrDown_RawKB(void* EventNode, unsigned int* unk1, unsigned
if ((inScannerConfig->JoyEvent == JOY_EVENT_THROTTLE) || (inScannerConfig->JoyEvent == JOY_EVENT_BRAKE) || (inScannerConfig->JoyEvent == JOY_EVENT_THROTTLE_ANALOG) || (inScannerConfig->JoyEvent == JOY_EVENT_THROTTLE_ANALOG_ALTERNATE) || (inScannerConfig->JoyEvent == JOY_EVENT_BRAKE_ANALOG) || (inScannerConfig->JoyEvent == JOY_EVENT_BRAKE_ANALOG_ALTERNATE))
{

if (VKeyStates[ci][inScannerConfig->keycode])
if (inScannerConfig->keycode && VKeyStates[ci][inScannerConfig->keycode])
{
EventStatesKB[ci][inScannerConfig->JoyEvent] = VKeyStates[ci][inScannerConfig->keycode];
return inScannerConfig->JoyEvent + (inScannerConfig->param << 8);
}

if (!VKeyStates[ci][inScannerConfig->keycode])
if (inScannerConfig->keycode && !VKeyStates[ci][inScannerConfig->keycode])
{
EventStatesKB[ci][inScannerConfig->JoyEvent] = VKeyStates[ci][inScannerConfig->keycode];
return inScannerConfig->JoyEvent;
}
}

// we're using BitmaskStuff to define which button is actually pressed
if ((VKeyStates[ci][inScannerConfig->keycode]) != EventStatesKB[ci][inScannerConfig->JoyEvent])
if (inScannerConfig->keycode && ((VKeyStates[ci][inScannerConfig->keycode]) != EventStatesKB[ci][inScannerConfig->JoyEvent]))
{
// on change to TRUE -- return 0xFF + JoyEvent number (normally this is a number defined in scannerconfig)
if (VKeyStates[ci][inScannerConfig->keycode])
if (inScannerConfig->keycode && VKeyStates[ci][inScannerConfig->keycode])
{
EventStatesKB[ci][inScannerConfig->JoyEvent] = VKeyStates[ci][inScannerConfig->keycode];
return inScannerConfig->JoyEvent + (inScannerConfig->param << 8);
}
// on change to FALSE -- return JoyEvent number
if (!VKeyStates[ci][inScannerConfig->keycode])
if (inScannerConfig->keycode && !VKeyStates[ci][inScannerConfig->keycode])
{
EventStatesKB[ci][inScannerConfig->JoyEvent] = VKeyStates[ci][inScannerConfig->keycode];
return inScannerConfig->JoyEvent;
Expand All @@ -639,7 +639,10 @@ int Scanner_DigitalUpOrDown_RawKB(void* EventNode, unsigned int* unk1, unsigned

int Scanner_DigitalUpOrDown_SyncKB(void* EventNode, unsigned int* unk1, unsigned int unk2, ScannerConfig* inScannerConfig, void* Joystick)
{
bool bKeyState = VKeyStates[0][inScannerConfig->keycode] >> 7;
bool bKeyState = false;

if (inScannerConfig->keycode)
bKeyState = VKeyStates[0][inScannerConfig->keycode] >> 7;

// throttle is very time sensitive, so we return all the time
if ((inScannerConfig->JoyEvent == JOY_EVENT_THROTTLE) || (inScannerConfig->JoyEvent == JOY_EVENT_BRAKE) || (inScannerConfig->JoyEvent == JOY_EVENT_THROTTLE_ANALOG) || (inScannerConfig->JoyEvent == JOY_EVENT_THROTTLE_ANALOG_ALTERNATE) || (inScannerConfig->JoyEvent == JOY_EVENT_BRAKE_ANALOG) || (inScannerConfig->JoyEvent == JOY_EVENT_BRAKE_ANALOG_ALTERNATE))
Expand Down Expand Up @@ -680,7 +683,10 @@ int Scanner_DigitalUpOrDown_SyncKB(void* EventNode, unsigned int* unk1, unsigned

int Scanner_DigitalUpOrDown_AsyncKB(void* EventNode, unsigned int* unk1, unsigned int unk2, ScannerConfig* inScannerConfig, void* Joystick)
{
bool bKeyState = GetAsyncKeyState(inScannerConfig->keycode) >> 15;
bool bKeyState = false;

if (inScannerConfig->keycode)
bKeyState = GetAsyncKeyState(inScannerConfig->keycode) >> 15;

// throttle/brake is very time sensitive, so we return all the time
if ((inScannerConfig->JoyEvent == JOY_EVENT_THROTTLE) || (inScannerConfig->JoyEvent == JOY_EVENT_BRAKE) || (inScannerConfig->JoyEvent == JOY_EVENT_THROTTLE_ANALOG) || (inScannerConfig->JoyEvent == JOY_EVENT_THROTTLE_ANALOG_ALTERNATE) || (inScannerConfig->JoyEvent == JOY_EVENT_BRAKE_ANALOG) || (inScannerConfig->JoyEvent == JOY_EVENT_BRAKE_ANALOG_ALTERNATE))
Expand Down Expand Up @@ -770,7 +776,7 @@ int Scanner_DigitalDown_RawKB(void* EventNode, unsigned int* unk1, unsigned int
ci = 1;

// we're using BitmaskStuff to define which button is actually pressed
if ((VKeyStates[ci][inScannerConfig->keycode]) != EventStatesKB[ci][inScannerConfig->JoyEvent])
if (inScannerConfig->keycode && ((VKeyStates[ci][inScannerConfig->keycode]) != EventStatesKB[ci][inScannerConfig->JoyEvent]))
{
EventStatesKB[ci][inScannerConfig->JoyEvent] = (VKeyStates[ci][inScannerConfig->keycode]);
// on change to TRUE -- return event
Expand All @@ -786,7 +792,10 @@ int Scanner_DigitalDown_RawKB(void* EventNode, unsigned int* unk1, unsigned int

int Scanner_DigitalDown_SyncKB(void* EventNode, unsigned int* unk1, unsigned int unk2, ScannerConfig* inScannerConfig, void* Joystick)
{
bool bKeyState = VKeyStates[0][inScannerConfig->keycode] >> 7;
bool bKeyState = false;

if (inScannerConfig->keycode)
bKeyState = VKeyStates[0][inScannerConfig->keycode] >> 7;

// we're using BitmaskStuff to define which button is actually pressed
if (bKeyState != EventStatesKB[0][inScannerConfig->JoyEvent])
Expand All @@ -805,7 +814,10 @@ int Scanner_DigitalDown_SyncKB(void* EventNode, unsigned int* unk1, unsigned int

int Scanner_DigitalDown_AsyncKB(void* EventNode, unsigned int* unk1, unsigned int unk2, ScannerConfig* inScannerConfig, void* Joystick)
{
bool bKeyState = GetAsyncKeyState(inScannerConfig->keycode) >> 15;
bool bKeyState = false;

if (inScannerConfig->keycode)
bKeyState = GetAsyncKeyState(inScannerConfig->keycode) >> 15;

// we're using BitmaskStuff to define which button is actually pressed
if (bKeyState != EventStatesKB[0][inScannerConfig->JoyEvent])
Expand Down Expand Up @@ -1328,22 +1340,22 @@ void RealDriver_JoyHandler_Steer(int JoystickPort, int JoyEvent, char Value, voi
switch (KeyboardReadingMode)
{
case KB_READINGMODE_UNBUFFERED_RAW:
if (VKeyStates[JoystickPort][SteerLeftVKey])
if (SteerLeftVKey && VKeyStates[JoystickPort][SteerLeftVKey])
DigitalSteerValueKB = DigitalSteerValueKB + 1.0;
if (VKeyStates[JoystickPort][SteerRightVKey])
if (SteerRightVKey && VKeyStates[JoystickPort][SteerRightVKey])
DigitalSteerValueKB = DigitalSteerValueKB - 1.0;
break;
case KB_READINGMODE_BUFFERED:
if (VKeyStates[0][SteerLeftVKey] >> 7)
if (SteerLeftVKey && VKeyStates[0][SteerLeftVKey] >> 7)
DigitalSteerValueKB = DigitalSteerValueKB + 1.0;
if (VKeyStates[0][SteerRightVKey] >> 7)
if (SteerRightVKey && VKeyStates[0][SteerRightVKey] >> 7)
DigitalSteerValueKB = DigitalSteerValueKB - 1.0;
break;
case KB_READINGMODE_UNBUFFERED_ASYNC:
default:
if (GetAsyncKeyState(SteerLeftVKey) >> 15)
if (SteerLeftVKey && GetAsyncKeyState(SteerLeftVKey) >> 15)
DigitalSteerValueKB = DigitalSteerValueKB + 1.0;
if (GetAsyncKeyState(SteerRightVKey) >> 15)
if (SteerRightVKey && GetAsyncKeyState(SteerRightVKey) >> 15)
DigitalSteerValueKB = DigitalSteerValueKB - 1.0;
break;
}
Expand Down Expand Up @@ -1628,7 +1640,7 @@ int KB_GetCurrentPressedKey()

for (int i = 0; i < 255; i++)
{
if (VKeyStates[0][i] >> 7)
if (i && VKeyStates[0][i] >> 7)
if (!bIgnorableVKey(i)) // TODO: separate Left Control and Right Alt
return i;
}
Expand All @@ -1638,7 +1650,7 @@ int KB_GetCurrentPressedKey()

for (int i = 0; i < 255; i++)
{
if (GetAsyncKeyState(i) >> 15)
if (i && GetAsyncKeyState(i) >> 15)
if (!bIgnorableVKey(i))
return i;
}
Expand Down

0 comments on commit 4bba70d

Please sign in to comment.