I am making a mapper program for the 360 controller using XInput I am using SendInput to send keypresses to games

Code:
if(controller1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B)
{
			
				
	memset(&ip,0,sizeof(INPUT));//
              UINT_PTR code = controller1->profile->Buttons[21]->keycode;
	ip.type = INPUT_KEYBOARD;
	ip.ki.wScan = MapVirtualKey(code,0);
	ip.ki.time = 0;
	ip.ki.dwExtraInfo = GetMessageExtraInfo();
	ip.ki.wVk = 0;
	ip.ki.dwFlags = KEYEVENTF_SCANCODE ;
				
              if(controller1->profile->Buttons[21]->delayTimer <= 0)
	SendInput(1, &ip, sizeof(INPUT));

	++controller1->profile->Buttons[21]->delayTimer;

	if(controller1->profile->Buttons[21]->delayTimer >= 300)
	{

	++controller1->profile->Buttons[21]->repeatTimer;

	if(controller1->profile->Buttons[21]->repeatTimer >= 100)
	{

						
	SendInput(1, &ip, sizeof(INPUT));
	 controller1->profile->Buttons[21]->repeatTimer = 0;
				

	}
				
	}
				
	}
	else 
	{
				
	ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP ;
	SendInput(1, &ip, sizeof(INPUT));
	controller1->profile->Buttons[21]->delayTimer = 0;
	controller1->profile->Buttons[21]->repeatTimer = 0;
	
	}
it sends the key up event continuously wich is not what I want I want to make it so when the button is up it only send the key up event once. I figured I would do this using a current controller state and a previous controller state using an XOR but I have failed every attempt. Any advice would be helpful considering this is the only thing holding me back from finishing.

P.S the code for checking the buttons state is in the main message loop.