Holding two keyboard buttons at ones

I'm using arduino uno in order to experiment with two keys keyboard.

Say that in order to write letter A I use:
Serial.write(00400000,8);

And for B I use
Serial.write(00500000,8);

I wrote a code that allows me to to use both keys and even hold them and everything works fine, the problem is that whenever I hold both of them, it goes crazy and instead of holding both keys it presses both of them really fast. I assume that it is because the loop always repeats those keys back and fourth. I tried to only Serial.write one time and release the button only when the keypress is over, but it is considered as one press. What can I do?

In case that you hate yourself, heres the code:>

//Array that holds 8 bits that needs to be sent
uint8_t buf[8] = {0};
//Array that holds inofrmation about which keys are pressed (true == pressed)
bool keyPressed[2];
#define Pin_A 13
#define Pin_B 12

void setup(){
Serial.begin(9600);
pinMode(Pin_A, INPUT);
pinMode(Pin_B, INPUT);
keyPressed[0] = false;
keyPressed[1] = false;
}

void loop() {

// A key

if(keyPressed[0] == false)
{
if(digitalRead(Pin_A) == LOW)
{
buf[2] = 4;
keyPressed[0] = true;
Serial.write(buf,8);
}

else
{
// do nothing
}

}
else
{
if(digitalRead(Pin_A) == HIGH)
{
keyRelease(0);
}
else{
buf[2] = 4;
Serial.write(buf,8);;
}
}

//B key

if(keyPressed[1] == false)
{
if(digitalRead(Pin_B) == LOW)
{
buf[2] = 5;
keyPressed[1] = true;
Serial.write(buf,8);
}

else
{
// do nothing
}

}
else
{
if(digitalRead(Pin_B) == HIGH)
{
keyRelease(1);
}
else{
buf[2] = 5;
Serial.write(buf,8);;
}
}

}
//release key numbered x in keypressed array
void keyRelease(int x){
keyPressed = false;
buf[0] = 0;
buf[2] = 0;
Serial.write(buf,8);
}

Can we have the code in code tags, please, so that we can read it?

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

It SOUNDS a simple circuit, but please provide a schematic anyway.

and links to the exact parts you are using.

Now the CONDITION you are representing is NOT simple. With a single button A you have 4 possibilities as follows
now was
0 0 button not pressed
1 0 button has been pressed
1 1 button still pressed
0 1 button was released

You can test as you are doing in your code. Has it changed? if yes, is it a press or release?

With two buttons its more complicated, as there are more possibilities, depending on the sequence in which buttons were pressed.

You can represent the states in a truth table as above, a timing diagram, or (most usefully) a "state daigram"

once you have done this you will be able to decide what action you want to be produced for each of the states.

I have basically found a better way to understand which buttons are being pressed and if they are being pressed together using bitwise manipulations. But I still cannot understand what bit I need to send in the Serial.write if two keys are pressed together

Serial.print vs Serial.write Arduino What's the difference? - Chip Piko Is Free Arduino ESP8266 NodeMCU Course, Tutorial and Tech Guide..

I can't see how print helps me to hold two buttons at once.
I also need to use keycodes and not ascii so that the keyboard can still work if I change to lowercase letters

I think that what I'm looking for is actually a way to include to bytes inside one packet.