2

I have an application to which I want to send the Ctrl-C keys combination. I am trying with SendMesssage, but I know that the application checks for Ctrl-C combination using GetKeyState and GetAsyncKeyState so SendMessage is pretty useless... How can I send the Ctrl-C combination to this window without calling SetForegroundWindow(hWnd)? I need a solution which works without focusing/bringing to front the window.

I am temporarily using this code (but requires focus):

SetForegroundWindow(hWnd);
SendKeys.SendWait("^(c)");

I am using C#, but C++ code is ok.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
Darxis
  • 1,482
  • 1
  • 17
  • 37
  • I believe this question has already been answered. See http://stackoverflow.com/questions/11512373/findwindow-and-setforegroundwindow-alternatives – barrypicker Aug 29 '13 at 01:02
  • @barrypicker I need a solution which works without focusing/bringing to front the window. – Darxis Aug 29 '13 at 01:04
  • @Darxis Take a look at this: http://stackoverflow.com/questions/813086/can-i-send-a-ctrl-c-sigint-to-an-application-on-windows – Peter L. Aug 29 '13 at 01:09
  • @PeterL. I don't want to terminate the app by using Ctrl-C. I want to send a Ctrl-C on a window handle, which then copy something to clipboard. WM_COPY and other messages don't work as the app checks by GetKeyState – Darxis Aug 29 '13 at 01:14
  • `SendMessage` with some `WM_KEY*`s? – chris Aug 29 '13 at 01:20
  • @chris GetKeyState doesn't work with SendMessage – Darxis Aug 29 '13 at 01:28
  • @Darxis, Oh, I see. Are you *sure* the app checks like that? It doesn't seem too normal (why activate a copy when your window isn't active). If it was truly using `GetKeyState`, it should detect a simple `SendInput`. – chris Aug 29 '13 at 01:33
  • @chris Yes I'am sure this app check by GetKeyState, I used a debugger, I am 100% sure. I just want to automize the Copy command. I read about SendInput, but it requires a focus on the window. – Darxis Aug 29 '13 at 11:19
  • `GetKeyState` doesn't require your window to be focused, though. That's why sending it just like a user pressing it should work if the app is using `GetKeyState`. – chris Aug 29 '13 at 12:25
  • @chris Yes, but SendMessage doesn't trigger GeyKeyState. If I were to use SendInput which will probably work, I would have to focus the window first before using SendInput. SendInput cannot take as a parameter a window handle. – Darxis Aug 29 '13 at 13:05
  • 2
    I just tried having a thread that waits for the space key to be pressed with a looped `GetAsyncKeyState` (async because I have no message loop, but almost the same thing). My main thread waits three seconds and uses `SendInput` to send a space. My other thread definitely catches it when the window isn't focused. – chris Aug 29 '13 at 13:21
  • @chris Yes, you're right, my mistake. So I have to Return into my debugger because with SendInput when the window is not focused, it doesn't work – Darxis Aug 29 '13 at 13:34
  • I also would like to send the Ctrl-C ONLY to this application, it shouldn't be visible to other windows like SendMessage do. – Darxis Aug 29 '13 at 13:36
  • And what is strange, the app reacts to SendMessage with WM_RBUTTONDOWN, WM_RBUTTONUP, WM_LBUTTONDOWN, WM_LBUTTONUP... – Darxis Aug 29 '13 at 13:40
  • Without activating that app or a window of your own, something else most likely has to get those keystrokes. I don't think it's possible to set off `GetKeyState` without doing that. – chris Aug 29 '13 at 13:47

1 Answers1

1

A long time ago, back when I was doing Sys Admin automation.. I used AutoIT. It's been a long time, but if you don't mind picking up their .dll...

This is the method I'd use.. it mentions you can send directly to a window/control without focus.. in some cases you can't..

AutoIT ControlSend Method

Slack Shot
  • 1,090
  • 6
  • 10