Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm sending "^c" to an external application, after finding window/class name of my target with Spy++, I have this :
C#
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
            public static extern IntPtr FindWindow(string lpClassName,
                string lpWindowName);

            [DllImport("USER32.DLL")]
            public static extern bool SetForegroundWindow(IntPtr hWnd);

    private void button1_Click(object sender, EventArgs e)
            {
                IntPtr newzieHandle= FindWindow("Afx:00400000:0", "Newzie");

                if (newzieHandle== IntPtr.Zero)
                {
                    MessageBox.Show("Unable to detect Newzie");
                    return;
                }
                SetForegroundWindow(newzieHandle);
                SendKeys.SendWait("^c");
            }

The problem is that the extrenal application already haves a default shortcut "ctrl+c" that can't be disabled! So is there a way to send the keys only to a comboBox and not to the whole application?
Image of Spy++ for my target application thread[^]
External app ScreenShot[^].
Update I tried a c# SendInput wrapper[^] and always have the same problem.
Posted
Updated 29-May-13 3:36am
v10

1 solution

This is a very unreliable technique. If you wan to simulate input, you should better use really low-level method SendInput:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^].

This is how to do it in .NET: http://www.pinvoke.net/default.aspx/user32.sendinput[^].

However, I should warn you: using simulation of the input (of any kind, starting from the lame .NET SendKeys to powerful SendInput) for "regular" UI development would be a huge abuse. The usual .NET FCL would be quite sufficient and compatible way. Simulation of input should be used only in very special utilities. Examples include playing of keyboard/mouse macro, UI testing utilities, virtual keyboard, something like that.

—SA
 
Share this answer
 
Comments
Nnorss 29-May-13 3:43am    
Hello Sergey! Actually I'm working on an add-on for a Rss feed agregator application , when the user select the Url text from the navigation bar of the other application and click the button of my little sized form, the highlited link must be copyed to the clipboard. Should I use "SendInput" for my case? How can I use the usual .NET FCL to realize what I covet?
Sergey Alexandrovich Kryukov 29-May-13 8:30am    
No, why would you eve simulate input? Input in what program? If you explain the behavior in sufficient detail, perhaps I can answer.
—SA
Nnorss 29-May-13 9:16am    
Well, when i Send "Ctrl+N" , I get response from the app, a new windows for adding new feed is opened since "Ctrl+N" is a default shortcut for adding a new feed in this app. But when I send "Ctrl+C" or "a" or "b" or another character... I can't get any response and nothing happens. It seems like the selected zone become deselected when I click my button form, or like I'm not sending input to the correct control. (I added a ScreenShot of the app interface).
Sergey Alexandrovich Kryukov 29-May-13 9:28am    
You would nee to use SendInput, starting with activation of the window of this application, focusing the control you need to operate, all as an interactive user would do.

Generally, controlling the other application with simulated keyboard or mouse input is a bad business. I wonder why doing so. If this is just RSS, wouldn't it be better to create a separate application doing it all legitimately, say, using System.Net.HttpWebRequest? Please see:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

—SA
Nnorss 29-May-13 9:45am    
Actually I can't create a new separate app even if it's just Rss, apparently this agregator is highly developped and very strong for new content notification... I will try to use Sendinput to interact with the app as an interactive user would do :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900