Sendkeys无限循环

时间:2018-08-11 21:22:20

标签: c# events sendkeys raw-input keystroke

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

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

  • 用户在Facebook Messenger中按numpad0
  • 我用自己的字符串替换numpad0

已更新

    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);

1 个答案:

答案 0 :(得分:0)

使用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;