Recent

Author Topic: [SOLVED] Simulate ctrl+c press in active application (Windows)  (Read 8869 times)

totya

  • Hero Member
  • *****
  • Posts: 720
Hi!

I want this: My app is running in background. If I press own hotkey (example:F2), the result is in any active application (select all, copy to clipboard):
ctrl-a
ctrl-c

Global hotkey is working for me from my Lazarus app, but I can't send ctrl-a/ctrl-c to the active application. How can I do this? I see MouseAndKeyInput examples, but this send keys only to the main application, not to others.

Thanks.
« Last Edit: May 15, 2015, 11:17:25 pm by totya »

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: Simulate ctrl+c press in active application.
« Reply #1 on: April 16, 2014, 07:58:23 pm »
I assume you are using Windows.

Normally, an application has to have focus to capture key strokes.

I remember having to trap messages to extract key strokes, and then passing the messages on again. This was by using API calls. I have not done it since Windows 98, so don't remember the details.

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Simulate ctrl+c press in active application.
« Reply #2 on: April 16, 2014, 08:25:58 pm »
Hi!

It's not really usable answer...

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: Simulate ctrl+c press in active application (Windows)
« Reply #3 on: April 19, 2014, 06:41:37 pm »
You will have to study how to trap Windows API calls that relate to messages. This will probably be in C or C++. You then need to convert it to FPC by using things like records rather than structs.

I can't give a complete answer because I threw away my old Windows/Delphi books several years ago.

Read articles like this one: http://www.delphi-central.com/ipc_sendmessage.aspx

Also look at torry.net and http://www.drbob42.com/.
« Last Edit: April 19, 2014, 06:46:56 pm by Windsurfer »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Simulate ctrl+c press in active application (Windows)
« Reply #4 on: April 19, 2014, 07:39:29 pm »
I see MouseAndKeyInput examples, but this send keys only to the main application, not to others.
@totya, MouseAndKeyInput is what you want. But you need to leave enough time between ctrl+a and ctrl+c to allow the target application to respond. You could also clear the clipboard before calling ctrl+c and afterwards check it again.

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Simulate ctrl+c press in active application (Windows)
« Reply #5 on: April 19, 2014, 07:43:56 pm »
I see MouseAndKeyInput examples, but this send keys only to the main application, not to others.
@totya, MouseAndKeyInput is what you want. But you need to leave enough time between ctrl+a and ctrl+c to allow the target application to respond. You could also clear the clipboard before calling ctrl+c and afterwards check it again.

Thanks, but I wrote this in starting post: " I see MouseAndKeyInput examples, but this send keys only to the main application, not to others."

This is my problem first. Can you see me example?

Thanks!

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Simulate ctrl+c press in active application (Windows)
« Reply #6 on: April 19, 2014, 07:56:31 pm »
Drop a memo and a timer on a form and add the following code:
Code: [Select]
uses
  ..., MouseAndKeyInput, Clipbrd;
...
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  KeyInput.Apply([ssCtrl]);
  KeyInput.Press('A');
  KeyInput.Unapply([ssCtrl]);

  Sleep(200);

  KeyInput.Apply([ssCtrl]);
  KeyInput.Press('C');
  KeyInput.Unapply([ssCtrl]);

  Memo1.Lines.Add(Clipboard.AsText);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Timer1.Interval:=10000;
  Timer1.Enabled:=True;
end;
Every ten seconds it copies whatever text of the active application.

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Simulate ctrl+c press in active application (Windows)
« Reply #7 on: April 19, 2014, 08:17:33 pm »
Hi!

KeyInput works with active application? LOL. This is the solution! And now, this is works perfectly!

Thanks engkin!

And thanks too to Windsurfer for informations!

The problem is solved.


Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: Simulate ctrl+c press in active application (Windows)
« Reply #8 on: April 21, 2014, 09:14:43 am »
Thanks Engkin,
That is much simpler than the way I would have tried.



 

TinyPortal © 2005-2018