public bool ProcessKeyboardEvent(RawInput rin) { bool flag1 = false; bool flag2 = rin.Data.Keyboard.Message == 256 || rin.Data.Keyboard.Message == 260; ContextHandle key = new ContextHandle(rin.Header.Device); if (!this.rawids.ContainsKey(key)) { this.RefreshDevices(); } if (this.keyboards.Count == 0) { return(false); } int index = this.rawids.ContainsKey(key) ? this.rawids[key] : 0; KeyboardState keyboardState = this.keyboards[index]; switch (rin.Data.Keyboard.VKey) { case VirtualKeys.SHIFT: keyboardState[Key.ShiftLeft] = keyboardState[Key.ShiftRight] = flag2; flag1 = true; break; case VirtualKeys.CONTROL: keyboardState[Key.ControlLeft] = keyboardState[Key.ControlRight] = flag2; flag1 = true; break; case VirtualKeys.MENU: keyboardState[Key.AltLeft] = keyboardState[Key.AltRight] = flag2; flag1 = true; break; default: if (WinRawKeyboard.KeyMap.ContainsKey(rin.Data.Keyboard.VKey)) { keyboardState[WinRawKeyboard.KeyMap[rin.Data.Keyboard.VKey]] = flag2; flag1 = true; break; } else { break; } } lock (this.UpdateLock) { this.keyboards[index] = keyboardState; return(flag1); } }
public bool ProcessKeyboardEvent(RawInput rin) { bool processed = false; bool pressed = rin.Data.Keyboard.Message == (int)WindowMessage.KEYDOWN || rin.Data.Keyboard.Message == (int)WindowMessage.SYSKEYDOWN; var scancode = rin.Data.Keyboard.MakeCode; var vkey = rin.Data.Keyboard.VKey; bool extended0 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E0) != 0; bool extended1 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E1) != 0; bool is_valid = true; ContextHandle handle = new ContextHandle(rin.Header.Device); KeyboardState keyboard; if (!rawids.ContainsKey(handle)) { RefreshDevices(); } if (keyboards.Count == 0) { return(false); } // Note:For some reason, my Microsoft Digital 3000 keyboard reports 0 // as rin.Header.Device for the "zoom-in/zoom-out" buttons. // That's problematic, because no device has a "0" id. // As a workaround, we'll add those buttons to the first device (if any). int keyboard_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0; keyboard = keyboards[keyboard_handle]; Key key = KeyMap.TranslateKey(scancode, vkey, extended0, extended1, out is_valid); if (is_valid) { keyboard.SetKeyState(key, (byte)scancode, pressed); processed = true; } lock (UpdateLock) { keyboards[keyboard_handle] = keyboard; return(processed); } }
public bool ProcessMouseEvent(RawInput rin) { RawMouse rawMouse = rin.Data.Mouse; ContextHandle key = new ContextHandle(rin.Header.Device); if (!this.rawids.ContainsKey(key)) this.RefreshDevices(); if (this.mice.Count == 0) return false; int index = this.rawids.ContainsKey(key) ? this.rawids[key] : 0; MouseState mouseState = this.mice[index]; if ((rawMouse.ButtonFlags & RawInputMouseState.LEFT_BUTTON_DOWN) != (RawInputMouseState) 0) mouseState.EnableBit(0); if ((rawMouse.ButtonFlags & RawInputMouseState.LEFT_BUTTON_UP) != (RawInputMouseState) 0) mouseState.DisableBit(0); if ((rawMouse.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_DOWN) != (RawInputMouseState) 0) mouseState.EnableBit(2); if ((rawMouse.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_UP) != (RawInputMouseState) 0) mouseState.DisableBit(2); if ((rawMouse.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_DOWN) != (RawInputMouseState) 0) mouseState.EnableBit(1); if ((rawMouse.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_UP) != (RawInputMouseState) 0) mouseState.DisableBit(1); if ((rawMouse.ButtonFlags & RawInputMouseState.BUTTON_4_DOWN) != (RawInputMouseState) 0) mouseState.EnableBit(3); if ((rawMouse.ButtonFlags & RawInputMouseState.BUTTON_4_UP) != (RawInputMouseState) 0) mouseState.DisableBit(3); if ((rawMouse.ButtonFlags & RawInputMouseState.BUTTON_5_DOWN) != (RawInputMouseState) 0) mouseState.EnableBit(4); if ((rawMouse.ButtonFlags & RawInputMouseState.BUTTON_5_UP) != (RawInputMouseState) 0) mouseState.DisableBit(4); if ((rawMouse.ButtonFlags & RawInputMouseState.WHEEL) != (RawInputMouseState) 0) mouseState.WheelPrecise += (float) (short) rawMouse.ButtonData / 120f; if ((rawMouse.Flags & RawMouseFlags.MOUSE_MOVE_ABSOLUTE) != RawMouseFlags.MOUSE_MOVE_RELATIVE) { mouseState.X = rawMouse.LastX; mouseState.Y = rawMouse.LastY; } else { mouseState.X += rawMouse.LastX; mouseState.Y += rawMouse.LastY; } lock (this.UpdateLock) { this.mice[index] = mouseState; return true; } }
public bool ProcessKeyboardEvent(RawInput rin) { bool flag1 = false; bool flag2 = rin.Data.Keyboard.Message == 256 || rin.Data.Keyboard.Message == 260; ContextHandle key = new ContextHandle(rin.Header.Device); if (!this.rawids.ContainsKey(key)) this.RefreshDevices(); if (this.keyboards.Count == 0) return false; int index = this.rawids.ContainsKey(key) ? this.rawids[key] : 0; KeyboardState keyboardState = this.keyboards[index]; switch (rin.Data.Keyboard.VKey) { case VirtualKeys.SHIFT: keyboardState[Key.ShiftLeft] = keyboardState[Key.ShiftRight] = flag2; flag1 = true; break; case VirtualKeys.CONTROL: keyboardState[Key.ControlLeft] = keyboardState[Key.ControlRight] = flag2; flag1 = true; break; case VirtualKeys.MENU: keyboardState[Key.AltLeft] = keyboardState[Key.AltRight] = flag2; flag1 = true; break; default: if (WinRawKeyboard.KeyMap.ContainsKey(rin.Data.Keyboard.VKey)) { keyboardState[WinRawKeyboard.KeyMap[rin.Data.Keyboard.VKey]] = flag2; flag1 = true; break; } else break; } lock (this.UpdateLock) { this.keyboards[index] = keyboardState; return flag1; } }
public void Poll() { return; #if false // We will do a buffered read for all input devices and route the RawInput structures // to the correct 'ProcessData' handlers. First, we need to find out the size of the // buffer to allocate for the structures. Then we allocate the buffer and read the // structures, calling the correct handler for each one. Last, we free the allocated // buffer. int size = 0; Functions.GetRawInputBuffer(IntPtr.Zero, ref size, API.RawInputHeaderSize); size *= 256; IntPtr rin_data = Marshal.AllocHGlobal(size); while (true) { // Iterate reading all available RawInput structures and routing them to their respective // handlers. int num = Functions.GetRawInputBuffer(rin_data, ref size, API.RawInputHeaderSize); if (num == 0) break; else if (num < 0) { /*int error = Marshal.GetLastWin32Error(); if (error == 122) { // Enlarge the buffer, it was too small. AllocateBuffer(); } else { throw new ApplicationException(String.Format( "GetRawInputBuffer failed with code: {0}", error)); }*/ Debug.Print("GetRawInputBuffer failed with code: {0}", Marshal.GetLastWin32Error()); //AllocateBuffer(); break; } RawInput[] rin_structs = new RawInput[num]; IntPtr next_rin = rin_data; for (int i = 0; i < num; i++) { rin_structs[i] = (RawInput)Marshal.PtrToStructure(next_rin, typeof(RawInput)); switch (rin_structs[i].Header.Type) { case RawInputDeviceType.KEYBOARD: keyboardDriver.ProcessKeyboardEvent(rin_structs[i]); break; case RawInputDeviceType.MOUSE: mouseDriver.ProcessEvent(rin_structs[i]); break; } next_rin = Functions.NextRawInputStructure(next_rin); } Functions.DefRawInputProc(rin_structs, num, (uint)API.RawInputHeaderSize); } Marshal.FreeHGlobal(rin_data); #endif }
unsafe void UpdateAxes(RawInput* rin, Device stick) { for (int i = 0; i < stick.AxisCaps.Count; i++) { if (stick.AxisCaps[i].IsRange) { Debug.Print("[{0}] Axis range collections not implemented. Please report your controller type at http://www.opentk.com", GetType().Name); continue; } HIDPage page = stick.AxisCaps[i].UsagePage; short usage = stick.AxisCaps[i].NotRange.Usage; uint value = 0; short collection = stick.AxisCaps[i].LinkCollection; HidProtocolStatus status = HidProtocol.GetUsageValue( HidProtocolReportType.Input, page, 0, usage, ref value, PreparsedData, new IntPtr((void*)&rin->Data.HID.RawData), rin->Data.HID.Size); if (status != HidProtocolStatus.Success) { Debug.Print("[{0}] HidProtocol.GetScaledUsageValue() failed. Error: {1}", GetType().Name, status); continue; } if (page == HIDPage.GenericDesktop && (HIDUsageGD)usage == HIDUsageGD.Hatswitch) { stick.SetHat(collection, page, usage, GetHatPosition(value, stick.AxisCaps[i])); } else { short scaled_value = (short)HidHelper.ScaleValue( (int)((long)value + stick.AxisCaps[i].LogicalMin), stick.AxisCaps[i].LogicalMin, stick.AxisCaps[i].LogicalMax, Int16.MinValue, Int16.MaxValue); stick.SetAxis(collection, page, usage, scaled_value); } } }
unsafe internal static extern INT GetRawInputData( HRAWINPUT RawInput, GetRawInputDataEnum Command, RawInput* Data, [In, Out] ref INT Size, INT SizeHeader );
internal static extern LRESULT DefRawInputProc(RawInput[] RawInput, INT Input, UINT SizeHeader);
public bool ProcessMouseEvent(RawInput rin) { RawMouse raw = rin.Data.Mouse; ContextHandle handle = new ContextHandle(rin.Header.Device); MouseState mouse; if (!rawids.ContainsKey(handle)) { RefreshDevices(); } if (mice.Count == 0) { return(false); } // Note:For some reason, my Microsoft Digital 3000 keyboard reports 0 // as rin.Header.Device for the "zoom-in/zoom-out" buttons. // That's problematic, because no device has a "0" id. // As a workaround, we'll add those buttons to the first device (if any). int mouse_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0; mouse = mice[mouse_handle]; // Set and release capture of the mouse to fix http://www.opentk.com/node/2133, Patch by Artfunkel if ((raw.ButtonFlags & RawInputMouseState.LEFT_BUTTON_DOWN) != 0) { mouse.EnableBit((int)MouseButton.Left); Functions.SetCapture(Window); } if ((raw.ButtonFlags & RawInputMouseState.LEFT_BUTTON_UP) != 0) { mouse.DisableBit((int)MouseButton.Left); Functions.ReleaseCapture(); } if ((raw.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_DOWN) != 0) { mouse.EnableBit((int)MouseButton.Right); Functions.SetCapture(Window); } if ((raw.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_UP) != 0) { mouse.DisableBit((int)MouseButton.Right); Functions.ReleaseCapture(); } if ((raw.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_DOWN) != 0) { mouse.EnableBit((int)MouseButton.Middle); Functions.SetCapture(Window); } if ((raw.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_UP) != 0) { mouse.DisableBit((int)MouseButton.Middle); Functions.ReleaseCapture(); } if ((raw.ButtonFlags & RawInputMouseState.BUTTON_4_DOWN) != 0) { mouse.EnableBit((int)MouseButton.Button1); Functions.SetCapture(Window); } if ((raw.ButtonFlags & RawInputMouseState.BUTTON_4_UP) != 0) { mouse.DisableBit((int)MouseButton.Button1); Functions.ReleaseCapture(); } if ((raw.ButtonFlags & RawInputMouseState.BUTTON_5_DOWN) != 0) { mouse.EnableBit((int)MouseButton.Button2); Functions.SetCapture(Window); } if ((raw.ButtonFlags & RawInputMouseState.BUTTON_5_UP) != 0) { mouse.DisableBit((int)MouseButton.Button2); Functions.ReleaseCapture(); } if ((raw.ButtonFlags & RawInputMouseState.WHEEL) != 0) { mouse.SetScrollRelative(0, unchecked ((short)raw.ButtonData) / 120.0f); } if ((raw.ButtonFlags & RawInputMouseState.HWHEEL) != 0) { mouse.SetScrollRelative(unchecked ((short)raw.ButtonData) / 120.0f, 0); } if ((raw.Flags & RawMouseFlags.MOUSE_MOVE_ABSOLUTE) != 0) { mouse.X = raw.LastX; mouse.Y = raw.LastY; } else { // Seems like MOUSE_MOVE_RELATIVE is the default, unless otherwise noted. mouse.X += raw.LastX; mouse.Y += raw.LastY; } lock (UpdateLock) { mice[mouse_handle] = mouse; return(true); } }
/// <summary> /// Processes raw input events. /// </summary> /// <param name="rin"></param> /// <returns></returns> internal bool ProcessEvent(RawInput rin) { //MouseDevice mouse = mice.Find(delegate(MouseDevice m) //{ // return m.DeviceID == rin.Header.Device; //}); MouseDevice mouse; if (mice.Count > 0) mouse = mice[0]; else return false; switch (rin.Header.Type) { case RawInputDeviceType.MOUSE: if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.LEFT_BUTTON_DOWN) != 0) mouse[MouseButton.Left] = true; if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.LEFT_BUTTON_UP) != 0) mouse[MouseButton.Left] = false; if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_DOWN) != 0) mouse[MouseButton.Right] = true; if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_UP) != 0) mouse[MouseButton.Right] = false; if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_DOWN) != 0) mouse[MouseButton.Middle] = true; if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_UP) != 0) mouse[MouseButton.Middle] = false; if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.BUTTON_4_DOWN) != 0) mouse[MouseButton.Button1] = true; if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.BUTTON_4_UP) != 0) mouse[MouseButton.Button1] = false; if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.BUTTON_5_DOWN) != 0) mouse[MouseButton.Button2] = true; if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.BUTTON_5_UP) != 0) mouse[MouseButton.Button2] = false; if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.WHEEL) != 0) mouse.Wheel += (short)rin.Data.Mouse.ButtonData / 120; if ((rin.Data.Mouse.Flags & RawMouseFlags.MOUSE_MOVE_ABSOLUTE) != 0) { mouse.Position = new Point(rin.Data.Mouse.LastX, rin.Data.Mouse.LastY); } else { // Seems like MOUSE_MOVE_RELATIVE is the default, unless otherwise noted. mouse.Position = new Point(mouse.X + rin.Data.Mouse.LastX, mouse.Y + rin.Data.Mouse.LastY); } if ((rin.Data.Mouse.Flags & RawMouseFlags.MOUSE_VIRTUAL_DESKTOP) != 0) Debug.WriteLine(String.Format("Mouse {0} defines MOUSE_VIRTUAL_DESKTOP flag, please report at http://www.opentk.com", mouse.ToString())); return true; default: throw new ApplicationException("WinRawMouse driver received invalid data."); } }
internal static int GetRawInputData(IntPtr RawInput, GetRawInputDataEnum Command, out RawInput Data, [In, Out] ref int Size, int SizeHeader);
internal static IntPtr DefRawInputProc(ref RawInput RawInput, int Input, uint SizeHeader);
internal static int GetRawInputData(IntPtr raw, out RawInput data) { int size = RawInput.SizeInBytes; unsafe { fixed (RawInput* pdata = &data) { GetRawInputData(raw, GetRawInputDataEnum.INPUT, (IntPtr)pdata, ref size, API.RawInputHeaderSize); } } return size; }
public bool ProcessKeyboardEvent(RawInput rin) { bool processed = false; bool pressed = rin.Data.Keyboard.Message == (int)WindowMessage.KEYDOWN || rin.Data.Keyboard.Message == (int)WindowMessage.SYSKEYDOWN; ContextHandle handle = new ContextHandle(rin.Header.Device); KeyboardState keyboard; if (!rawids.ContainsKey(handle)) { RefreshDevices(); } if (keyboards.Count == 0) { return(false); } // Note:For some reason, my Microsoft Digital 3000 keyboard reports 0 // as rin.Header.Device for the "zoom-in/zoom-out" buttons. // That's problematic, because no device has a "0" id. // As a workaround, we'll add those buttons to the first device (if any). int keyboard_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0; keyboard = keyboards[keyboard_handle]; // Generic control, shift, alt keys may be sent instead of left/right. // It seems you have to explicitly register left/right events. switch (rin.Data.Keyboard.VKey) { case VirtualKeys.SHIFT: keyboard[Input.Key.ShiftLeft] = keyboard[Input.Key.ShiftRight] = pressed; processed = true; break; case VirtualKeys.CONTROL: keyboard[Input.Key.ControlLeft] = keyboard[Input.Key.ControlRight] = pressed; processed = true; break; case VirtualKeys.MENU: keyboard[Input.Key.AltLeft] = keyboard[Input.Key.AltRight] = pressed; processed = true; break; default: if (!KeyMap.ContainsKey(rin.Data.Keyboard.VKey)) { Debug.Print("Virtual key {0} ({1}) not mapped.", rin.Data.Keyboard.VKey, (int)rin.Data.Keyboard.VKey); } else { keyboard[KeyMap[rin.Data.Keyboard.VKey]] = pressed; processed = true; } break; } lock (UpdateLock) { keyboards[keyboard_handle] = keyboard; return(processed); } }
unsafe void UpdateButtons(RawInput* rin, Device stick) { stick.ClearButtons(); for (int i = 0; i < stick.ButtonCaps.Count; i++) { short* usage_list = stackalloc short[(int)JoystickButton.Last + 1]; int usage_length = (int)JoystickButton.Last; HIDPage page = stick.ButtonCaps[i].UsagePage; short collection = stick.ButtonCaps[i].LinkCollection; HidProtocolStatus status = HidProtocol.GetUsages( HidProtocolReportType.Input, page, 0, usage_list, ref usage_length, PreparsedData, new IntPtr((void*)&rin->Data.HID.RawData), rin->Data.HID.Size); if (status != HidProtocolStatus.Success) { Debug.Print("[WinRawJoystick] HidProtocol.GetUsages() failed with {0}", Marshal.GetLastWin32Error()); continue; } for (int j = 0; j < usage_length; j++) { short usage = *(usage_list + j); stick.SetButton(collection, page, usage, true); } } }
public bool ProcessMouseEvent(RawInput rin) { RawMouse raw = rin.Data.Mouse; ContextHandle handle = new ContextHandle(rin.Header.Device); MouseState mouse; if (!rawids.ContainsKey(handle)) { RefreshDevices(); } if (mice.Count == 0) return false; // Note:For some reason, my Microsoft Digital 3000 keyboard reports 0 // as rin.Header.Device for the "zoom-in/zoom-out" buttons. // That's problematic, because no device has a "0" id. // As a workaround, we'll add those buttons to the first device (if any). int mouse_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0; mouse = mice[mouse_handle]; // Set and release capture of the mouse to fix http://www.opentk.com/node/2133, Patch by Artfunkel if ((raw.ButtonFlags & RawInputMouseState.LEFT_BUTTON_DOWN) != 0){ mouse.EnableBit((int)MouseButton.Left); Functions.SetCapture(Window); } if ((raw.ButtonFlags & RawInputMouseState.LEFT_BUTTON_UP) != 0) { mouse.DisableBit((int)MouseButton.Left); Functions.ReleaseCapture(); } if ((raw.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_DOWN) != 0) { mouse.EnableBit((int)MouseButton.Right); Functions.SetCapture(Window); } if ((raw.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_UP) != 0) { mouse.DisableBit((int)MouseButton.Right); Functions.ReleaseCapture(); } if ((raw.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_DOWN) != 0) { mouse.EnableBit((int)MouseButton.Middle); Functions.SetCapture(Window); } if ((raw.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_UP) != 0) { mouse.DisableBit((int)MouseButton.Middle); Functions.ReleaseCapture(); } if ((raw.ButtonFlags & RawInputMouseState.BUTTON_4_DOWN) != 0) { mouse.EnableBit((int)MouseButton.Button1); Functions.SetCapture(Window); } if ((raw.ButtonFlags & RawInputMouseState.BUTTON_4_UP) != 0) { mouse.DisableBit((int)MouseButton.Button1); Functions.ReleaseCapture(); } if ((raw.ButtonFlags & RawInputMouseState.BUTTON_5_DOWN) != 0) { mouse.EnableBit((int)MouseButton.Button2); Functions.SetCapture(Window); } if ((raw.ButtonFlags & RawInputMouseState.BUTTON_5_UP) != 0) { mouse.DisableBit((int)MouseButton.Button2); Functions.ReleaseCapture(); } if ((raw.ButtonFlags & RawInputMouseState.WHEEL) != 0) mouse.WheelPrecise += (short)raw.ButtonData / 120.0f; if ((raw.Flags & RawMouseFlags.MOUSE_MOVE_ABSOLUTE) != 0) { mouse.X = raw.LastX; mouse.Y = raw.LastY; } else { // Seems like MOUSE_MOVE_RELATIVE is the default, unless otherwise noted. mouse.X += raw.LastX; mouse.Y += raw.LastY; } lock (UpdateLock) { mice[mouse_handle] = mouse; return true; } }
/// <summary> /// Processes raw input events. /// </summary> /// <param name="rin"></param> /// <returns></returns> internal bool ProcessKeyboardEvent(RawInput rin) { //Keyboard key = keyboards[0]; //rin.Header.Device; switch (rin.Header.Type) { case RawInputDeviceType.KEYBOARD: bool pressed = rin.Data.Keyboard.Message == (int)WindowMessage.KEYDOWN || rin.Data.Keyboard.Message == (int)WindowMessage.SYSKEYDOWN; // Find the device where the button was pressed. It can be that the input notification // came not from a physical keyboard device but from a code-generated input message - in // that case, the event goes to the default (first) keyboard. // TODO: Send the event to all keyboards instead of the default one. // TODO: Optimize this! The predicate allocates way to much memory. //int index = keyboards.FindIndex(delegate(KeyboardDevice kb) //{ // return kb.DeviceID == rin.Header.Device; //}); //if (index == -1) index = 0; int index; if (keyboards.Count > 0) { index = 0; } else { return(false); } // Generic control, shift, alt keys may be sent instead of left/right. // It seems you have to explicitly register left/right events. switch (rin.Data.Keyboard.VKey) { case VirtualKeys.SHIFT: keyboards[index][Input.Key.ShiftLeft] = keyboards[index][Input.Key.ShiftRight] = pressed; return(true); case VirtualKeys.CONTROL: keyboards[index][Input.Key.ControlLeft] = keyboards[index][Input.Key.ControlRight] = pressed; return(true); case VirtualKeys.MENU: keyboards[index][Input.Key.AltLeft] = keyboards[index][Input.Key.AltRight] = pressed; return(true); default: if (!WMInput.KeyMap.ContainsKey(rin.Data.Keyboard.VKey)) { Debug.Print("Virtual key {0} ({1}) not mapped.", rin.Data.Keyboard.VKey, (int)rin.Data.Keyboard.VKey); } else { keyboards[index][WMInput.KeyMap[rin.Data.Keyboard.VKey]] = pressed; } return(false); } default: throw new ApplicationException("Windows raw keyboard driver received invalid data."); } }
internal static int GetRawInputData(IntPtr RawInput, GetRawInputDataEnum Command, RawInput* Data, [In, Out] ref int Size, int SizeHeader);
/// <summary> /// Processes raw input events. /// </summary> /// <param name="rin"></param> /// <returns></returns> internal bool ProcessEvent(RawInput rin) { //MouseDevice mouse = mice.Find(delegate(MouseDevice m) //{ // return m.DeviceID == rin.Header.Device; //}); MouseDevice mouse; if (mice.Count > 0) { mouse = mice[0]; } else { return(false); } switch (rin.Header.Type) { case RawInputDeviceType.MOUSE: if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.LEFT_BUTTON_DOWN) != 0) { mouse[MouseButton.Left] = true; } if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.LEFT_BUTTON_UP) != 0) { mouse[MouseButton.Left] = false; } if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_DOWN) != 0) { mouse[MouseButton.Right] = true; } if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_UP) != 0) { mouse[MouseButton.Right] = false; } if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_DOWN) != 0) { mouse[MouseButton.Middle] = true; } if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_UP) != 0) { mouse[MouseButton.Middle] = false; } if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.BUTTON_4_DOWN) != 0) { mouse[MouseButton.Button1] = true; } if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.BUTTON_4_UP) != 0) { mouse[MouseButton.Button1] = false; } if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.BUTTON_5_DOWN) != 0) { mouse[MouseButton.Button2] = true; } if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.BUTTON_5_UP) != 0) { mouse[MouseButton.Button2] = false; } if ((rin.Data.Mouse.ButtonFlags & RawInputMouseState.WHEEL) != 0) { mouse.Wheel += (short)rin.Data.Mouse.ButtonData / 120; } if ((rin.Data.Mouse.Flags & RawMouseFlags.MOUSE_MOVE_ABSOLUTE) != 0) { mouse.Position = new System.Drawing.Point(rin.Data.Mouse.LastX, rin.Data.Mouse.LastY); } else { // Seems like MOUSE_MOVE_RELATIVE is the default, unless otherwise noted. mouse.Position = new System.Drawing.Point(mouse.X + rin.Data.Mouse.LastX, mouse.Y + rin.Data.Mouse.LastY); } if ((rin.Data.Mouse.Flags & RawMouseFlags.MOUSE_VIRTUAL_DESKTOP) != 0) { Debug.WriteLine(String.Format("Mouse {0} defines MOUSE_VIRTUAL_DESKTOP flag, please report at http://www.opentk.com", mouse.ToString())); } return(true); default: throw new ApplicationException("WinRawMouse driver received invalid data."); } }
unsafe internal static extern LRESULT DefRawInputProc(ref RawInput RawInput, INT Input, UINT SizeHeader);
public bool ProcessKeyboardEvent(RawInput rin) { bool processed = false; bool pressed = rin.Data.Keyboard.Message == (int)WindowMessage.KEYDOWN || rin.Data.Keyboard.Message == (int)WindowMessage.SYSKEYDOWN; var scancode = rin.Data.Keyboard.MakeCode; var vkey = rin.Data.Keyboard.VKey; bool extended0 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E0) != 0; bool extended1 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E1) != 0; bool is_valid = true; ContextHandle handle = new ContextHandle(rin.Header.Device); KeyboardState keyboard; if (!rawids.ContainsKey(handle)) { RefreshDevices(); } if (keyboards.Count == 0) return false; // Note:For some reason, my Microsoft Digital 3000 keyboard reports 0 // as rin.Header.Device for the "zoom-in/zoom-out" buttons. // That's problematic, because no device has a "0" id. // As a workaround, we'll add those buttons to the first device (if any). int keyboard_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0; keyboard = keyboards[keyboard_handle]; Key key = WinKeyMap.TranslateKey(scancode, vkey, extended0, extended1, out is_valid); if (is_valid) { keyboard.SetKeyState(key, pressed); processed = true; } lock (UpdateLock) { keyboards[keyboard_handle] = keyboard; return processed; } }
public void Poll() { return; #if false // We will do a buffered read for all input devices and route the RawInput structures // to the correct 'ProcessData' handlers. First, we need to find out the size of the // buffer to allocate for the structures. Then we allocate the buffer and read the // structures, calling the correct handler for each one. Last, we free the allocated // buffer. int size = 0; Functions.GetRawInputBuffer(IntPtr.Zero, ref size, API.RawInputHeaderSize); size *= 256; IntPtr rin_data = Marshal.AllocHGlobal(size); while (true) { // Iterate reading all available RawInput structures and routing them to their respective // handlers. int num = Functions.GetRawInputBuffer(rin_data, ref size, API.RawInputHeaderSize); if (num == 0) { break; } else if (num < 0) { /*int error = Marshal.GetLastWin32Error(); * if (error == 122) * { * // Enlarge the buffer, it was too small. * AllocateBuffer(); * } * else * { * throw new ApplicationException(String.Format( * "GetRawInputBuffer failed with code: {0}", error)); * }*/ Debug.Print("GetRawInputBuffer failed with code: {0}", Marshal.GetLastWin32Error()); //AllocateBuffer(); break; } RawInput[] rin_structs = new RawInput[num]; IntPtr next_rin = rin_data; for (int i = 0; i < num; i++) { rin_structs[i] = (RawInput)Marshal.PtrToStructure(next_rin, typeof(RawInput)); switch (rin_structs[i].Header.Type) { case RawInputDeviceType.KEYBOARD: keyboardDriver.ProcessKeyboardEvent(rin_structs[i]); break; case RawInputDeviceType.MOUSE: mouseDriver.ProcessEvent(rin_structs[i]); break; } next_rin = Functions.NextRawInputStructure(next_rin); } Functions.DefRawInputProc(rin_structs, num, (uint)API.RawInputHeaderSize); } Marshal.FreeHGlobal(rin_data); #endif }
/// <summary> /// Processes raw input events. /// </summary> /// <param name="rin"></param> /// <returns></returns> internal bool ProcessKeyboardEvent(RawInput rin) { //Keyboard key = keyboards[0]; //rin.Header.Device; switch (rin.Header.Type) { case RawInputDeviceType.KEYBOARD: bool pressed = rin.Data.Keyboard.Message == (int)WindowMessage.KEYDOWN || rin.Data.Keyboard.Message == (int)WindowMessage.SYSKEYDOWN; // Find the device where the button was pressed. It can be that the input notification // came not from a physical keyboard device but from a code-generated input message - in // that case, the event goes to the default (first) keyboard. // TODO: Send the event to all keyboards instead of the default one. // TODO: Optimize this! The predicate allocates way to much memory. //int index = keyboards.FindIndex(delegate(KeyboardDevice kb) //{ // return kb.DeviceID == rin.Header.Device; //}); //if (index == -1) index = 0; int index; if (keyboards.Count > 0) index = 0; else return false; // Generic control, shift, alt keys may be sent instead of left/right. // It seems you have to explicitly register left/right events. switch (rin.Data.Keyboard.VKey) { case VirtualKeys.SHIFT: keyboards[index][Input.Key.ShiftLeft] = keyboards[index][Input.Key.ShiftRight] = pressed; return true; case VirtualKeys.CONTROL: keyboards[index][Input.Key.ControlLeft] = keyboards[index][Input.Key.ControlRight] = pressed; return true; case VirtualKeys.MENU: keyboards[index][Input.Key.AltLeft] = keyboards[index][Input.Key.AltRight] = pressed; return true; default: if (!WMInput.KeyMap.ContainsKey(rin.Data.Keyboard.VKey)) Debug.Print("Virtual key {0} ({1}) not mapped.", rin.Data.Keyboard.VKey, (int)rin.Data.Keyboard.VKey); else keyboards[index][WMInput.KeyMap[rin.Data.Keyboard.VKey]] = pressed; return false; } default: throw new ApplicationException("Windows raw keyboard driver received invalid data."); } }
public bool ProcessKeyboardEvent(RawInput rin) { bool processed = false; bool pressed = rin.Data.Keyboard.Message == (int)WindowMessage.KEYDOWN || rin.Data.Keyboard.Message == (int)WindowMessage.SYSKEYDOWN; ContextHandle handle = new ContextHandle(rin.Header.Device); KeyboardState keyboard; if (!rawids.ContainsKey(handle)) { RefreshDevices(); } if (keyboards.Count == 0) return false; // Note:For some reason, my Microsoft Digital 3000 keyboard reports 0 // as rin.Header.Device for the "zoom-in/zoom-out" buttons. // That's problematic, because no device has a "0" id. // As a workaround, we'll add those buttons to the first device (if any). int keyboard_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0; keyboard = keyboards[keyboard_handle]; // Generic control, shift, alt keys may be sent instead of left/right. // It seems you have to explicitly register left/right events. switch (rin.Data.Keyboard.VKey) { case VirtualKeys.SHIFT: keyboard[Input.Key.ShiftLeft] = keyboard[Input.Key.ShiftRight] = pressed; processed = true; break; case VirtualKeys.CONTROL: keyboard[Input.Key.ControlLeft] = keyboard[Input.Key.ControlRight] = pressed; processed = true; break; case VirtualKeys.MENU: keyboard[Input.Key.AltLeft] = keyboard[Input.Key.AltRight] = pressed; processed = true; break; default: if (!KeyMap.ContainsKey(rin.Data.Keyboard.VKey)) { Debug.Print("Virtual key {0} ({1}) not mapped.", rin.Data.Keyboard.VKey, (int)rin.Data.Keyboard.VKey); } else { keyboard[KeyMap[rin.Data.Keyboard.VKey]] = pressed; processed = true; } break; } lock (UpdateLock) { keyboards[keyboard_handle] = keyboard; return processed; } }
public bool ProcessMouseEvent(RawInput rin) { RawMouse rawMouse = rin.Data.Mouse; ContextHandle key = new ContextHandle(rin.Header.Device); if (!this.rawids.ContainsKey(key)) { this.RefreshDevices(); } if (this.mice.Count == 0) { return(false); } int index = this.rawids.ContainsKey(key) ? this.rawids[key] : 0; MouseState mouseState = this.mice[index]; if ((rawMouse.ButtonFlags & RawInputMouseState.LEFT_BUTTON_DOWN) != (RawInputMouseState)0) { mouseState.EnableBit(0); } if ((rawMouse.ButtonFlags & RawInputMouseState.LEFT_BUTTON_UP) != (RawInputMouseState)0) { mouseState.DisableBit(0); } if ((rawMouse.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_DOWN) != (RawInputMouseState)0) { mouseState.EnableBit(2); } if ((rawMouse.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_UP) != (RawInputMouseState)0) { mouseState.DisableBit(2); } if ((rawMouse.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_DOWN) != (RawInputMouseState)0) { mouseState.EnableBit(1); } if ((rawMouse.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_UP) != (RawInputMouseState)0) { mouseState.DisableBit(1); } if ((rawMouse.ButtonFlags & RawInputMouseState.BUTTON_4_DOWN) != (RawInputMouseState)0) { mouseState.EnableBit(3); } if ((rawMouse.ButtonFlags & RawInputMouseState.BUTTON_4_UP) != (RawInputMouseState)0) { mouseState.DisableBit(3); } if ((rawMouse.ButtonFlags & RawInputMouseState.BUTTON_5_DOWN) != (RawInputMouseState)0) { mouseState.EnableBit(4); } if ((rawMouse.ButtonFlags & RawInputMouseState.BUTTON_5_UP) != (RawInputMouseState)0) { mouseState.DisableBit(4); } if ((rawMouse.ButtonFlags & RawInputMouseState.WHEEL) != (RawInputMouseState)0) { mouseState.WheelPrecise += (float)(short)rawMouse.ButtonData / 120f; } if ((rawMouse.Flags & RawMouseFlags.MOUSE_MOVE_ABSOLUTE) != RawMouseFlags.MOUSE_MOVE_RELATIVE) { mouseState.X = rawMouse.LastX; mouseState.Y = rawMouse.LastY; } else { mouseState.X += rawMouse.LastX; mouseState.Y += rawMouse.LastY; } lock (this.UpdateLock) { this.mice[index] = mouseState; return(true); } }