AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Chris
Expertise
I can answer pretty much any question relating to VB.NET and its use in a Windows environment. I specialize in ASP.NET web development and MSSQL database access.

Experience
I have over 5 years of industry experience using VB.NET and other .NET technologies for web and database development.

Education/Credentials
I have some college education, but does it really matter in this field of work?

 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > Accessing other programs selected text

Topic: VB.NET



Expert: Chris
Date: 8/11/2007
Subject: Accessing other programs selected text

Question
hello Chris.
I want to write a dictionary sofware.
It should be lunched when the user select a text in any other programs and then press an especial key (For example Alt F10) . Such as Babylon Dictionary.
So I have 2 problems.
1- Lunching my dictionary by pressing an especial key.
2 - Accessing selected text in other program.
Can you help me ?
Thanks alot.

Answer
Hi,

A quick search of Google for ".net (vb OR c#) hotkey" led me to this URL at codeproject.com:
http://www.codeproject.com/vb/net/mclhotkeynet.asp

This utility here can easily be used from your app to capture a system-wide hotkey.

Another quick search of Google for ".net (vb OR c#) selected text other program" led me to gotdotnet.com:
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=b8aeb907-...

This example program, through a module called WndUtils in the SendKeys.vb file, demonstrates how to simulate a keypress in another program.  You'll want to simulate the Control+C key combination, and send it to the active window.

Another site I found shows exactly how to do this, though it's in C#:
http://www.thescripts.com/forum/thread255335.html

Translated to VB, it would look like:

Module CopyActiveWindow
 Const KEYEVENTF_KEYUP As UInt32 = 2
 Const VK_CONTROL As Byte = &H11 ' 0x11h

 Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" ( _
     ByVal hWnd As IntPtr) As Boolean

 Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As IntPtr

 Declare Sub SendKeyboardEvent Lib "user32" Alias "keybd_event" ( _
     ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As UInt32, ByVal dwExtraInfo As UInt32)

 Public Sub SendCtrlC(ByVal hWnd As IntPtr)
   SetForegroundWindow(hWnd)

   SendKeyboardEvent(VK_CONTROL, 0, 0, 0)
   SendKeyboardEvent(&H43, 0, 0, 0) ' Send the C key (0x43h is "C")
   SendKeyboardEvent(&H43, 0, KEYEVENTF_KEYUP, 0)
   SendKeyboardEvent(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0) ' Left Control Up
 End Sub
End Module

Calling SendCtrlC(GetForegroundWindow()) from inside an event handler from the hotkey utility I referenced should do as you want, although I'm having trouble getting the Ctrl+C to go through properly myself.  

I hope this helps - let me know if you can get it to work.

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.