By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,050 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,050 IT Pros & Developers. It's quick & easy.

Using SendMessage to simulate the ENTER key press

P: n/a
I have an app that keeps popping up a windows with a 'Yes' or 'OK' button
on it. I am trying to write a little app that automates hitting the enter
key so I don't have to do it myself.

I used the FindWindow method to get a handle of the window and then issued:
SendMessage(hwnd, 273, 0, IntPtr.Zero)

to send an ENTER key to it but it didn't work.

If I need to get the handle of the 'OK' button, how do I do it? (How do you
loop through the buttons and find the handle you need?) If I just need to
send an ENTER key to the window, is the method above correct. hwnd is the
handle of the popup window and 273 is the decimal value for ENTER (0x111).

Thanks.

John Dalberg
Sep 26 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
John,

You should not use SendMessage to simulate user input. Rather, take a
look at the SendInput API function to simulate user input (key presses,
mouse input, etc, etc).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Dalberg" <no****@nospam.ssswrote in message
news:20*******************@newsreader.com...
>I have an app that keeps popping up a windows with a 'Yes' or 'OK' button
on it. I am trying to write a little app that automates hitting the enter
key so I don't have to do it myself.

I used the FindWindow method to get a handle of the window and then
issued:
SendMessage(hwnd, 273, 0, IntPtr.Zero)

to send an ENTER key to it but it didn't work.

If I need to get the handle of the 'OK' button, how do I do it? (How do
you
loop through the buttons and find the handle you need?) If I just need to
send an ENTER key to the window, is the method above correct. hwnd is the
handle of the popup window and 273 is the decimal value for ENTER (0x111).

Thanks.

John Dalberg

Sep 26 '06 #2

P: n/a
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote:
John,

You should not use SendMessage to simulate user input. Rather, take
a look at the SendInput API function to simulate user input (key presses,
mouse input, etc, etc).

Why shouldn't I use SendMessage?

>
Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Dalberg" <no****@nospam.ssswrote in message
news:20*******************@newsreader.com...
I have an app that keeps popping up a windows with a 'Yes' or 'OK'
button
on it. I am trying to write a little app that automates hitting the
enter key so I don't have to do it myself.

I used the FindWindow method to get a handle of the window and then
issued:
SendMessage(hwnd, 273, 0, IntPtr.Zero)

to send an ENTER key to it but it didn't work.

If I need to get the handle of the 'OK' button, how do I do it? (How do
you
loop through the buttons and find the handle you need?) If I just need
to send an ENTER key to the window, is the method above correct. hwnd
is the handle of the popup window and 273 is the decimal value for
ENTER (0x111).

Thanks.

John Dalberg
Sep 26 '06 #3

P: n/a
John,

When you click a key on the keyboard, it sends a key down, a key press,
and a key up message. You are only sending one message and it doesn't look
right to the code that is trying to interpret it.

If you use SendInput, it will send all the appropriate windows messages
associated with a key click.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Dalberg" <no****@nospam.ssswrote in message
news:20*******************@newsreader.com...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote:
>John,

You should not use SendMessage to simulate user input. Rather, take
a look at the SendInput API function to simulate user input (key presses,
mouse input, etc, etc).


Why shouldn't I use SendMessage?

>>
Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Dalberg" <no****@nospam.ssswrote in message
news:20*******************@newsreader.com...
>I have an app that keeps popping up a windows with a 'Yes' or 'OK'
button
on it. I am trying to write a little app that automates hitting the
enter key so I don't have to do it myself.

I used the FindWindow method to get a handle of the window and then
issued:
SendMessage(hwnd, 273, 0, IntPtr.Zero)

to send an ENTER key to it but it didn't work.

If I need to get the handle of the 'OK' button, how do I do it? (How do
you
loop through the buttons and find the handle you need?) If I just need
to send an ENTER key to the window, is the method above correct. hwnd
is the handle of the popup window and 273 is the decimal value for
ENTER (0x111).

Thanks.

John Dalberg

Sep 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.