Using SendMessage to simulate the ENTER key press

  • Thread starter Thread starter John Dalberg
  • Start date Start date
J

John Dalberg

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
 
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 said:
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]
- (e-mail address removed)

John Dalberg said:
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
 
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]
- (e-mail address removed)

John Dalberg said:
Nicholas Paldino said:
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]
- (e-mail address removed)

John Dalberg said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top