I want to copy the highlighted text from outside of winform using vb.net. For example, if the user highlights a text in a browser or in notepad it should be copied into a textbox in a winform. Any help would be great! Thanks in advance.
Asked
Active
Viewed 519 times
3
-
1It seems like you want to have a all-purpose select=copy. I dont know if there is a nice event-based way to do get the text value from that. So I would try to bodge it using polling: Send a `ClipBoard.Copy()` every 0.1 sec, check if it changed since last time, change=show in form. unchanged=reset clipboard to old value. – Fixation Jun 23 '20 at 20:18
-
this question is the C# equivallent: https://stackoverflow.com/questions/21460943/how-to-get-selected-text-of-any-application-into-a-windows-form-application – Fixation Jun 23 '20 at 20:27
-
1You can use something like this: [Move window when external application's window moves](https://stackoverflow.com/a/48812831/7444103). Now, that code hooks a specific Window handle, but of course you can change it to hook the Foreground Window (the active window that receives mouse input). The event must be set to `EVENT_OBJECT_TEXTSELECTIONCHANGED`, while that code is of course using `EVENT_OBJECT_LOCATIONCHANGE`. Note that you just need to hook the main Window. The Text selection change is notified for any child Window that has an Automation TextPattern that notifies text selection changes. – Jimi Jun 23 '20 at 21:34
-
1@Fixation All that code supposes that child Windows have an handle. This applies to only Win32/WinForms controls. WPF/UWP window-less controls don't have handles. Plus, you need constant polling, which implies that the user activity may be disrupted because of this. – Jimi Jun 23 '20 at 21:37
-
1You also need to remove the `... && idObject == (Hook.SWEH_ObjectId)Hook.SWEH_CHILDID_SELF` condition, since this was added to prevent child objects notifications from activating the same procedure, while now you want them. – Jimi Jun 23 '20 at 21:57
-
@Fixation Thanks for your suggestion it helped me to build the logic. – Uniquedesign Jun 25 '20 at 15:20
1 Answers
1
Ok! Thanks to this link I got my answer.
My Logic is First of all the user will highlight a value, not specific to browser but anywhere. then the user will press the Hotkey, In my case it's F8. Then Code will trigger the copy command and then retrieve the Clipboard value and will assign it to a textbox text. Here is the Full Code along with Helping Class.
Hotkey.vb
Public Class Hotkey
#Region "Declarations - WinAPI, Hotkey constant and Modifier Enum"
''' <summary>
''' Declaration of winAPI function wrappers. The winAPI functions are used to register / unregister a hotkey
''' </summary>
Private Declare Function RegisterHotKey Lib "user32" _
(ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
Public Const WM_HOTKEY As Integer = &H312
Enum KeyModifier
None = 0
Alt = &H1
Control = &H2
Shift = &H4
Winkey = &H8
End Enum 'This enum is just to make it easier to call the registerHotKey function: The modifier integer codes are replaced by a friendly "Alt","Shift" etc.
#End Region
#Region "Hotkey registration, unregistration and handling"
Public Shared Sub registerHotkey(ByRef sourceForm As Form, ByVal triggerKey As String, ByVal modifier As KeyModifier)
RegisterHotKey(sourceForm.Handle, 1, modifier, &H77)
End Sub
Public Shared Sub unregisterHotkeys(ByRef sourceForm As Form)
UnregisterHotKey(sourceForm.Handle, 1) 'Remember to call unregisterHotkeys() when closing your application.
End Sub
Public Shared Sub handleHotKeyEvent(ByVal hotkeyID As IntPtr)
SendKeys.Send("^(c)") 'for Ctrl-C[/CODE]
End Sub
#End Region
End Class
This Section will Trigger the Hotkey Code and do the rest of logic
Main Form
'This Program will wait for a key to press in our case it will wait for the F8 key to be press
'Then it will copy the highlighted text (Outside and Inside of the form) and will concatinate the text with a webaddress'
Imports Name_Of_Your_Project.Hotkey
Public Class Form1
'This Chunk of code will register the F8 key as a main key for the Program'
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Hotkey.registerHotkey(Me, "f8", Hotkey.KeyModifier.None)
End Sub
'This sub will trigger the Hotkey Sub Code in the Hotkey.vb Class'
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = Hotkey.WM_HOTKEY Then
Hotkey.handleHotKeyEvent(m.WParam)
'After pressing the F8 key It will copy the highlighted data from anywhere and store it to the clipboard'
If Clipboard.ContainsText Then
Try
Textbox1.text = My.Computer.Clipboard.GetData(DataFormats.Text).ToString
Catch ex As Exception
MessageBox.Show("Error in Program" + ex.ToString())
End Try
End If
End If
MyBase.WndProc(m)
End Sub
'System wide hotkey event handling
End Class

Uniquedesign
- 423
- 1
- 7
- 23