We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Send accelerators to applications.

otti
otti asked
on
Medium Priority
323 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
Comment
Watch Question

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

Author

Commented:
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
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

Author

Commented:
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
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

Author

Commented:
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.

Author

Commented:
Adjusted points to 200
Hi

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

JB

Author

Commented:
Hi!

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

Seems to be a really hard problem.
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
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;


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.