Sendkeys 无限循环

Sendkeys infinite loop

我正在使用 RawInput 方法来拦截全局键盘击键,然后将字符串发送到聚焦的前景窗口,我的问题是当我使用 sendkeys.send() 时我遇到了无限循环(每次都会触发事件)

我试过删除事件然后添加它但没有成功,我发现在某些主题上出现问题是因为我发送字符串的地方仍然集中但我需要它仍然集中,例如我在做什么:

已更新

    public static bool Keypressed = false;
    private void OnKeyPressed(object sender, RawInputEventArg e)
    {
        if (Keypressed){Keypressed=false;return;}

        if (cpt == 0)
        {
            cpt++;
            Console.WriteLine(e.KeyPressEvent.Name);
            //Check the Device name first !!!!
            //if(e.KeyPressEvent.Name.Contains(Settings.Default.DeviceName) || Settings.Default.DeviceName.Contains(e.KeyPressEvent.Name)) { }
            String ActiveProcess = ActiveApp.getActiveProccess();

            switch (ActiveProcess)
            {
                case "chrome":
                    if(API.getChromeUrl().Contains("facebook") || API.getChromeUrl().Contains("messenger"))
                    {

                        SendKeys.Send("1");
                        //Clipboard.SetText(""+Previous);
                        Console.WriteLine(API.getChromeUrl());
                        Keypressed = true;

                        // String Previous = Clipboard.GetText();
                        // Clipboard.SetText(FBEmo.numpad0);



                    }
                    break;
                case "mozzila":
                    break;
                case "Skype":
                    break;
                case "Viber":
                    break;


            }
            Console.WriteLine(ActiveProcess);

使用 InputSimulator 而不是 Sendkeys.Send() 效果很好。

InputSimulator cmd = new InputSimulator();

        if (e.KeyPressEvent.KeyPressState.Equals("BREAK"))
        {
            cpt = 0;
            Console.WriteLine(e.KeyPressEvent.Name);
            //Check the Device name first !!!!
            //if(e.KeyPressEvent.Name.Contains(Settings.Default.DeviceName) || Settings.Default.DeviceName.Contains(e.KeyPressEvent.Name)) { }
            String ActiveProcess = ActiveApp.getActiveProccess();

          switch (ActiveProcess)
            {
                case "chrome":
                    if(API.getChromeUrl().Contains("facebook") || API.getChromeUrl().Contains("messenger"))
                    {

                        switch (e.KeyPressEvent.VKeyName)
                        {
                            case "NUMPAD0":
                                cmd.Keyboard.TextEntry(FBEmo.numpad0);
                                break;