is there any way to simulate Ctrl+C command in delphi ? the problem is i want that from another application for example copy a text from Notepad after select the target text .
-
1Send a `WM_COPY` message. – Andreas Rejbrand Jul 11 '13 at 22:51
-
http://www.delphipages.com/forum/showthread.php?t=152642 – Robert Harvey Jul 11 '13 at 22:52
-
@ANdreas i just trying it before and it does not working with others process . – Oussaki Jul 11 '13 at 22:55
-
@Oussaki: yes it does, so you must not be using it correctly. – Remy Lebeau Jul 11 '13 at 22:57
-
When you tried sending `wm_Copy`, was there any text selected in the target window? That's a prerequisite for copying to occur. – Rob Kennedy Jul 11 '13 at 23:05
-
@ROb yeah there is a text selected before doing WM_COPY . – Oussaki Jul 11 '13 at 23:08
-
Could this relate to different process integrity levels? See: http://stackoverflow.com/questions/13772136/sending-wm-copy-from-a-delphi-app-to-another-process-in-windows-7 This might explain why Ken's example works but your situation does not. What's the other app, and have you tried to access an app that you've written (i.e. with the same integrity)? – Argalatyr Jul 12 '13 at 02:10
2 Answers
(Let me preface this by saying that using the clipboard for inter-process communication is a bad idea. The clipboard belongs to the user, and your application should only use it as a result of the user choosing to do so.)
If you have text selected in Notepad, this will get the contents into a TMemo
on a Delphi form (uses just a TMemo
and TButton
; add ClipBrd
to your uses clause):
procedure TForm1.Button1Click(Sender: TObject);
var
NpWnd, NpEdit: HWnd;
begin
Memo1.Clear;
NpWnd := FindWindow('Notepad', nil);
if NpWnd <> 0 then
begin
NpEdit := FindWindowEx(NpWnd, 0, 'Edit', nil);
if NpEdit <> 0 then
begin
SendMessage(NpEdit, WM_COPY, 0, 0);
Memo1.Lines.Text := Clipboard.AsText;
end;
end;
end;
Sample of results:
If the text is not selected first, send it a WM_SETSEL
message first. Passing values of 0
and '-1' selects all text.
procedure TForm1.Button1Click(Sender: TObject);
var
NpWnd, NpEdit: HWnd;
begin
Memo1.Clear;
NpWnd := FindWindow('Notepad', nil);
if NpWnd <> 0 then
begin
NpEdit := FindWindowEx(NpWnd, 0, 'Edit', nil);
if NpEdit <> 0 then
begin
SendMessage(NpEdit, EM_SETSEL, 0, -1);
SendMessage(NpEdit, WM_COPY, 0, 0);
Memo1.Lines.Text := Clipboard.AsText;
end;
end;
end;

- 123,280
- 14
- 225
- 444
-
that pretty work good. why i can't ask more questions in this web site ? – Oussaki Jul 17 '13 at 18:08
-
1@Oussaki: I have no idea why you can't ask more questions. You should check [meta] for that type of information; it's where questions about StackOverflow's workings, design and guidelines belong. (You saw I answered your [other question](http://stackoverflow.com/a/17605421/62576) as well?) – Ken White Jul 17 '13 at 18:11
-
1
Is there any way to simulate CTRL+C?
The way to do this is to use the SendInput
function of Win32 to synthesize keystrokes. Here is an example:
procedure SendCtrlC;
var
Inputs: array [0..3] of TInput;
begin
ZeroMemory(@Inputs, SizeOf(Inputs));
Inputs[0].Itype := INPUT_KEYBOARD;
Inputs[0].ki.wVk := VK_CONTROL;
Inputs[0].ki.dwFlags := 0; // key down
Inputs[1].Itype := INPUT_KEYBOARD;
Inputs[1].ki.wVk := ord('C');
Inputs[1].ki.dwFlags := 0; // key down
Inputs[2].Itype := INPUT_KEYBOARD;
Inputs[2].ki.wVk := ord('C');
Inputs[2].ki.dwFlags := KEYEVENTF_KEYUP;
Inputs[3].Itype := INPUT_KEYBOARD;
Inputs[3].ki.wVk := VK_CONTROL;
Inputs[3].ki.dwFlags := KEYEVENTF_KEYUP;
SendInput(4, Inputs[0], SizeOf(Inputs[0]));
end;
Naturally the application which you wish to receive the CTRL+C key stroke will need to have input focus.

- 601,492
- 42
- 1,072
- 1,490