Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SendKeys.Send instead SendKeys.SendWait

SendKeys.Send instead SendKeys.SendWait

Scheduled Pinned Locked Moved C#
help
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M Riaz Bashir
    wrote on last edited by
    #1

    Hi, I am trying to send some text to notepad using SendKeys.Send("Hello World!") instead SendKeys.SendWait("Hello World!"). But I could not send and facing problem. Error is: "SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method." Thank you

    namespace Send_Key
    {
    class Program
    {
    [DllImport("user32.dll", SetLastError = true, EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll", SetLastError = true, EntryPoint = "SetForegroundWindow")]
    static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll", SetLastError = true, EntryPoint = "ShowWindow")]
    static extern bool ShowWindow(IntPtr hWnd, int cmCommand);
    static IntPtr hwnd;

        static void Main(string\[\] args)
        {
            Process notePad = new Process();
            notePad.StartInfo.FileName = "notepad.exe";
            notePad.Start();
    
            System.Threading.Thread.Sleep(1000);
    
            SetForegroundWindow(hWnd);
    
            System.Threading.Thread.Sleep(1000);
            SendKeys.Send("Hello World!");
        }
    }
    

    }

    B D 2 Replies Last reply
    0
    • M M Riaz Bashir

      Hi, I am trying to send some text to notepad using SendKeys.Send("Hello World!") instead SendKeys.SendWait("Hello World!"). But I could not send and facing problem. Error is: "SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method." Thank you

      namespace Send_Key
      {
      class Program
      {
      [DllImport("user32.dll", SetLastError = true, EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
      static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
      [DllImport("user32.dll", SetLastError = true, EntryPoint = "SetForegroundWindow")]
      static extern bool SetForegroundWindow(IntPtr hWnd);
      [DllImport("user32.dll", SetLastError = true, EntryPoint = "ShowWindow")]
      static extern bool ShowWindow(IntPtr hWnd, int cmCommand);
      static IntPtr hwnd;

          static void Main(string\[\] args)
          {
              Process notePad = new Process();
              notePad.StartInfo.FileName = "notepad.exe";
              notePad.Start();
      
              System.Threading.Thread.Sleep(1000);
      
              SetForegroundWindow(hWnd);
      
              System.Threading.Thread.Sleep(1000);
              SendKeys.Send("Hello World!");
          }
      }
      

      }

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      You are not setting NotePad as the ForegroundWindow correctly. Try:

      SetForegroundWindow(notePad.MainWindowHandle);

      Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

      1 Reply Last reply
      0
      • M M Riaz Bashir

        Hi, I am trying to send some text to notepad using SendKeys.Send("Hello World!") instead SendKeys.SendWait("Hello World!"). But I could not send and facing problem. Error is: "SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method." Thank you

        namespace Send_Key
        {
        class Program
        {
        [DllImport("user32.dll", SetLastError = true, EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", SetLastError = true, EntryPoint = "SetForegroundWindow")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll", SetLastError = true, EntryPoint = "ShowWindow")]
        static extern bool ShowWindow(IntPtr hWnd, int cmCommand);
        static IntPtr hwnd;

            static void Main(string\[\] args)
            {
                Process notePad = new Process();
                notePad.StartInfo.FileName = "notepad.exe";
                notePad.Start();
        
                System.Threading.Thread.Sleep(1000);
        
                SetForegroundWindow(hWnd);
        
                System.Threading.Thread.Sleep(1000);
                SendKeys.Send("Hello World!");
            }
        }
        

        }

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Your Thread.Sleep lines can be removed. You don't need them. Also, the numbers you have in there are just arbitrary and make certain assumptions about the performance if the system. A better way to wait for Notepad to launch is:

        notePad.WaitForInputIdle();
        

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups