Sendkeys problem from .NET program

Posted by user203123 on Stack Overflow See other posts from Stack Overflow or by user203123
Published on 2010-04-09T01:06:23Z Indexed on 2010/04/09 1:23 UTC
Read the original article Hit count: 365

Filed under:
|

THe code below I copied from MSDN with a bit of modification:

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
DllImport("User32")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
int cnt = 0;
private void button1_Click(object sender, EventArgs e)
{
     IntPtr calculatorHandle = FindWindow("Notepad", "Untitled - Notepad");
      if (calculatorHandle == IntPtr.Zero)
      {
          MessageBox.Show("Calculator is not running.");
          return;
      }
      SetForegroundWindow(calculatorHandle);
      SendKeys.SendWait(cnt.ToString());
      SendKeys.SendWait("{ENTER}");
      cnt++;
      SendKeys.Flush();
      System.Threading.Thread.Sleep(1000);
}

The problem is the number sequence in Notepad is not continuously. The first click always results 0 (as expected). but from the second click, the result is unpredictable (but the sequence is still in order, e.g. 3, 4, 5, 10, 14, 15, ....)

If I click the button fast enough, I was able to get the result in continuous order (0,1,2,3,4,....) but sometimes it produces more than 2 same numbers (e.g. 0,1,2,3,3,3,4,5,6,6,6,7,8,9,...)

© Stack Overflow or respective owner

Related posts about c#

Related posts about sendkeys

  • .NET sendkeys to calculator

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The sendkeys code below works well for Notepad but it doesn't work for Calculator. What is the problem? (It's another problem compared to what I sent here http://stackoverflow.com/questions/2604486/c-sendkeys-problem) [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr… >>> More

  • C# sendkeys to calculator

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The sendkeys code below works well for Notepad but it doesn't work for Calculator. What is the problem? (It's another problem compared to what I sent here http://stackoverflow.com/questions/2604486/c-sendkeys-problem) [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr… >>> More

  • Cisco Configuration backup with Windows Script.

    as seen on Server Fault - Search for 'Server Fault'
    We have a client with a lot of Cisco Devices and we would like to automate the backups of these devices through telnet. We have both 2003 and 2008 servers and ideally use tftp to back it up. I wrote this: Set WshShell = WScript.CreateObject("WScript.Shell") Dim fso Set fso = CreateObject("Scripting… >>> More

  • C# sendkeys problem

    as seen on Stack Overflow - Search for 'Stack Overflow'
    THe code below I copied from MSDN with a bit of modification: [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName,string lpWindowName); DllImport("User32")] public static extern bool SetForegroundWindow(IntPtr hWnd); int cnt = 0; private… >>> More

  • Sendkeys problem from .NET program

    as seen on Stack Overflow - Search for 'Stack Overflow'
    THe code below I copied from MSDN with a bit of modification: [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName,string lpWindowName); DllImport("User32")] public static extern bool SetForegroundWindow(IntPtr hWnd); int cnt = 0; private… >>> More