[英]how to Simulate Ctrl+ c in Delphi
is there any way to simulate Ctrl+C command in delphi ? 有没有办法在delphi中模拟Ctrl + C命令? the problem is i want that from another application for example copy a text from Notepad after select the target text . 问题是我希望从另一个应用程序,例如在选择目标文本后从记事本复制文本。
(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): 如果您在记事本中选择文本,这将让内容制作成TMemo
一个Delphi形式(只使用一个TMemo
和TButton
;添加ClipBrd
到您的使用条款):
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. 如果未首先选择文本,请首先向其发送WM_SETSEL
消息。 Passing values of 0
and '-1' selects all text. 传递值0
和'-1'选择所有文本。
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;
Is there any way to simulate CTRL+C ? 有没有办法模拟CTRL + C ?
The way to do this is to use the SendInput
function of Win32 to synthesize keystrokes. 这样做的方法是使用Win32的SendInput
函数来合成击键。 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. 当然,您希望接收CTRL + C键击的应用程序需要具有输入焦点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.