Link to home
Start Free TrialLog in
Avatar of TheLeader
TheLeader

asked on

Selected Text From an Active Window

Greeting...
How can I read selected text from an Active Window [using hot key] without the need to know title name or program name, modify the text in my program, and send it back to the same window I toke it from ?

Selected text from an active window => modify it => replace the selected text with the modify text.

please i need a clear code, i don't know much about API
thank you.
Avatar of geobul
geobul

Hi,

uses Clipbrd;

var s: string;
begin
  // Ctrl-C
  keybd_event(VK_CONTROL, 0, 0, 0);
  keybd_event(ORD('c'), 0, 0, 0);
  keybd_event(ORD('c'), 0, KEYEVENTF_KEYUP, 0);
  keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
  if Clipboard.HasFormat(CF_TEXT) then begin // there is text in the clipboard
    s := Clipboard.AsText; // get the text
    if Length(s) > 0 then begin
      s:= 'Hello'; // modigy it here. You must write your own code instead of this line
      Clipboard.SetTextBuf(PChar(s)); // write to the clipboard
      // Ctrl-V
      keybd_event(VK_CONTROL, 0, 0, 0);
      keybd_event(ORD('V'), 0, 0, 0);
      keybd_event(ORD('V'), 0, KEYEVENTF_KEYUP, 0);
      keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
    end;
  end;
end;

Regards, Geo
Replace the lines above

  keybd_event(ORD('c'), 0, 0, 0);
  keybd_event(ORD('c'), 0, KEYEVENTF_KEYUP, 0);

with

  keybd_event(ORD('C'), 0, 0, 0);
  keybd_event(ORD('C'), 0, KEYEVENTF_KEYUP, 0);

Sorry.
Avatar of TheLeader

ASKER

geobul, it seems (did not tested yet) your code will work perfect, but it uses clipboard
can you do it in another way, I don't want the user loses clipboard data.
thank you
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect :)
do you suggest any book, website, or article to help as you wrote this code ?
thank you.
You are welcome :-)

Except GetWindowText function which I already had I wrote the rest using Delphi help (Win32 Programmer's Reference mainly) and the MSDN site http://msdn.microsoft.com 

Regards, Geo