public static void UninstallBuiltInDrivers()
        {
            LogWriter.Write("Uninstalling Built-in drivers");
            try
            {
                var    path = ResourceExtractor.ExtractResource(Assembly.GetExecutingAssembly(), "KeyboardSplitter.Lib.keyboard_driver.exe");
                string msg1 = InterceptionDriver.Uninstall(path);
                string msg2 = VirtualXboxBus.Uninstall();
                LogWriter.Write(msg1);
                LogWriter.Write(msg2);

                Controls.MessageBox.Show(
                    "Uninstallation finished. You must reboot the system to completely remove the drivers.\r\n\r\n Application will now close.",
                    ApplicationInfo.AppName,
                    MessageBoxButton.OK,
                    MessageBoxImage.Information);

                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                LogWriter.Write("Uninstalling Built-in drivers FAILED:\r\n" + ex.ToString());

                Controls.MessageBox.Show(
                    "Uninstall failed: " + ex.Message,
                    "Uninstall failed",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
        }
 public void DestroyContext()
 {
     if (IsContextCreated == true)
     {
         InterceptionDriver.DestroyContext(context);
         IsContextCreated = false;
     }
 }
 public void CreateContext()
 {
     if (IsContextCreated == false)
     {
         context          = InterceptionDriver.CreateContext();
         IsContextCreated = true;
     }
 }
        private void ReceiveKeyboardEvent()
        {
            Stroke stroke = new Stroke();

            while (InterceptionDriver.Receive(context, deviceId = InterceptionDriver.Wait(context), ref stroke, 1) > 0)
            {
                InterceptionDriver.Send(context, deviceId, ref stroke, 1);
            }
        }
예제 #5
0
        private void Stop()
        {
            if (context != IntPtr.Zero)
            {
                InterceptionDriver.DestroyContext(context);
            }
            ni.Dispose();

            Process.GetCurrentProcess().Kill();
        }
        public void PressKey(Keys key)
        {
            Stroke stroke = new Stroke();

            stroke.Key      = new KeyStroke();
            stroke.Key.Code = key;

            InterceptionDriver.Send(context, deviceId, ref stroke, 1);
            Thread.Sleep(1);
            ReleaseKey(key);
        }
        public void ReleaseKey(Keys key)
        {
            Stroke stroke = new Stroke();

            stroke.Key = new KeyStroke();

            stroke.Key.Code  = key;
            stroke.Key.State = KeyState.Up;

            InterceptionDriver.Send(context, deviceId, ref stroke, 1);
        }
예제 #8
0
 public void Abort()
 {
     _cancelSource.Cancel();
     _InterceptionListenerThread.Abort();
     if (_inteceptionContext != IntPtr.Zero)
     {
         InterceptionDriver.DestroyContext(_inteceptionContext);
     }
     if (_targets != null)
     {
         foreach (IClickerTarget plugin in _targets)
         {
             plugin.Dispose();
         }
     }
 }
예제 #9
0
        private void Stop()
        {
            if (context != IntPtr.Zero)
            {
                InterceptionDriver.DestroyContext(context);
            }

            ni.Dispose();

            Dispatcher.Invoke(() =>
            {
                black_sc1.Close();
                black_sc2.Close();
                this.Close();
            });
        }
예제 #10
0
        private void Start()
        {
            context = InterceptionDriver.CreateContext();

            ni.BalloonTipIcon  = System.Windows.Forms.ToolTipIcon.None;
            ni.BalloonTipTitle = "BMS2";

            if (context != IntPtr.Zero)
            {
                callbackThread              = new Thread(new ThreadStart(hook));
                callbackThread.Priority     = ThreadPriority.Highest;
                callbackThread.IsBackground = true;
                callbackThread.Start();
            }
            ch_work();
        }
예제 #11
0
        private void SendKey(Keys key, KeyState state)
        {
            try
            {
                Stroke    stroke    = new Stroke();
                KeyStroke keyStroke = new KeyStroke();

                keyStroke.Code  = key;
                keyStroke.State = state;

                stroke.Key = keyStroke;

                InterceptionDriver.Send(context, devk, ref stroke, 1);
            }
            catch { Stop(); }
        }
예제 #12
0
        public void WaitForCmd()
        {
            IntPtr ctx    = InterceptionDriver.CreateContext();
            int    device = 0;
            Stroke strk   = new Stroke();

            InterceptionDriver.SetFilter(ctx, InterceptionDriver.IsKeyboard, (int)KeyboardFilterMode.KeyDown | (int)KeyboardFilterMode.KeyUp);

            int i = 0;

            while (InterceptionDriver.Receive(ctx, device = InterceptionDriver.Wait(ctx), ref strk, 1) > 0)
            {
#if DEBUG
                Console.WriteLine((int)strk.Key.Code);
#endif
                if (strk.Key.Code == (Keys)powerKey)
                {
                    ++i;
                    if (i < 2)
                    {
                        continue;
                    }
                    lock (thisLock)
                    {
                        if (IsRunning())
                        {
                            Stop();

#if DEBUG
                            Console.WriteLine("Stop");
#endif
                        }
                        else
                        {
                            Start();

#if DEBUG
                            Console.WriteLine("Start");
#endif
                        }
                    }
                    i = 0;
                }
                InterceptionDriver.Send(ctx, device, ref strk, 1);
            }
        }
예제 #13
0
        private void Start()
        {
            context = InterceptionDriver.CreateContext();

            if (context != IntPtr.Zero)
            {
                callbackThread = new Thread(new ThreadStart(hook))
                {
                    Priority     = ThreadPriority.Highest,
                    IsBackground = true
                };
                callbackThread.Start();
            }

            Thread tmr = new Thread(_timer)
            {
                IsBackground = true
            };

            tmr.Start();
        }
예제 #14
0
        private void SendMouseEvent(MouseState state)
        {
            try
            {
                Stroke      stroke      = new Stroke();
                MouseStroke mouseStroke = new MouseStroke();

                mouseStroke.State = state;

                if (state == MouseState.ScrollUp)
                {
                    mouseStroke.Rolling = 120;
                }
                else if (state == MouseState.ScrollDown)
                {
                    mouseStroke.Rolling = -120;
                }

                stroke.Mouse = mouseStroke;

                InterceptionDriver.Send(context, devm, ref stroke, 1);
            }
            catch { Stop(); }
        }
예제 #15
0
        private void hook()
        {
            bool s  = true;
            bool l1 = true;
            bool l2 = true;

            Stroke stroke = new Stroke();

            InterceptionDriver.SetFilter(context, InterceptionDriver.IsKeyboard, (Int32)KeyboardFilterMode.All);
            InterceptionDriver.SetFilter(context, InterceptionDriver.IsMouse, (Int32)MouseFilterMode.All);

            //keyboard: device=3
            //mouse: device=11

            while (InterceptionDriver.Receive(context, device = InterceptionDriver.Wait(context), ref stroke, 1) > 0)
            {
                s = true;
                if (InterceptionDriver.IsMouse(device) > 0)
                {
                    if (l1)
                    {
                        l1   = false;
                        devm = device;
                        Dispatcher.Invoke(() => { label1.Content = device; });
                    }

                    if (OnMousePressed != null)
                    {
                        var args = new MousePressedEventArgs()
                        {
                            X = stroke.Mouse.X, Y = stroke.Mouse.Y, State = stroke.Mouse.State, Rolling = stroke.Mouse.Rolling
                        };
                        OnMousePressed(this, args);

                        if (args.Handled)
                        {
                            continue;
                        }
                        stroke.Mouse.X       = args.X;
                        stroke.Mouse.Y       = args.Y;
                        stroke.Mouse.State   = args.State;
                        stroke.Mouse.Rolling = args.Rolling;
                    }
                }

                if (InterceptionDriver.IsKeyboard(device) > 0)
                {
                    if (l2)
                    {
                        l2   = false;
                        devk = device;
                        Dispatcher.Invoke(() => { label2.Content = device; });
                    }

                    if (stroke.Key.Code == Keys.Tilde)
                    {
                        if (work)
                        {
                            if (bss)
                            {
                                if (stroke.Key.State == KeyState.Down)
                                {
                                    if (mss == false)
                                    {
                                        m   = false;
                                        mss = true;
                                        new Thread(macross).Start();
                                    }
                                }
                                else
                                if (stroke.Key.State == KeyState.Up)
                                {
                                    if (mss == true)
                                    {
                                        mss = false;
                                    }
                                }

                                s = false;
                            }
                        }
                    }

                    if (stroke.Key.Code == Keys.One)
                    {
                        if (work)
                        {
                            if (bs1)
                            {
                                if (stroke.Key.State == KeyState.Down)
                                {
                                    m = false;
                                }
                            }
                        }
                    }

                    if (stroke.Key.Code == Keys.Z)
                    {
                        if (work)
                        {
                            if (bs2)
                            {
                                if (stroke.Key.State == KeyState.Down)
                                {
                                    m = false;
                                }
                            }
                        }
                    }

                    if (stroke.Key.Code == Keys.Three)
                    {
                        if (work)
                        {
                            if (bs3)
                            {
                                if (stroke.Key.State == KeyState.Down)
                                {
                                    m = false;
                                }
                            }
                        }
                    }

                    if (stroke.Key.Code == Keys.Four)
                    {
                        if (work)
                        {
                            if (bs4)
                            {
                                if (stroke.Key.State == KeyState.Down)
                                {
                                    m = false;
                                }
                            }
                        }
                    }

                    if (stroke.Key.Code == Keys.R)
                    {
                        if (work)
                        {
                            if (bpw)
                            {
                                if (stroke.Key.State == KeyState.Up)
                                {
                                    if (m == false)
                                    {
                                        m = true;
                                        if (mt)
                                        {
                                            new Thread(macro_text).Start();
                                        }
                                        else
                                        {
                                            new Thread(macro).Start();
                                        }
                                    }
                                    else
                                    {
                                        m = false;
                                    }
                                }
                            }
                            else
                            {
                                if (stroke.Key.State == KeyState.Down)
                                {
                                    if (m == false)
                                    {
                                        m = true;
                                        if (mt)
                                        {
                                            new Thread(macro_text).Start();
                                        }
                                        else
                                        {
                                            new Thread(macro).Start();
                                        }
                                    }
                                }
                                else
                                if (stroke.Key.State == KeyState.Up)
                                {
                                    if (m == true)
                                    {
                                        m = false;
                                    }
                                }
                            }

                            s = false;
                        }
                    }

                    if (stroke.Key.Code == Keys.T)
                    {
                        if (work)
                        {
                            if (bt)
                            {
                                if (stroke.Key.State == KeyState.Down)
                                {
                                    if (m2 == false)
                                    {
                                        m  = false;
                                        m2 = true;
                                        new Thread(macro2).Start();
                                    }
                                }
                                else
                                if (stroke.Key.State == KeyState.Up)
                                {
                                    if (m2 == true)
                                    {
                                        m2 = false;
                                    }
                                }

                                s = false;
                            }
                        }
                    }

                    if (stroke.Key.Code == Keys.Insert)
                    {
                        if (stroke.Key.State == KeyState.E0)
                        {
                            if (pause_exit)
                            {
                                Stop();
                            }
                            new Thread(timer).Start();
                            new Thread(ch_work).Start();
                            s = false;
                        }
                    }

                    if (stroke.Key.Code == Keys.Enter)
                    {
                        if (stroke.Key.State == KeyState.Down)
                        {
                            if (work == true)
                            {
                                new Thread(ch_work).Start();
                                m = false;
                            }
                        }
                    }

                    if (OnKeyPressed != null)
                    {
                        var args = new KeyPressedEventArgs()
                        {
                            Key = stroke.Key.Code, State = stroke.Key.State
                        };
                        OnKeyPressed(this, args);

                        if (args.Handled)
                        {
                            continue;
                        }
                        stroke.Key.Code  = args.Key;
                        stroke.Key.State = args.State;
                    }
                }

                if (s)
                {
                    InterceptionDriver.Send(context, device, ref stroke, 1);
                }
            }

            Stop();
        }
예제 #16
0
        private void hook()
        {
            bool s      = true;
            bool l1     = true;
            bool l2     = true;
            bool winkey = false;

            Stroke stroke = new Stroke();

            InterceptionDriver.SetFilter(context, InterceptionDriver.IsMouse, (Int32)MouseFilterMode.All);
            InterceptionDriver.SetFilter(context, InterceptionDriver.IsKeyboard, (Int32)KeyboardFilterMode.All);

            while (InterceptionDriver.Receive(context, device = InterceptionDriver.Wait(context), ref stroke, 1) > 0)
            {
                s = true;
                if (InterceptionDriver.IsMouse(device) > 0)
                {
                    if (l1)
                    {
                        l1   = false;
                        devm = device;
                    }
                    user_out = false;
                    if (_lock)
                    {
                        s = false;
                    }

                    if (OnMousePressed != null)
                    {
                        var args = new MousePressedEventArgs()
                        {
                            X = stroke.Mouse.X, Y = stroke.Mouse.Y, State = stroke.Mouse.State, Rolling = stroke.Mouse.Rolling
                        };
                        OnMousePressed(this, args);

                        if (args.Handled)
                        {
                            continue;
                        }
                        stroke.Mouse.X       = args.X;
                        stroke.Mouse.Y       = args.Y;
                        stroke.Mouse.State   = args.State;
                        stroke.Mouse.Rolling = args.Rolling;
                    }
                }

                if (InterceptionDriver.IsKeyboard(device) > 0)
                {
                    if (l2)
                    {
                        l2   = false;
                        devk = device;
                    }

                    if (_lock)
                    {
                        s = false;
                    }

                    user_out = false;

                    if ((stroke.Key.Code == Keys.WindowsKeyLeft) | (stroke.Key.Code == Keys.WindowsKeyRight))
                    {
                        if (stroke.Key.State == KeyState.E0)
                        {
                            winkey = true;
                        }
                        else
                        {
                            winkey = false;
                        }
                    }

                    if (stroke.Key.Code == Keys.NumpadMinus)
                    {
                        try
                        {
                            Thread blk = new Thread(block);
                            blk.IsBackground = true;
                            blk.Start();

                            s = false;
                        }
                        catch { System.Windows.MessageBox.Show("Error"); }
                    }

                    if (stroke.Key.Code == Keys.Q)
                    {
                        if (winkey)
                        {
                            try
                            {
                                Thread unblk = new Thread(unblock);
                                unblk.IsBackground = true;
                                unblk.Start();
                            }
                            catch { System.Windows.MessageBox.Show("Error"); }
                        }
                    }

                    if (OnKeyPressed != null)
                    {
                        var args = new KeyPressedEventArgs()
                        {
                            Key = stroke.Key.Code, State = stroke.Key.State
                        };
                        OnKeyPressed(this, args);

                        if (args.Handled)
                        {
                            continue;
                        }
                        stroke.Key.Code  = args.Key;
                        stroke.Key.State = args.State;
                    }
                }

                if (s)
                {
                    InterceptionDriver.Send(context, device, ref stroke, 1);
                }
            }

            Stop();
        }
        public static void InstallBuiltInDrivers()
        {
            if (!IsInterceptionInstalled)
            {
                LogWriter.Write("Installing interception driver");
                var path = KeyboardSplitter.Helpers.ResourceExtractor.ExtractResource(
                    Assembly.GetExecutingAssembly(),
                    "KeyboardSplitter.Lib.keyboard_driver.exe");

                LogWriter.Write(InterceptionDriver.Install(path));
            }
            else
            {
                LogWriter.Write("Skipping interception driver installation, because it is already installed");
            }

            if (!IsVirtualXboxBusInstallled)
            {
                LogWriter.Write("Installing virtual xbox bus (SCP) driver");
                LogWriter.Write(VirtualXboxBus.Install());
            }
            else
            {
                LogWriter.Write("Skipping virtual xbox bus (SCP) driver installation, because it is already installed");
            }

            var driverState = InterceptionDriver.DriverState;

            // Original Messagebox is allowed here, because Interception is not loaded yet.
            switch (driverState)
            {
            case InterceptionDriverState.Installed:
            {
                MessageBox.Show(
                    "Installation finished, please start the application again.",
                    ApplicationInfo.AppNameVersion,
                    MessageBoxButton.OK,
                    MessageBoxImage.Information);

                LogWriter.Write("Installtion OK. Closing the application, because it needs to be restarted.");
                Environment.Exit(0);
            }

            break;

            case InterceptionDriverState.NotInstalled:
            {
                MessageBox.Show(
                    "Built-In Drivers Installation failed!",
                    ApplicationInfo.AppNameVersion,
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);

                LogWriter.Write("Installation Error: Interception driver reports it is not installed");
                Environment.Exit(0);
            }

            break;

            case InterceptionDriverState.RebootRequired:
            {
                var result = MessageBox.Show(
                    "Built-in drivers installation finished.\r\n" +
                    "In order to use them, you should reboot your PC.\r\n\r\n" +
                    "Do you want to reboot now?",
                    "Reboot required",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Question);

                LogWriter.Write("Installation finishes, but system reboot is required.");

                if (result == MessageBoxResult.Yes)
                {
                    try
                    {
                        System.Diagnostics.Process.Start("shutdown.exe", "-r -t 0");
                    }
                    catch (Exception)
                    {
                        Controls.MessageBox.Show(
                            "Reboot command failed! Please reboot manually!",
                            ApplicationInfo.AppNameVersion,
                            MessageBoxButton.OK,
                            MessageBoxImage.Hand);
                    }
                }

                Environment.Exit(0);
            }

            break;

            default:
                throw new NotImplementedException(
                          "Not implemented driver state: " + driverState);
            }
        }
예제 #18
0
        private void ClickerInputEventLoop()
        {
            // load the list of device strings to test
            if (!File.Exists(DEVICES_LIST_FILE))
            {
                // write defaults
                string[] createText = { "HID\\VID_046D&PID_C540" };
                File.WriteAllLines(DEVICES_LIST_FILE, createText);
            }

            string[] deviceStringListToTest = File.ReadAllLines(DEVICES_LIST_FILE);

            // initialize all available 'clicker tagets'
            Type[] typelist = GetClassesInNamespace(Assembly.GetExecutingAssembly(), "ClickerFixer.ClickerTargets");
            for (int i = 0; i < typelist.Length; i++)
            {
                _targets.Add((IClickerTarget)Activator.CreateInstance(typelist[i]));
            }

            // initialize Interception driver
            try
            {
                _inteceptionContext = InterceptionDriver.CreateContext();
            }
            catch (DllNotFoundException e)
            {
                MessageBox.Show($"Failed to load Interception. Please copy interception.dll into {System.Environment.CurrentDirectory}\n\n" + e.Message);
                Application.Exit();
                return;
            }
            catch (Exception e)
            {
                MessageBox.Show("Failed to load Interception (interception.dll). Ensure driver installed, and you rebooted after installation.\n" + e.Message);
                Application.Exit();
                return;
            }

            if (_inteceptionContext == IntPtr.Zero)
            {
                MessageBox.Show("Failed to load Interception (interception.dll). Ensure driver installed, and you rebooted after installation.");
                Application.Exit();
                return;
            }

            InterceptionDriver.SetFilter(_inteceptionContext, InterceptionDriver.IsKeyboard, (Int32)KeyboardFilterMode);

            Stroke stroke = new Stroke();

            CancellationToken token = _cancelSource.Token;

            while (!token.IsCancellationRequested && InterceptionDriver.Receive(_inteceptionContext, deviceId = InterceptionDriver.Wait(_inteceptionContext), ref stroke, 1) > 0)
            {
                bool   isHandled           = false;
                string handledByClientName = null;

                if (InterceptionDriver.IsKeyboard(deviceId) > 0 && stroke.Key.State == KeyState.E0)
                {
                    Console.WriteLine($"{InterceptionDriver.GetHardwareStr(_inteceptionContext, deviceId)} Code={stroke.Key.Code} State={stroke.Key.State}");
                    var deviceHwStr = InterceptionDriver.GetHardwareStr(_inteceptionContext, deviceId);

                    // only handle if string test passes:
                    if (Array.Find(deviceStringListToTest, p => deviceHwStr.StartsWith(p)) != null)
                    {
                        foreach (IClickerTarget client in _targets)
                        {
                            if (!client.IsActive())
                            {
                                continue;
                            }
                            else
                            {
                                handledByClientName = client.GetType().Name;

                                System.Diagnostics.Debug.WriteLine($"{handledByClientName} {stroke.Key.Code}");

                                if (stroke.Key.Code == Interception.Keys.Right)
                                {
                                    System.Diagnostics.Debug.WriteLine("Right");
                                    client.SendNext();
                                }
                                else if (stroke.Key.Code == Interception.Keys.Left)
                                {
                                    System.Diagnostics.Debug.WriteLine("Left");
                                    client.SendPrevious();
                                }

                                isHandled = true;
                                break;
                            }
                        }

                        OnKeyPressed(new KeyPressedHandledEventArgs(handledByClientName));
                    }
                }

                // pass-through if unhandled
                // (or your normal PC keyboard won't work!)
                if (!isHandled)
                {
                    InterceptionDriver.Send(_inteceptionContext, deviceId, ref stroke, 1);
                }
            }

            Abort();
        }
 private void InitKeyboardDriver()
 {
     CreateContext();
     InterceptionDriver.SetFilter(context, InterceptionDriver.IsKeyboard, (int)KeyboardFilterMode.All);
 }
 //PressKey메서드 실행전 반드시 실행해야함
 public void GetKeyboardId()
 {
     deviceId = InterceptionDriver.Wait(context);
 }
예제 #21
0
 public static int FilterMouse(int device)
 {
     return(InterceptionDriver.IsMouse(device) + (device != 13 ? 0 : 1));
 }