I have added a couple of lines and a label to your code.
Leftmost bit (bit 7) in KeyState seems to indicate if key is pressed.
You can try your own app- when key is pressed indicator on the keyboard and indicator on the app mismatch in one of the direction.
When application itself is working- everything is ok, but when ScrollLock is toggled outside it (another app is focused)- bit 7 starts to make troubles.
I will try to ignore bit 7.