private void ButtonImportJson_Click(object sender, EventArgs e) { if (MessageBox.Show("This will override your current data, do you want to continue?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) { return; } listView1.Items.Clear(); frameList.Clear(); textBoxKeys.Text = ""; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "JSON files (*.json)|*.json"; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } List <AirFrameJSON> inputBuffer; using (StreamReader file = new StreamReader(openFileDialog.FileName)) { try { inputBuffer = new JavaScriptSerializer().Deserialize <List <AirFrameJSON> >(file.ReadToEnd()); } catch { MessageBox.Show("Invalid JSON file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } KeyboardLayout us_keyboard = new KeyboardLayout(); foreach (AirFrameJSON item in inputBuffer) { listView1.Items.Insert(0, new ListViewItem(new[] { item.Address, item.PID.ToString(), item.Ch.ToString(), item.Length.ToString(), item.Payload, item.DecryptedPayload })); AirFrame af = new AirFrame(item); frameList.Add(af); if (af.decryptedPayload == null || af.decryptedPayload.Length == 0) { continue; } textBoxKeys.Text += us_keyboard.ToComboKeyPress(af.decryptedPayload); } }
private void HidHandler_DoWork(object sender, DoWorkEventArgs e) { Console.Write("Opening device..."); ReportDescriptor reportDescriptor = selectedDevice.GetReportDescriptor(); foreach (DeviceItem deviceItem in reportDescriptor.DeviceItems) { HidStream hidStream; if (selectedDevice.TryOpen(out hidStream)) { Console.WriteLine("OK"); hidStream.ReadTimeout = Timeout.Infinite; using (hidStream) { var inputReportBuffer = new byte[selectedDevice.GetMaxInputReportLength()]; var inputReceiver = reportDescriptor.CreateHidDeviceInputReceiver(); var inputParser = deviceItem.CreateDeviceItemInputParser(); inputReceiver.Start(hidStream); while (!HidHandler.CancellationPending) { if (inputReceiver.WaitHandle.WaitOne(1)) { if (!inputReceiver.IsRunning) { Console.WriteLine("Device disconnected!"); break; } Report report; byte[] buffer = new byte[255]; while (inputReceiver.TryRead(inputReportBuffer, 0, out report)) { if (inputParser.TryParseReport(inputReportBuffer, 0, report)) { AirFrame af = new AirFrame(inputReportBuffer); string address = BitConverter.ToString(af.address).Replace("-", ":"); string pid = af.pid.ToString(); string ch = af.rf_channel.ToString(); string len = af.payloadLength.ToString(); string payload = af.payloadLength != 0 ? BitConverter.ToString(af.payload) : "<EMPTY>"; string decrpytedPayload = ""; byte[] decryptedPayloadBytes = null; try { if (autoDecrypt) { af.DecryptKeyboardFrame(deviceKey); decryptedPayloadBytes = af.decryptedPayload; decrpytedPayload = BitConverter.ToString(decryptedPayloadBytes); } } catch { decrpytedPayload = ""; } frameList.Add(af); try { if (autoDecrypt && decryptedPayloadBytes != null) { KeyboardLayout kl = new KeyboardLayout(); textBoxKeys.Invoke(new Action(() => { textBoxKeys.Text += kl.ToComboKeyPress(decryptedPayloadBytes); })); } } catch { } listView1.Invoke(new Action(() => { listView1.Items.Insert(0, new ListViewItem(new[] { address, pid, ch, len, payload, decrpytedPayload })); })); } } } } } } else { Console.WriteLine("Fail"); } } }