Ejemplo n.º 1
0
        public override void Execute()
        {
            if(SearchEngineUrl ==null || !Uri.IsWellFormedUriString(PopulateSearchEngingUrl("Nothing"),UriKind.Absolute)) return;

            var t = new Thread(() =>
            {

                var clipboardMonitor = new ClipboardMonitor();

                clipboardMonitor.MonitorRegistered += () =>
                {
                    var timer = new Timer() { Interval = 2000 };
                    timer.Tick += (sender, args) =>
                    {
                        Debug.WriteLine("WebSearchCommand Timeout!");
                        timer.Enabled = false;

                        clipboardMonitor.StopMonitor();
                        clipboardMonitor.DestroyHandle();
                        Application.ExitThread();

                        Debug.WriteLine("超时结束 WebSearchCommand Runloop");

                    };

                    timer.Enabled = true;

                    try
                    {
                        Sim.KeyDown(VirtualKeyCode.CONTROL);
                        Sim.KeyDown(VirtualKeyCode.VK_C);

                        Sim.KeyUp(VirtualKeyCode.VK_C);
                        Sim.KeyUp(VirtualKeyCode.CONTROL);
                        //_sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, new[] { VirtualKeyCode.VK_C });
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("发送按键失败:" + ex);
                        Native.TryResetKeys(new []{VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C});
            #if DEBUG
                        //throw;
            #endif
                    }
                };

                clipboardMonitor.ClipboardUpdated += args =>
                {
                    Debug.WriteLine("ClipboardChanged");
                    args.Handled = true;

                    var text = "";
                    if (Clipboard.ContainsText() && (text = Clipboard.GetText().Trim()).Length > 0)
                    {
                        //如果是URL则打开,否则搜索
                        if (Uri.IsWellFormedUriString(text, UriKind.Absolute))
                        {
                            using(Process.Start("explorer.exe", "\"" + text + "\""));
                        }
                        else
                        {
                            if (text.Length > 100) text = text.Substring(0, 100);
                            var url = "\"" + PopulateSearchEngingUrl(text) + "\"";
                            var startInfo = new ProcessStartInfo("explorer.exe", url);
                            using (Process.Start(startInfo));
                        }
                    }

                    clipboardMonitor.StopMonitor();
                    clipboardMonitor.DestroyHandle();

                    //GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);

                    Application.ExitThread();
                };

                clipboardMonitor.StartMonitor();

                Application.Run();
                Debug.WriteLine("Thread End?");

            }) {Name = "WebSearchCommand Runloop(Bug!我不应该存在!)"};

            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;
            t.Start();
        }
Ejemplo n.º 2
0
        public override void Execute()
        {
            if(SearchEngineUrl ==null || !Uri.IsWellFormedUriString(PopulateSearchEngingUrl("Nothing"),UriKind.Absolute)) return;

            var t = new Thread(() =>
            {

                var clipboardMonitor = new ClipboardMonitor();

                clipboardMonitor.MonitorRegistered += () =>
                {
                    var timer = new Timer() { Interval = 2000 };
                    timer.Tick += (sender, args) =>
                    {
                        Debug.WriteLine("WebSearchCommand Timeout!");
                        timer.Enabled = false;

                        clipboardMonitor.StopMonitor();
                        clipboardMonitor.DestroyHandle();
                        Application.ExitThread();
                        
                        Debug.WriteLine("超时结束 WebSearchCommand Runloop");

                    };

                    timer.Enabled = true;

                    try
                    {
                        Sim.KeyDown(VirtualKeyCode.CONTROL);
                        Sim.KeyDown(VirtualKeyCode.VK_C);
                        
                        Sim.KeyUp(VirtualKeyCode.VK_C);
                        Sim.KeyUp(VirtualKeyCode.CONTROL);
                        //_sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, new[] { VirtualKeyCode.VK_C });
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("发送按键失败:" + ex);
                        Native.TryResetKeys(new []{VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C});
#if DEBUG
                        //throw;
#endif
                    }
                };

                clipboardMonitor.ClipboardUpdated += args =>
                {
                    Debug.WriteLine("ClipboardChanged");
                    args.Handled = true;

                    var text = "";
                    if (Clipboard.ContainsText() && (text = Clipboard.GetText().Trim()).Length > 0)
                    {
                        var browser = GetDefaultBrowserPath();
                        if (UseBrowser != null && File.Exists(UseBrowser.Replace("\"", "")) )
                        {
                            browser = UseBrowser;
                        }

                        string urlToOpen;
                        //如果是URL则打开,否则搜索
                        if (Uri.IsWellFormedUriString(text, UriKind.Absolute))
                        {
                            urlToOpen = text;
                        }
                        else
                        {
                            if (text.Length > 100) text = text.Substring(0, 100);
                            urlToOpen = PopulateSearchEngingUrl(text);
                        }
                        //M$ Edge Hack
                        if (browser.Contains("LaunchWinApp.exe"))
                        {
                            urlToOpen = "microsoft-edge:" + urlToOpen;
                            Process.Start(urlToOpen);
                        }else
                        {
                            var startInfo = new ProcessStartInfo(browser, "\"" + urlToOpen + "\"");
                            using (Process.Start(startInfo)) ;
                        }
                    }

                    clipboardMonitor.StopMonitor();
                    clipboardMonitor.DestroyHandle();

                    Application.ExitThread();
                };

                clipboardMonitor.StartMonitor();

                Application.Run();
                Debug.WriteLine("Thread End?");

            }) {Name = "WebSearchCommand Runloop(Bug!我不应该存在!)"};

            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;
            t.Start();
        }