Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Sending keystrokes to a webbrowser object.

$
0
0
I've been frustratingly trying to send keystroes to a webbrowser object in C#. But the catch is, is that It needs to continue functioning if the parent window loses focus. In short I can't just use the SendKeys method.

Here's what I have so far:

        private void button1_Click(object sender, EventArgs e)
        {
            sendKeystroke();
        }

        [DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        public void sendKeystroke()
        {
            const uint WM_KEYDOWN = 0x100;
            const uint WM_KEYUP = 0x0101;
  
            IntPtr edit = webBrowser1.Handle;

            PostMessage(edit, WM_KEYDOWN, (IntPtr)(Keys.A), IntPtr.Zero);
            PostMessage(edit, WM_KEYUP, (IntPtr)(Keys.A), IntPtr.Zero);
        }



This works when I switch the window handle to putty so I might be able to use it if I can get the browser to accept the messages posted to it.

Any suggestions? Could really use some help.

Thanks.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>