Send accelerators to applications.

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
ottiAsked:
Who is Participating?
 
ronit051397Connect With a Mentor Commented:
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
 
JimBob091197Commented:
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
 
ottiAuthor 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
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
JimBob091197Commented:
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
 
ottiAuthor 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
0
 
JimBob091197Commented:
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
 
ottiAuthor 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.
0
 
ottiAuthor Commented:
Adjusted points to 200
0
 
JimBob091197Commented:
Hi

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

JB
0
 
ottiAuthor Commented:
Hi!

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

Seems to be a really hard problem.
0
 
JimBob091197Commented:
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
 
ottiAuthor 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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.