//here we send a ping to the keyboard driver void Ping() { var KeyboardData = new SetFeatureKeyboard(); KeyboardData.ReportID = 1; KeyboardData.CommandCode = 3; //the timeout is how long the driver will wait (milliseconds) without receiving a ping before resetting itself //we'll be pinging every 200ms, and loss of ping will cause driver reset in FTimeout. //No more stuck keys requiring reboot to clear. //the following fields are not used by the driver for a ping, but we'll zero them anyways KeyboardData.Timeout = FTimeout / 5; //50 because we count in blocks of 50 in the driver; KeyboardData.Modifier = 0; KeyboardData.Padding = 0; KeyboardData.Key0 = 0; KeyboardData.Key1 = 0; KeyboardData.Key2 = 0; KeyboardData.Key3 = 0; KeyboardData.Key4 = 0; KeyboardData.Key5 = 0; //convert struct to buffer var buf = getBytesSFJ(KeyboardData, Marshal.SizeOf(KeyboardData)); //send filled buffer to driver HID.SendData(buf, (uint)Marshal.SizeOf(KeyboardData)); }
private bool KeysPress(byte Modifier, byte Key0, byte Key1, byte Key2, byte Key3, byte Key4, byte Key5) { try { //Set feature data SetFeatureKeyboard featureData = new SetFeatureKeyboard(); featureData.ReportID = 1; featureData.CommandCode = 2; featureData.Timeout = 1000; featureData.Modifier = Modifier; featureData.Key0 = Key0; featureData.Key1 = Key1; featureData.Key2 = Key2; featureData.Key3 = Key3; featureData.Key4 = Key4; featureData.Key5 = Key5; //Convert to byte array int featureSize = Marshal.SizeOf(featureData); byte[] featureArray = new byte[featureSize]; IntPtr featureIntPtr = Marshal.AllocHGlobal(featureSize); Marshal.StructureToPtr(featureData, featureIntPtr, false); Marshal.Copy(featureIntPtr, featureArray, 0, featureSize); Marshal.FreeHGlobal(featureIntPtr); //Send byte array to driver return(SetFeature(FileHandle, featureArray)); } catch { Debug.WriteLine("Failed to press tether keys."); return(false); } }
private bool KeysRelease() { try { //Set feature data SetFeatureKeyboard featureData = new SetFeatureKeyboard(); featureData.ReportID = 1; featureData.CommandCode = 2; featureData.Timeout = 1000; //Convert to byte array int featureSize = Marshal.SizeOf(featureData); byte[] featureArray = new byte[featureSize]; IntPtr featureIntPtr = Marshal.AllocHGlobal(featureSize); Marshal.StructureToPtr(featureData, featureIntPtr, false); Marshal.Copy(featureIntPtr, featureArray, 0, featureSize); Marshal.FreeHGlobal(featureIntPtr); //Send byte array to driver return(SetFeature(FileHandle, featureArray)); } catch { Debug.WriteLine("Failed to release tether keys."); return(false); } }
//for converting a struct to byte array public static byte[] getBytesSFJ(SetFeatureKeyboard sfj, int size) { var arr = new byte[size]; var ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(sfj, ptr, false); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); return(arr); }
//here we send data to the keyboard driver void Send(Byte Modifier, Byte Padding, Byte Key0, Byte Key1, Byte Key2, Byte Key3, Byte Key4, Byte Key5) { SetFeatureKeyboard KeyboardData = new SetFeatureKeyboard(); KeyboardData.ReportID = 1; KeyboardData.CommandCode = 2; KeyboardData.Timeout = FTimeout / 5; //5 because we count in blocks of 5 in the driver KeyboardData.Modifier = Modifier; //padding should always be zero. KeyboardData.Padding = Padding; KeyboardData.Key0 = Key0; KeyboardData.Key1 = Key1; KeyboardData.Key2 = Key2; KeyboardData.Key3 = Key3; KeyboardData.Key4 = Key4; KeyboardData.Key5 = Key5; //convert struct to buffer byte[] buf = getBytesSFJ(KeyboardData, Marshal.SizeOf(KeyboardData)); //send filled buffer to driver HID.SendData(buf, (uint)Marshal.SizeOf(KeyboardData)); }