public void SetPosition(double x, double y) { Functions.SetCursorPos((int)x, (int)y); }
public void RefreshDevices() { lock (UpdateLock) { // Mark all devices as disconnected. We will check which of those // are connected later on. for (int i = 0; i < mice.Count; i++) { MouseState state = mice[i]; state.IsConnected = false; mice[i] = state; } int count = WinRawInput.DeviceCount; RawInputDeviceList[] ridl = new RawInputDeviceList[count]; for (int i = 0; i < count; i++) { ridl[i] = new RawInputDeviceList(); } Functions.GetRawInputDeviceList(ridl, ref count, API.RawInputDeviceListSize); // Discover mouse devices foreach (RawInputDeviceList dev in ridl) { ContextHandle id = new ContextHandle(dev.Device); if (rawids.ContainsKey(id)) { // Device already registered, mark as connected MouseState state = mice[rawids[id]]; state.IsConnected = true; mice[rawids[id]] = state; continue; } // Unregistered device, find what it is string name = GetDeviceName(dev); if (name.ToLower().Contains("root")) { // This is a terminal services device, skip it. continue; } else if (dev.Type == RawInputDeviceType.MOUSE || dev.Type == RawInputDeviceType.HID) { // This is a mouse or a USB mouse device. In the latter case, discover if it really is a // mouse device by qeurying the registry. RegistryKey regkey = FindRegistryKey(name); if (regkey == null) { continue; } string deviceDesc = (string)regkey.GetValue("DeviceDesc"); string deviceClass = (string)regkey.GetValue("Class") as string; if (deviceClass == null) { // Added to address OpenTK issue 3198 with mouse on Windows 8 string deviceClassGUID = (string)regkey.GetValue("ClassGUID"); RegistryKey classGUIDKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\" + deviceClassGUID); deviceClass = classGUIDKey != null ? (string)classGUIDKey.GetValue("Class") : string.Empty; } // deviceDesc remained null on a new Win7 system - not sure why. // Since the description is not vital information, use a dummy description // when that happens. if (String.IsNullOrEmpty(deviceDesc)) { deviceDesc = "Windows Mouse " + mice.Count; } else { deviceDesc = deviceDesc.Substring(deviceDesc.LastIndexOf(';') + 1); } if (!String.IsNullOrEmpty(deviceClass) && deviceClass.ToLower().Equals("mouse")) { if (!rawids.ContainsKey(new ContextHandle(dev.Device))) { // Register the device: RawInputDeviceInfo info = new RawInputDeviceInfo(); int devInfoSize = API.RawInputDeviceInfoSize; Functions.GetRawInputDeviceInfo(dev.Device, RawInputDeviceInfoEnum.DEVICEINFO, info, ref devInfoSize); RegisterRawDevice(Window, deviceDesc); MouseState state = new MouseState(); state.IsConnected = true; mice.Add(state); names.Add(deviceDesc); rawids.Add(new ContextHandle(dev.Device), mice.Count - 1); } } } } } }
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, (short)raw.ButtonData / 120.0f); } if ((raw.ButtonFlags & RawInputMouseState.HWHEEL) != 0) { mouse.SetScrollRelative((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); } }