public void RefreshKeyboardList() { lock (lockRefreshKeyboards) { List<DeviceInstance> keyboardInstances = null; if (Keyboards == null) Keyboards = new List<DirectInputKeyboard>(); try { keyboardInstances = Utils.DevList(Manager.GetDevices(DeviceClass.Keyboard, EnumDevicesFlags.AttachedOnly)); } catch (Exception e) { Debug.Print("RefreshKeyboardList {0}", e.Message); } if (keyboardInstances == null) return; Keyboards.Where(dev => !keyboardInstances.Exists(d => dev.Guid == d.InstanceGuid)).ToList().ForEach( gc => { gc.Unacquire(); Keyboards.Remove(gc); } ); var addList = keyboardInstances.Where(d => !Keyboards.Exists(dev => dev.Guid == d.InstanceGuid)); if (addList == null || addList.Count() <= 0) return; foreach (DeviceInstance dev in addList) { var keyboard = new DirectInputKeyboard(dev); keyboard.OnError += new EventHandler<EventArgs>(OnNoNeed); keyboard.OnUnacquire += new EventHandler<EventArgs>(OnNoNeed); Keyboards.Add(keyboard); if (Keyboards.Where(d => d.Name.StartsWith(keyboard.Name)).Count() > 1) { Keyboards.Where(d => d.Name.StartsWith(keyboard.Name) && d.Index == 0).ToList().ForEach( d => d.Index = Keyboards.Max(dm => dm.Index) + 1 ); } } if (OnKeyboardList != null) OnKeyboardList(this, EventArgs.Empty); } }