Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

send data to other application 1

Status
Not open for further replies.

Gigatech

Programmer
Jul 12, 2000
80
CR
Hi, I use Delphi 7.

I catched information from COM1 (a simple string). I have an event that is executed when the information "arrives" to the port. I need to send this text to the current active program (for example notepad or Word) as if the user has typed it through the keyboard. Any idea?

Thanks
 
Try this :-
Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ComObj,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  WordApplication, WordDocument: Variant;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  WordApplication := CreateOleObject('Word.Application');
  WordApplication.visible := true;
  WordDocument := WordApplication.Documents.Add;
  WordApplication.Selection.TypeText(edit1.text);
  WordDocument.SaveAs(FileName := 'C:\Doc.Doc', AddToRecentFiles := False);
end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  WordApplication.quit(false);
  VarClear(WordDocument);
  VarClear(WordApplication);
end;

end.
 
Hi,

nice code there Dachyon, but this will only activate a Word instance. but if I understood Gigatech , he would like to send the string to the active application and this could be any app...

here's some sample code to send a string in the form of keystrokes to the current active app :

Code:
procedure TFrm_main.SendStringToActiveApp(Received : string);

var  Tid   : Cardinal;
     TidTo : Cardinal;


begin
 Tid:=GetCurrentThreadId;
 TidTo:=GetWindowThreadProcessId(GetForeGroundWindow);
 AttachThreadInput(TidTo,Tid,True); 
 SendKeysToHwnd(GetFocus,Received);
 AttachThreadInput(TidTo,Tid,False);
end;

procedure SendKeysTohWnd(hWnd : LongWord; Text : string);
// send keystrokes to a specific window handle
var i         : integer;
    c         : char;
    wparam,
    lparam    : longword;
    scancode  : byte;
    oemScan   : word;
    prevchar  : word;
    Len       : word;

begin
 if Text <> '' then
  begin
   Len:=length(Text);
   i:=1;
   while i <= Len do
   begin
    c:=Text[i];
    wparam:=ord(c);
    // if character is #0 then next chars will be only keydown
    // if char is #1 then next chars will only be keyup
    if wparam < 2 then
     begin
      // #0 or #1
      Inc(i);
      if i > Len then Exit; // prevent critical error
      c:=Text[i];
      prevchar:=wparam;
      wparam:=ord(c);
     end
    else prevchar:=wparam;
    if (c > #31) and (PrevChar > 1) then // only do wm_char if keyup,keydown control chars are not used
     begin
      // normal chars, use WM_CHAR message to simulate keystroke
      oemScan:=Lobyte(vkKeyScan(c));
      scancode:=MapVirtualKey(oemScan,0);
      lparam:=1+(scancode shl 16);
      postmessage(hwnd,WM_CHAR,wparam,lparam);
     end
    else
     begin
      // system codes, use WM_KEYDOWN and WM_KEYUP to simulate keystroke
      scancode:=MapVirtualKey(wparam,0);
      if prevchar <> 1 then
       begin
        lparam:=1 or (scancode shl 16) or $40000000; // (1 shl 30);
        postmessage(hwnd,WM_KEYDOWN,wparam,lparam);
       end;
      if prevchar > 1 then
       begin
        oemScan:=Lobyte(vkKeyScan(c));
        scancode:=MapVirtualKey(oemScan,0);
        lparam:=1+(scancode shl 16);
        postmessage(hwnd,WM_CHAR,wparam,lparam);
       end;
      if prevchar <> 0 then
       begin
        lparam:=1 or (scancode shl 16) or $C0000000; // 3 shl 30
        postmessage(hwnd,WM_KEYUP,wparam,lparam);
       end;
     end;
    Inc(i);
   end;
  end;
end;

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top