[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

How to grab selected text in a window

Is it possible to get selected text from a window without sending it to the clipboard using the keystrokes Ctrl + C?

What I do at the moment, is that I react on the mouse event WM_LBUTTONUP in windows/controls with classnames that I list in my Delphi program. At the moment of the Mouse Up Event, I also know the handle of that window/control. I send Ctrl + C to that window, but my question is: Is there another way, can the selected text be acquired with for instance the Windows messaging methods, or by other means, not including the clipboard?
0
haab
Asked:
haab
  • 3
  • 2
2 Solutions
 
shaneholmesCommented:
You could program a HotKey to get the current focused controls text, is this what your interested in?

Shane
0
 
shaneholmesCommented:
Hotkeys:

Uses Messages;

In the main forms OnCreate handler assign the hotkey:

  If not RegisterHotkey( Handle, 1, MOD_ALT or MOD_SHIFT, VK_F9 ) Then
    ShowMessage('Unable to assign Alt-Shift-F9 as hotkey.');

In the main forms OnClose event remove the handler:

  UnRegisterHotkey( Handle, 1 );

Add a handler for the WM_HOTKEY message to the form:

  private // form declaration
    Procedure WMHotkey( Var msg: TWMHotkey );
      message WM_HOTKEY;

Procedure TForm1.WMHotkey( Var msg: TWMHotkey );
  Begin
   // If msg.hotkey =
  End;


OR

You can also use Form's onShortCut event.

procedure TForm1.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
case msg.CharCode of
  120 : begin { F9 pressed }; Handled := true; end;
  121 : begin { F10 pressed }; Handled := true; end;
end;
end;


Current Focused control

 Form1.ActiveControl


You could simple test the control for type

 if Form1.ActiveControl is TEdit


 MyText:= TEdit(Form1.ActiveControl).Text;


Hope this helps!

Shane
0
 
haabAuthor Commented:
Well, it is only the selected text that I want. I already know how to get the entire text of controls; the problem is to grab only the selected text, and the question whether it is possible to do so without involving the clipboard.

Thank you for your kind response, both on this issue and my other question from today regarding the maintaining of clipboard formats!
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!

 
haabAuthor Commented:
I see that you have given some code as well. Thank you! I will try it out!
0
 
shaneholmesCommented:
Again, test for the type of focused control, then use the

 SelText property of the control

MyText:= TEdit(Form1.ActiveControl).SelText;


Shane
0
 
kretzschmarCommented:
you could use the wm_gettext-message,
this will return you the whole text of course,
not only the selection

alternativly you could try the EM_GETSELTEXT-message
(but i guess this works only for richtext-controls)

meikl ;-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now