Solved

Send accelerators to applications.

Posted on 1997-11-23
12
294 Views
Last Modified: 2010-04-04
Hello!

I want to send i.e to Notepad the accelerator for
"Select all" (ctrl-a). How to do it ?

I tried "GetMenu" and got the handle of the menuitem and send then the WM_COMMAND message. This works fine, but i need it for differnet apps and searching everytime the handle of the menuitem is not the best way.

I'm using Delphi2.0 with WIN95 and WinNT4.0

Thanks in advance!

Bye, Sascha
0
Comment
Question by:otti
  • 6
  • 5
12 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1351607
Hi

Another way is to use FindWindow to get the handle of the window.  You need to know the window class name and optionally the window caption too.

procedure NotepadSelectCopy;
var
  hNotePad: THandle;
begin
  // To send to 1st found instance of Notepad
  hNotePad := FindWindow(PChar('Notepad'), nil);
  // If you know the window caption (e.g. "Untitled - Notepad")you could use:
  // hNotePad := FindWindow(PChar('Notepad'), PChar(Untitled - Notepad));

  if (hNotePad > 0) then begin
    hNotePad := GetTopWindow(hNotePad);
    if (hNotePad > 0) then begin
      SendMessage(hNotePad, EM_SETSEL, 0, 65535);
      SendMessage(hNotePad, WM_COPY, 0, 0);
    end;
  end;
end;


JB
0
 

Author Comment

by:otti
ID: 1351608
Hi Jim!

I tried it with WM_COPY too, but it only works with Notepad. It should work with all apps, like MS Outlook Express, Agent, Eduroa, InternetMail... (To get the windowhandle is not my problem)

Notepad is only one window, but i.E. Outlook Express have a MainWindow, with some child windows. Simply send the EM_SETSEL and WM_COPY message to the Handle of the child window (The control) dosen't work. I don't know why.

The best soulution for me is really send the accelerator to the main window, so i don't have to look for all child windows and search for the control which has the text i want to copy.

Any hint?

Bye, Sascha
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1351609
Different apps use different keys for select all text.  Some use Ctrl+A, others (e.g. Notepad) don't have any shortcut keys, but rely on menu items only.  There is no universal way of telling any given app to select all the text and copy to clipbrd.

I think that your Outlook sample wasn't working because you're sending the messages to the wrong child window.  I tried it in Delphi by hardcoding the window handle (which I got using VC++ Spy++ program) and the text was selected perfectly.

JB
0
 

Author Comment

by:otti
ID: 1351610
Hi Jim!

Yes op course, use differnet apps differnet accs. but i want to make userconfiguarble. The user has the choise which keystroke he want to send to the app.
The only problem is that i don't know how to send accelerators.

I'm wondering that it works at you.
I'm using Winsight to got the Windowclass of the Childwindow, i tried to send the messages to the windows with the Classname "ATH_Note", "Ath_DocHost" and "Internet Explorer_Server".
Nothing works.

To which childwindow (Windowclass) do you sent the message?

My way to get the windowhandles:

MainHandle:=FindWindowEx(0,0,'ATH_Note'+#0,nil);
Childhandle:=FindWindowEx(MainHandle,0,'Ath_DocHost'+#0,nil);
Childhandle2:=FindWindowEx(Childhandle,0,'Internet Explorer_Server'+#0,nil);

SendMessage(Childhandle2,....)

Bye, Sascha
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1351611
Hi

I only used the FindWindow method for Notepad (in my example above).  For the Outlook sample I used Spy++ to find the window handle, then I hard-coded the window handle:

var
  h: THandle;
begin
  h := $878;  // Used Spy++ to get this handle.
  SendMessage(h, EM_SETSEL, 0, 65535);
end;

This selected all the text in the outlook window.

I just did this as a test because you said that your Outlook example doesn't work.  The Outlook test I did only works because I was sending the msg to the correct window, but I cheated in getting the window handle...

JB
0
 

Author Comment

by:otti
ID: 1351612
Hi!

I understand that you hardcoded the handle, but if i try this it doesen't work :-(

Winsight tells me the Windowclassname, does this Spy++, too??
If yes, please tell me the windowclassname of the handle you used. (The Windowclass is uniqe - so i can check if i used the wrong childwindowhandle).

I tried it with the window which appear if you doubleclick on a message, you too?

Bye, Sascha
If we can get this work at my computer it would be great.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:otti
ID: 1351613
Adjusted points to 200
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1351614
Hi

I set it to the childwindow, which has WindowClass "RichEdit20A".

JB
0
 

Author Comment

by:otti
ID: 1351615
Hi!

Winsight don't show any Windowclass called "RichEdit20A" :-((

Seems to be a really hard problem.
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1351616
Mmm...

Using Spy++ I see that when you press Ctrl+A in Outlook, the parent window gets the following msgs:
WM_KEYDOWN: VK_CONTROL
WM_KEYDOWN: Ord('A')
WM_KEYUP: Ord('A')
WM_KEYUP: VK_CONTROL

But when I send these messages manually to Outlook nothing happens!!  I will try more later (got a bit of work to do now) and let you know if I come up with anything...

JB
0
 
LVL 5

Accepted Solution

by:
ronit051397 earned 400 total points
ID: 1351617
Hellow Sascha, from Russia?...

In most applications, by pressing the Ctrl+Home and then by pressing the Ctrl+Shift+End, you select all text/objects.
Using the keybd_event function enables you to simulate key strokes.

The following code selects all text from a word document or a notepad and copy it to the clipboard:
(Tested in Delphi 3 under WinNT 4)
1. Place a button on a form and copy the attached code.
2. activate the application and follow the instructions.

procedure TForm1.Button1Click(Sender: TObject);
var WinHandle: THandle;
FCode: string;
begin
if InputQuery('Selecting and copying', 'Press 1 for word document or 2 for Notepad', FCode) then
begin
if FCode='1' then
begin
  ShowMessage('Open a word document and enter some text in it. After that press OK');
  WinHandle:=Findwindow('OpusApp',nil);
end;
if FCode='2' then
begin
  ShowMessage('Open a Notepad and enter some text in it. After that press OK');
  WinHandle:=Findwindow('NotePad',nil);
end;
  SetForeGroundWindow(WinHandle);
  keybd_event(VK_CONTROL,0,0,0); //send the ^HOME
  keybd_event(VK_HOME,0,0,0);
  keybd_event(VK_CONTROL,0,KEYEVENTF_KEYUP,0);
  keybd_event(VK_HOME,0,KEYEVENTF_KEYUP,0);

  keybd_event(VK_CONTROL,0,0,0); //send the Ctrl+Shift+Home
  keybd_event(VK_SHIFT,0,0,0);
  keybd_event(VK_END,0,0,0);
  keybd_event(VK_CONTROL,0,KEYEVENTF_KEYUP,0);
  keybd_event(VK_SHIFT,0,KEYEVENTF_KEYUP,0);
  keybd_event(VK_END,0,KEYEVENTF_KEYUP,0);

  keybd_event(VK_CONTROL,0,0,0);//send the ^C
  keybd_event(67,0,0,0);
  keybd_event(67,0,KEYEVENTF_KEYUP,0);
  keybd_event(VK_CONTROL,0,KEYEVENTF_KEYUP,0);
  ShowMessage('All selected text was copied to the clipboard');
end
end;


0
 

Author Comment

by:otti
ID: 1351618
Hi !

I tried your source, but only notepad selects all the text if i used this source. The most other apps i tried don't.

I found the answer by myself - i used:

Handle:=GetForegroundWindow;
PostMessage(Handle,WM_KEYDOWN,VK_CONTROL,0);
PostMessage(Handle,WM_KEYDOWN,$41,0);

Simple, isn't it?
But in some applications this not worked, so i tried to use the keybd_event that you have used - now everything works fine!

Thank you all for your help!

Sascha
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now