You are overcomplicating this. Keyboards shouldn't have any logic apart from scanning and sending key press and release events to the OS.
I press modifier on keyboard A, keyboard A sends key press event to the OS.
I press key on keyboard B, keyboard B sends key press event to the OS.
I release key on keyboard B, keyboard B sends key release event to the OS.
I release modifier on keyboard A, keyboard A sends key release event to the OS.
From the sequence of events, the OS knows that a key was pressed with the modifier and can act accordingly e.g. emit an event downstream that corresponds to the key that should be generated when pressing a modifier.
Scanning the keyboard matrix is no concern for the OS.
I press modifier on keyboard A, keyboard A sends key press event to the OS. I press key on keyboard B, keyboard B sends key press event to the OS. I release key on keyboard B, keyboard B sends key release event to the OS. I release modifier on keyboard A, keyboard A sends key release event to the OS.
From the sequence of events, the OS knows that a key was pressed with the modifier and can act accordingly e.g. emit an event downstream that corresponds to the key that should be generated when pressing a modifier.
Scanning the keyboard matrix is no concern for the OS.