I'm trying out to read window text (a console kind of window) using my application. This is how i do it.
1. Send Ctrl+a and then Ctrl + C using 'sendkey' to terminal
// Ctrl + a
keybd_event(VK_CONTROL, 0, 0, 0);
fn_SendKeys('a');
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
// Ctrl + c
keybd_event(VK_CONTROL, 0, 0, 0);
fn_SendKeys('c');
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
2. Once copied, it is saved to a txt file from the clipboard
function Tform1.fn_Save_Clipboard_Text_ToFile(sFileTo: string): Boolean;
var
success: Boolean;
counter: Integer;
begin
success := False;
counter := 1;
while ((not success) and (counter < 6)) do
begin
Result := Clipboard.HasFormat(CF_UNICODETEXT);
if Result then
try
TFile.WriteAllText(sFileTo, Clipboard.AsText);
success := True;
except
on Exception do
begin
fn_delay(1);
counter := counter + 1;
end;
end;
end;
end;
3. process the information in txt file by reading it
Everything works perfectly, except when i do a Ctrl + C on a memo, within my application, prior to sending Ctrl + c to window. When i do the Ctrl + C within memo, and try to save the clip board as text in my application, it throws exception saying "cannot open clipboard - access denied".
It works 100% perfectly when i do not do the copy operation on the memo prior to screen copy. The strange thing is, it fails 100% if i do the copy/paste in memo prior to screen read.
Could anyone please help me to identify, what am I doing wrong here?