public ListViewItem ToListViewItem() { //TODO: What to do with multiple usage var usage = ""; var usagePage = (UsagePage)UsagePage; switch (usagePage) { case Hid.UsagePage.Consumer: usage = ((ConsumerControl)Usages[0]).ToString(); break; case Hid.UsagePage.WindowsMediaCenterRemoteControl: usage = ((WindowsMediaCenterRemoteControl)Usages[0]).ToString(); break; } var item = new ListViewItem(new[] { usage, UsagePage.ToString("X2"), UsageCollection.ToString("X2"), RepeatCount.ToString(), Time.ToString("HH:mm:ss:fff") }); return(item); }
/// <summary> /// Create a human readable string out of this object. /// </summary> /// <returns></returns> public override string ToString() { if (!IsValid) { return("HID: Invalid HidEvent"); } // string res = "==== HidEvent ====\n"; res += Device.ToString(); if (IsGeneric) { res += "Generic\n"; } if (IsKeyboard) { res += "Keyboard\n"; } if (IsMouse) { res += "Mouse\n"; } res += "Foreground: " + IsForeground + "\n"; res += "UsagePage: 0x" + UsagePage.ToString("X4") + "\n"; res += "UsageCollection: 0x" + UsageCollection.ToString("X4") + "\n"; foreach (var usage in Usages) { res += "Usage: 0x" + usage.ToString("X4") + "\n"; } res += "==================\n"; return(res); }
/// <summary> /// Print information about this device to our debug output. /// </summary> public void DebugWrite() { if (!IsValid) { Debug.WriteLine("==== Invalid HidEvent"); return; } Device.DebugWrite(); if (IsGeneric) { Debug.WriteLine("==== Generic"); } if (IsKeyboard) { Debug.WriteLine("==== Keyboard"); } if (IsMouse) { Debug.WriteLine("==== Mouse"); } Debug.WriteLine("==== Foreground: " + IsForeground); Debug.WriteLine("==== UsagePage: 0x" + UsagePage.ToString("X4")); Debug.WriteLine("==== UsageCollection: 0x" + UsageCollection.ToString("X4")); foreach (var usage in Usages) { Debug.WriteLine("==== Usage: 0x" + usage.ToString("X4")); } }
public static bool UsageToString(UsagePage usagePage, int usage, out string usagePageString, out string usageString) { const string kVendorDefined = "Vendor-Defined"; if (( int )usagePage >= 0xFF00) { usagePageString = kVendorDefined; usageString = kVendorDefined; return(true); } usagePageString = usagePage.ToString(); usageString = null; switch (usagePage) { case UsagePage.GenericDesktop: usageString = ((GenericDesktop)usage).ToString(); break; case UsagePage.Simulation: usageString = ((Simulation)usage).ToString(); break; default: return(false); } return(true); }
/// <summary> /// /// </summary> /// <returns></returns> public string ToLog(uint aDepth = 0) { string res = ""; if (!IsValid) { res += "WARNING: Invalid HID Event"; } //Compute our prefix and indent const string KPrefix = " "; string prefix = ""; for (uint i = 0; i < aDepth; i++) { prefix += KPrefix; } string indent = prefix + KPrefix; // //Open our HID Event block res += "[HID Event]" + "\r\n"; // Log our debug string too to give a quick overview res += indent + "String: " + ToString() + "\r\n"; //For each properties of our HID Event foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(this)) { string propertyName = property.Name; object value = property.GetValue(this); if (value != null) { //Check if our property value object type has a method of the same name has this one. Type type = value.GetType(); MethodInfo method = type.GetMethod(System.Reflection.MethodBase.GetCurrentMethod().Name); if (method != null && method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == aDepth.GetType()) { object[] parameters = new object[] { aDepth + 1 }; res += method.Invoke(value, parameters); } //Make special case for some properties else if (propertyName.Equals("InputReport")) { res += string.Format("{0}{1}: {2}\r\n", indent, propertyName, InputReportString()); } else if (propertyName.Equals("UsagePage")) { res += string.Format("{0}{1}: {2} (0x{3})\r\n", indent, propertyName, UsagePageEnum, UsagePage.ToString("X4")); } else if (propertyName.Equals("UsageCollection")) { res += string.Format("{0}{1}: {2}\r\n", indent, propertyName, UsageCollectionNameAndValue()); } else if (propertyName.Equals("Usages")) { for (int i = 0; i < Usages.Count; i++) { res += string.Format("{0}{1}: {2}\r\n", indent, propertyName, UsageNameAndValue(i)); } } else if (propertyName.Equals("UsageValues")) { //Just give the count res += string.Format("{0}{1}: {2}\r\n", indent, "UsageValues count", UsageValues.Count); } else if (propertyName.Equals("RawInput") || propertyName.Equals("UsagePageEnum")) { //Skip those } else { res += string.Format("{0}{1}: {2}\r\n", indent, propertyName, value); } } else { // Special case for NULL values res += string.Format("{0}{1}: NULL\r\n", indent, propertyName); } } //Close our HID Event block res += "[/HID Event]" + "\r\n"; return(res); }
/// <summary> /// Provides name and value of our usage page as a string. /// </summary> public string UsagePageNameAndValue() { return(string.Format("{0} (0x{1})", UsagePageName(), UsagePage.ToString("X4"))); }
/// <summary> /// Provides the name of our usage page. /// </summary> public string UsagePageName() { return(UsagePageEnum.ToString()); }
/// <summary> /// /// </summary> private void SetFriendlyName() { //Work out proper suffix for our device root node. //That allows users to see in a glance what kind of device this is. string suffix = ""; Type usageCollectionType = null; if (Info.dwType == RawInputDeviceType.RIM_TYPEHID) { //Process usage page if (Enum.IsDefined(typeof(UsagePage), Info.hid.usUsagePage)) { //We know this usage page, add its name UsagePage usagePage = (UsagePage)Info.hid.usUsagePage; suffix += " ( " + usagePage.ToString() + ", "; usageCollectionType = Utils.UsageCollectionType(usagePage); } else { //We don't know this usage page, add its value suffix += " ( 0x" + Info.hid.usUsagePage.ToString("X4") + ", "; } //Process usage collection //We don't know this usage page, add its value if (usageCollectionType == null || !Enum.IsDefined(usageCollectionType, Info.hid.usUsage)) { //Show Hexa suffix += "0x" + Info.hid.usUsage.ToString("X4") + " )"; } else { //We know this usage page, add its name suffix += Enum.GetName(usageCollectionType, Info.hid.usUsage) + " )"; } } else if (Info.dwType == RawInputDeviceType.RIM_TYPEKEYBOARD) { suffix = " - Keyboard"; } else if (Info.dwType == RawInputDeviceType.RIM_TYPEMOUSE) { suffix = " - Mouse"; } if (Product != null && Product.Length > 1) { //This device as a proper name, use it FriendlyName = Product + suffix; } else { //Extract friendly name from name char[] delimiterChars = { '#', '&' }; string[] words = Name.Split(delimiterChars); if (words.Length >= 2) { //Use our name sub-string to describe this device FriendlyName = words[1] + " - 0x" + ProductId.ToString("X4") + suffix; } else { //No proper name just use the device ID instead FriendlyName = "0x" + ProductId.ToString("X4") + suffix; } } }