views:

410

answers:

3

Hello everyone,

In Google Chrome you can retrieve the text of a JavaScript alert/dialog by simply having the dialog open and doing CTRL-C on the dialog. This will put the text in the dialog into the clipboard.

I'm trying to use SendMessage to send CTRL-C to perform a copy on the text of a JavaScript dialog. I've already managed to do this using SendInput but unfortunately that could fail if the window doesn't have focus.

I have tried using SendMessage with WM_COPY message but that didn't work for my needs.

Please if anyone has done this before successfully using SendMessage I would greatly appreciate your help.

A: 

Try using these

WM_CUT = 0x300
WM_COPY = 0x301
WM_PASTE = 0x302
WM_CLEAR = 0x303

as the wmsg param depending on your operation.

Ian Davis
I mentioned in the question that I tried WM_COPY. Unfortunately it didn't work for my needs. I need to be able to perform the keystroke but through SendMessage calls.
gtaborga
@gtaborga: it is the right answer. You just don't send it to the correct window.
Hans Passant
A: 

Since the question is a bit vague as to whether you are talking about sending a Ctrl+C keystroke to terminate a process, or to copy a selection to the clipboard. If it is the former, please see this answer here and here on SO which was asked on a previous occasion, where an OP was asking how to send a Ctrl+C keystrokes to a shell process in order to terminate it.

If you are talking about Ctrl+C for copying a selection to the clipboard, that is even trickier as to whether the selection has indeed being selected first prior to copying to the clipboard...

If I am going off the wrong angle, at least, please edit your question to make it more clearer!

tommieb75
I apologize for the vagueness of the question. I'm indeed trying to perform a copy; however, it is being done on the dialog content of a javascript dialog in Google Chrome. If you simply have a Google Chrome dialog selected and perform CTRL-C using keystrokes it actually copies the entire text of the dialog. That is what I'm trying to perform. It requires that no text be selected for it to work. All that is needed is for the keystroke to be performed.
gtaborga
A: 

If WM_COPY fails, there are a few avenues you could try...

To fake a keypress you can send WM_KEYDOWN and WM_KEYUP messages.

However, what do you send in the message? If you send a "c", then there is no way in these messages for you to inform the application that ctrl was also held down.

You may be able to send a character 0x03 (which is the character code that ctrl+c actually generates), but there is no guarantee that the receiving application will interpret this as a "ctrl+c" action.

Why might it not work? The receiving application might...

  • ignore WM_KEYDOWN and WM_KEYUP and use other means to read the keyboard (e.g. GetAsyncKeyState to see if a key is down)
  • handle WM_KEYDOWN and/or WM_KEYUP messages, but look for "c" and then use GetAsyncKeyState() or similar to detect if ctrl is down when the message is processed.
  • It might still ignore the messages if it doesn't have input focus, or worse, action those messages as though they were received through the input focus window.

So - give it a try, but it may not work.

Alternatively, if SendInput works, then you could just force the input focus to the correct control, SendInput, and then restore the input focus to its previous location.

Another approach (possibly the best one) is if it is a dialog known to you and it contains a static text field, you may just be able to find that child control and GetText on it (send WM_GETTEXT message), and avoid using the clipboard at all. (Or if you need the text on the clipboard, get it like this and then place it on the clipboard yourself). This would avoid tricking the application into providing the text, and fall back to standard Windows behaviour.

Jason Williams
Thank you for your response. I will try using WM_KEYDOWN AND WM_KEYUP with 0x03. Please read my updated question for more detail on what I'm trying to do. Unfortunately WM_GETTEXT isn't working because the dialog isn't exposing the text. I tried accessing it through Spy++. The only thing I can retrieve is the title of the dialog which isn't what I'm looking for.
gtaborga