private void btnConnect_Click(object sender, EventArgs e) { if (btnConnect.Text == "连接设备") { if (myHid.Opened == false) { //获取 ID 号 UInt16 myVendorID = Convert.ToUInt16(tbxVendorID.Text, 16); UInt16 myProductID = Convert.ToUInt16(tbxProductID.Text, 16); //myHidPtr = new IntPtr();//定义为类字段 if ((int)(myHidPtr = myHid.OpenDevice(myVendorID, myProductID)) != -1) { btnConnect.Text = "断开设备"; stateLabel.Text = "设备已连接"; stateLabel.BackColor = this.stateLabel.BackColor = System.Drawing.Color.Gray; } else { stateLabel.Text = "连接失败"; stateLabel.BackColor = this.stateLabel.BackColor = System.Drawing.Color.Red; } } } else { btnConnect.Text = "连接设备"; myHid.CloseDevice(myHidPtr); stateLabel.Text = "设备未连接"; this.stateLabel.BackColor = System.Drawing.SystemColors.ActiveCaption; } }
/// <summary> /// Lights the keyboard with the specified keys and colors, using the specified default color for unlisted keys. /// </summary> /// <param name="keyColors">The key colors.</param> /// <param name="defaultColor">The default color for unspecified keys.</param> public override void DrawKeys(IDictionary <uint, Color> keyColors, Color defaultColor) { var redPayload = new byte[72]; var greenPayload = new byte[72]; var bluePayload = new byte[72]; var redDefault = (byte)(defaultColor.R ^ byte.MaxValue); var greenDefault = (byte)(defaultColor.G ^ byte.MaxValue); var blueDefault = (byte)(defaultColor.B ^ byte.MaxValue); for (uint i = 0; i < 72; i++) { var keyIndex = i * 2; byte red; byte green; byte blue; Color color; if (keyColors.TryGetValue(keyIndex, out color)) { red = (byte)(((color.R ^ byte.MaxValue) >> 1) & 0x70); green = (byte)(((color.G ^ byte.MaxValue) >> 1) & 0x70); blue = (byte)(((color.B ^ byte.MaxValue) >> 1) & 0x70); } else { red = (byte)(redDefault >> 1); green = (byte)(greenDefault >> 1); blue = (byte)(blueDefault >> 1); } keyIndex = (i * 2) + 1; if (keyColors.TryGetValue(keyIndex, out color)) { red |= (byte)((color.R ^ byte.MaxValue) >> 5); green |= (byte)((color.G ^ byte.MaxValue) >> 5); blue |= (byte)((color.B ^ byte.MaxValue) >> 5); } else { red |= (byte)(redDefault >> 5); green |= (byte)(greenDefault >> 5); blue |= (byte)(blueDefault >> 5); } redPayload[i] = red; greenPayload[i] = green; bluePayload[i] = blue; } var payloads = CorsairRgbKeyboardPacket.GetPackets(redPayload, greenPayload, bluePayload); Hid.OpenDevice(); foreach (var payload in payloads) { Hid.WriteReport(new HidReport(Hid.Capabilities.OutputReportByteLength, new HidDeviceData(new byte[] { 0x00 }.Concat(payload.GetBytes()).ToArray <byte>(), HidDeviceData.ReadStatus.Success))); } Hid.CloseDevice(); }
private void btnConnect_Click(object sender, EventArgs e) { if (btnConnect.Text == "Connect") { ushort vid = 0x8088; byte mid = 0x02; ushort pid = (ushort)(devList.SelectedIndex + 1); if (devList.SelectedItem.Equals("SimPad Boot")) { pid = 0x00FF; } ptr = hid.OpenDevice(vid, pid, mid); if ((int)ptr != -1) { outputBox.Clear(); outputBox.Text += ("Connected to: " + devList.SelectedItem + "\n"); devList.Enabled = false; btnConnect.Text = "Close"; } } else { if ((int)ptr != -1) { hid.CloseDevice(ptr); } ptr = new IntPtr(-1); devList.Enabled = true; btnConnect.Text = "Connect"; } }