static unsafe void CopyBuffer(byte* windowsKeyData, ref MyKeyboardBuffer buffer) { for (int i = 0; i < 256; i++) { if ((windowsKeyData[i] & 0x80) != 0) { buffer.SetBit((byte)i, true); } } }
public static MyKeyboardState GetCurrentState() { MyKeyboardBuffer buffer = new MyKeyboardBuffer(); unsafe { byte* keyData = stackalloc byte[256]; if (!GetKeyboardState(keyData)) { throw new InvalidOperationException("Could not read keyboard"); } CopyBuffer(keyData, ref buffer); } return MyKeyboardState.FromBuffer(buffer); }