Solved

Send accelerators to applications.

Posted on 1997-11-23
12
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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