/// <summary> /// /// </summary> /// <param name="m"></param> protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_INPUT: { unsafe { uint dwSize = 0; uint receivedBytes; uint sizeof_RAWINPUTHEADER = (uint)sizeof(RAWINPUTHEADER); // Find out the size of the buffer we have to provide int res = GetRawInputData(m.LParam.ToPointer(), RID_INPUT, null, &dwSize, sizeof_RAWINPUTHEADER); if (res == 0) { // Allocate a buffer byte *lpb = stackalloc byte[(int)dwSize]; // Get the data receivedBytes = (uint)GetRawInputData((RAWINPUTHKEYBOARD *)m.LParam.ToPointer(), RID_INPUT, lpb, &dwSize, sizeof_RAWINPUTHEADER); if (receivedBytes == dwSize) { RAWINPUTHKEYBOARD *keybData = (RAWINPUTHKEYBOARD *)lpb; // Finally, analyze the data if (keybData->header.dwType == RIM_TYPEKEYBOARD) { if ((this.m_PrevControlKey != keybData->VKey) || (this.m_PrevMessage != keybData->Message)) { this.m_PrevControlKey = keybData->VKey; this.m_PrevMessage = keybData->Message; // Call the delegate in case data satisfies this.m_KeyHandler(keybData->VKey, keybData->Message); } } } else { string errMsg = string.Format("WndProc::GetRawInputData (2) received {0} bytes while expected {1} bytes", receivedBytes, dwSize); throw new Exception(errMsg); } } else { string errMsg = string.Format("WndProc::GetRawInputData (1) returned non zero value ({0})", res); throw new Exception(errMsg); } } } break; } base.WndProc(ref m); }
protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_INPUT: { unsafe { uint dwSize, receivedBytes; uint sizeof_RAWINPUTHEADER = (uint)(sizeof(RAWINPUTHEADER)); int res = GetRawInputData(m.LParam.ToPointer(), RID_INPUT, null, &dwSize, sizeof_RAWINPUTHEADER); if (res == 0) { byte *lpb = stackalloc byte[(int)dwSize]; receivedBytes = (uint)GetRawInputData((RAWINPUTHKEYBOARD *)(m.LParam.ToPointer()), RID_INPUT, lpb, &dwSize, sizeof_RAWINPUTHEADER); if (receivedBytes == dwSize) { RAWINPUTHKEYBOARD *keybData = (RAWINPUTHKEYBOARD *)lpb; if (keybData->header.dwType == RIM_TYPEKEYBOARD) { if ((m_previousControlKey != keybData->VKey) || (m_previousMessage != keybData->Message)) { m_previousControlKey = keybData->VKey; m_previousMessage = keybData->Message; m_KeyHandler(keybData->VKey, keybData->Message); } } } } } } break; } base.WndProc(ref m); }
protected override void WndProc(ref Message m) { if (m.Msg == WM_INPUT) { try { unsafe { uint dwSize, receivedBytes; uint sizeof_RAWINPUTHEADER = (uint)(sizeof(RAWINPUTHEADER)); // Find out the size of the buffer we have to provide int res = GetRawInputData(m.LParam.ToPointer(), RID_INPUT, null, &dwSize, sizeof_RAWINPUTHEADER); if (res == 0) { // Allocate a buffer and ... byte *lpb = stackalloc byte[(int)dwSize]; // ... get the data receivedBytes = (uint)GetRawInputData((RAWINPUTHKEYBOARD *)(m.LParam.ToPointer()), RID_INPUT, lpb, &dwSize, sizeof_RAWINPUTHEADER); if (receivedBytes == dwSize) { RAWINPUTHKEYBOARD *keybData = (RAWINPUTHKEYBOARD *)lpb; // Finally, analyze the data if (keybData->header.dwType == RIM_TYPEKEYBOARD) { /*if ((m_PrevControlKey != keybData->VKey) || * (m_PrevMessage != keybData->Message)) * {*/ m_PrevControlKey = keybData->VKey; m_PrevMessage = keybData->Message; if (keybData->Message == 257) { System.Windows.Forms.MessageBox.Show(keybData->VKey.ToString()); } // Call the delegate in case data satisfies m_KeyHandler(keybData->VKey, keybData->Message); // } } } else { string errMsg = string.Format("WndProc::GetRawInputData " + "(2) received {0} bytes while expected {1} bytes", receivedBytes, dwSize); throw new Exception(errMsg); } } else { string errMsg = string.Format("WndProc::GetRawInputData " + "(1) returned non zero value ({0})", res); throw new Exception(errMsg); } } } catch { throw; } } // In case you forget this you will run into problems base.WndProc(ref m); }