Search code examples
c#eventssendkeysraw-inputkeystroke

Sendkeys infinite loop


I'm using RawInput methods to intercept global keyboard keystrokes then send a string to the focused foregroundwindow, my problem is when i use sendkeys.send() i'm getting an infinite loop (the event is triggered each time)

i've tried to delete the event then add it but didn't work, i found on some topics that the problem appears because the place where i sent the string still focused but i need it to still focused, e.g. of what i'm doing :

  • user press numpad0 in facebook messenger
  • i remplace the numpad0 with my own string

UPDATED

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

Solution

  • it works fine by using InputSimulator instead of 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;