InputDeviceInterface GetInputDeviceFromBindingDevice(BindingsFile.Device dv)
        {
            InputDeviceInterface i = devices.Find(x =>
            {
                BindingsFile.Device b = bf.FindDevice(x.ID().Name, x.ID().Instanceguid, x.ID().Productguid);
                return(b != null && b.Name.Equals(dv.Name));
            });

            return(i);
        }
        private void Devices_OnNewEvent(List <InputDeviceEvent> list)
        {
            IntPtr handle = GetForegroundWindow();

            Process[] processes = Process.GetProcessesByName("elitedangerous64");//Process.GetProcessesByName("EliteDangerous64");
            bool      ed        = false;

            foreach (Process p in processes)
            {
                if (p.MainWindowHandle == handle)       //ED seems to have multiple processes running.. find one
                {
                    ed = true;
                    break;
                }
            }

            if (!ed)
            {
                //System.Diagnostics.Debug.WriteLine("Rejected keypress " + processes.Length);
                return;
            }

            foreach (InputDeviceEvent je in list)
            {
                string match = je.EventName();              // same as bindings name..
                System.Diagnostics.Debug.WriteLine(je.ToString(10) + " " + match);

                ac.ActionRun("onEliteInputRaw", "EliteUIEvent", additionalvars: new Conditions.ConditionVariables(new string[]
                                                                                                                  { "Device", je.Device.ID().Name, "EventName", match, "Pressed", je.Pressed?"1":"0", "Value", je.Value.ToStringInvariant() }));

                BindingsFile.Device dv = GetBindingDeviceFromInputDeviceIdentifier(je.Device.ID());

                if (dv != null)
                {
                    List <BindingsFile.Assignment> assignlist = dv.Find(match, false);       // get everything associated with this key..

                    if (assignlist != null)
                    {
                        List <BindingsFile.Assignment> inonstate = new List <BindingsFile.Assignment>();
                        List <bool> ispressable = new List <bool>();

                        foreach (BindingsFile.Assignment a in assignlist)
                        {
                            Tuple <bool, bool> pressstate = IsAllPressed(a);
                            if (pressstate.Item1)       // if pressed
                            {
                                //System.Diagnostics.Debug.WriteLine("  Rule Matches " + a.assignedfunc);
                                inonstate.Add(a);       // but it might not be the best rule..
                                ispressable.Add(pressstate.Item2);
                            }
                            else
                            {
                                int isonindex = assignmentsinonstate.IndexOf(a);
                                if (isonindex != -1)
                                {
                                    System.Diagnostics.Debug.WriteLine("Action " + a.assignedfunc + "-");
                                    assignmentsinonstate.Remove(a);
                                    ac.ActionRun("onEliteInputOff", "EliteUIEvent", additionalvars: new Conditions.ConditionVariables(new string[]
                                                                                                                                      { "Binding", a.assignedfunc }));
                                }
                            }
                        }

                        List <string> bindingstoexecute = new List <string>();

                        for (int i = 0; i < inonstate.Count; i++)
                        {
                            BindingsFile.Assignment a = inonstate[i];
                            if (a.KeyAssignementLongerThan(inonstate))  // we have the best key list
                            {
                                if (ispressable[i])
                                {
                                    assignmentsinonstate.Add(a);
                                }

                                bindingstoexecute.Add(a.assignedfunc);
                                System.Diagnostics.Debug.WriteLine("Action " + a.assignedfunc);
                            }
                            else
                            {
                                //System.Diagnostics.Debug.WriteLine("  Reject Rule due to others are longer " + inonstate[i].assignedfunc);
                            }
                        }

                        //System.Diagnostics.Debug.WriteLine("On state " + assignmentsinonstate.Count);

                        foreach (string s in bindingstoexecute)
                        {
                            ac.ActionRun("onEliteInput", "EliteUIEvent", additionalvars: new Conditions.ConditionVariables(new string[]
                                                                                                                           { "Device", je.Device.ID().Name, "Binding", s, "BindingList", String.Join(",", bindingstoexecute),
                                                                                                                             "EventName", match, "Pressed", je.Pressed?"1":"0", "Value", je.Value.ToStringInvariant() }));
                        }
                    }
                }
            }
        }
 BindingsFile.Device GetBindingDeviceFromInputDeviceIdentifier(InputDeviceIdentity i)
 {
     BindingsFile.Device dv = bf.FindDevice(i.Name, i.Instanceguid, i.Productguid);
     return(dv);
 }