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

x
?
Solved

How do you get the selected text from any application and have it automatically copied into a edit box on my form when you  press a key on the keyboard?

Posted on 2007-08-11
5
Medium Priority
?
219 Views
Last Modified: 2010-04-05
How do you get the selected text from any application and have it automatically copied into a edit box on my form when you hold down a press a key on the keyboard?

I can get the caretpos cursor position, (x and y coordinates), had some help on that one.

Ben
0
Comment
Question by:Alpha_AI
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 19679271
I think it has nothing to do with caret position, to be honest.

I don't know how to get selected text from *any* application, but this should work with many of them:


procedure GetSelectedText;
var
  ThreadID : cardinal;
begin
ThreadID := GetWindowThreadProcessId(GetForegroundWindow);
AttachThreadInput(ThreadID, GetCurrentThreadId, true);
SendMessage(GetFocus, WM_COPY, 0, 0);
AttachThreadInput(ThreadID, GetCurrentThreadId, false);
Form1.Edit1.Text := '';
Form1.Edit1.PasteFromClipboard;
end;

procedure TForm1.FormHotKey(var msg : TWMHotKey);
begin
case msg.HotKey of
  1 : GetSelectedText;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
RegisterHotKey(handle, 1, MOD_CONTROL or MOD_SHIFT, ord('C')); // ctrl + shift + c
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnregisterHotKey(handle, 1)
end;


It will clear clipboard contents though (i.e., it will overwrite it with the selected text in apps that "support" this method).
Doesn't work with Firefox.
0
 

Author Comment

by:Alpha_AI
ID: 19681265
I dont understand what you mean by the last comment.

It will clear clipboard contents though (i.e., it will overwrite it with the selected text in apps that "support" this method).

I found another example on the net with this code, how ever for some reason i have to hit it twice
and sometimes it says 'cannot open clipboard'

if ((GetAsyncKeyState(VK_F12) and 1) = 1) then //if the user presses F12
  begin
    keybd_event(VK_CONTROL,0,0,0);
    keybd_event(ord('C'),0,0,0);
    keybd_event(VK_CONTROL,0,KEYEVENTF_KEYUP,0);
    keybd_event(ord('C'),0,KEYEVENTF_KEYUP,0);
    ProcessF12(Clipboard.asText);
    //richedit3.Clear;
    //richedit3.PasteFromClipboard; //load contents
   // control:=richedit3.Text;
    VtGetTotalCount(myvirtualstringtree1);
  end;

I put this code inside the timer on event.

How does this code compare to your one.
This code does work in Firefox.

Ben
0
 

Author Comment

by:Alpha_AI
ID: 19681303
Then there is this code

procedure TForm1.FormCreate(Sender: TObject);
begin
RegisterHotKey(Form1.Handle,101,0,VK_INSERT);
end;

procedure TForm1.WMHotKey(var Msg: TWMHotKey);
var
  thewnd : hwnd;
begin
   if Msg.HotKey = 101 then begin
     thewnd := getforegroundwindow;
     if thewnd <> 0 then
     begin //probably no reason to do this, but why not
       keybd_event( VK_Control,MapVirtualKey(VK_Control, 0),0, 0);
       keybd_event( ord('C'), MapvirtualKey( ord('C'), 0 ), 0, 0 );
       keybd_event( ord('C'), MapvirtualKey( ord('C'), 0 ), KEYEVENTF_KEYUP, 0 );
       keybd_event( VK_Control,MapVirtualKey(VK_Control, 0),KEYEVENTF_KEYUP, 0);
       Sleep(250); //give windows time to catchup
      //do what you want with the text here.
       showmessage('in the clipboard is: '+ clipboard.AsText);
       end;
     end;



end;


Havent had any problems with this code, can you see anywhere where this code will not work?
It also works with firefox and it doesnt give me any problems so far.

Ben
0
 

Author Comment

by:Alpha_AI
ID: 19681355
I want to change this code to F12 instead of INSert
how do I do that
Is it like this?

procedure TForm1.FormCreate(Sender: TObject);
begin
RegisterHotKey(Form1.Handle,101,0,VK_F12);
end;

procedure TForm1.WMHotKey(var Msg: TWMHotKey);
var
  thewnd : hwnd;
begin
   if Msg.HotKey = 101 then begin
     thewnd := getforegroundwindow;
     if thewnd <> 0 then
     begin //probably no reason to do this, but why not
       keybd_event( VK_Control,MapVirtualKey(VK_Control, 0),0, 0);
       keybd_event( ord('C'), MapvirtualKey( ord('C'), 0 ), 0, 0 );
       keybd_event( ord('C'), MapvirtualKey( ord('C'), 0 ), KEYEVENTF_KEYUP, 0 );
       keybd_event( VK_Control,MapVirtualKey(VK_Control, 0),KEYEVENTF_KEYUP, 0);
       Sleep(250); //give windows time to catchup
      //do what you want with the text here.
       showmessage('in the clipboard is: '+ clipboard.AsText);
       end;
     end;



end;

It doesnt work. Insert button is commonly used alot by people, i want tochange it to F12. How do i do that?
Ben
0
 
LVL 11

Accepted Solution

by:
ZhaawZ earned 500 total points
ID: 19682388
>> I dont understand what you mean by the last comment.

>> It will clear clipboard contents though (i.e., it will overwrite it with the selected text in apps that "support" this method).

If you have something in clipboard and use my code (or one of those that you copied), new data will be written to clipboard. For example, you take a screenshot for some purpose (i.e., hit Print Screen key) - it's in clipboard now. Then you use the code from previous posts - after using it screnshot is not in clipboard anymore, there is new data instead of it.

---------------------
>> if ((GetAsyncKeyState(VK_F12) and 1) = 1) then //if the user presses F12

I'd say this is *bad* way to do that.
1) some other applications could use GetAsyncKeyState() - in this case only one of them will catch it
2) there's no way to "block key" when using GetAsyncKeyState() - if you monitor, for example, 'A' key, it may catch that you pressed A key, but an active application will also receive it - in many cases it's not what you want.
3) you have to use timer to monitor key with GetAsyncKeyState(), i.e., you have to call this function all the time. Wouldn't it be better just to receive some "push" when this key is pressed? ;)

Hotkeys (or sometimes hooks) are better for this. I used hotkeys in my example - RegisterHotKey() and UnregisterHotKey() functions, WM_HOTKEY message processing.

---------------------
>> Havent had any problems with this code, can you see anywhere where this code will not work?

Do I have to check each and every application now? :)
No, I don't know where that code will not work - it *should* work in most applications.

---------------------
>> It doesnt work. Insert button is commonly used alot by people, i want tochange it to F12. How do i do that?

Read documentation...
http://msdn2.microsoft.com/en-us/library/ms646309.aspx




P.S. keybd_event() is obsolete, you should use SendInput() instead for simulating "ctrl+c".
0

Featured Post

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!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses

872 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