|
-
Oct 25th, 2014, 09:39 AM
#1
Thread Starter
Member
Keyboard Hook to intercept clipboard pasting
I have a need to copy a numeric value to the clipboard and upon pasting it into a document having it increment the number with each paste. I am using a keyboard hook to intercept the CTRL-V, Un-register the hook, read the clipboard and if it can be incremented then do so, paste the clipboard (by use of SendKeys to send a CTRL-V), then Re-register the keyboard hook.
This works, however by using SendKeys to paste the clipboard it kills the ability for key-repeat. I can keep pasting one number at a time as long as I lift the CTRL key each time, otherwise the key repeat kicks in and pastes a bunch of v's.
I am trying to find an alternative to using SendKeys as a way of continuing the paste operation or a way of letting the computer recognize the CTRL key as part of the key repeat. Anyone?
Thanks,
Jim
Code:
Imports System.Runtime.InteropServices
Public Class Form1
Public Const VK_ALT As Integer = &H1
Public Const VK_CTRL As Integer = &H2
Public Const VK_SHIFT As Integer = &H4
Public Const VK_CTRL_ALT As Integer = &H1 + &H2
Public Const VK_CTRL_SHIFT As Integer = &H2 + &H4
Public Const WM_HOTKEY As Integer = &H312
<DllImport("User32.dll")> _
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
End Function
<DllImport("User32.dll")> _
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RegisterHotKey(Me.Handle, 100, VK_CTRL, Keys.V)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
UnregisterHotKey(Me.Handle, 100)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
Dim id As IntPtr = m.WParam
Dim iData As IDataObject = Clipboard.GetDataObject()
Dim Numb As Integer = 0
UnregisterHotKey(Me.Handle, 100)
If id.ToString = "100" Then
If iData.GetDataPresent(DataFormats.Text) Then
Try
Numb = CType(iData.GetData(DataFormats.Text), String) + 1
Clipboard.SetText(Numb.ToString)
System.Windows.Forms.SendKeys.Send("^v")
Catch ex As Exception
System.Windows.Forms.SendKeys.Send("^v")
End Try
Else
System.Windows.Forms.SendKeys.Send("^v")
End If
End If
RegisterHotKey(Me.Handle, 100, VK_CTRL, Keys.V)
End If
MyBase.WndProc(m)
End Sub
End Class
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|