在 computer.keyboard 上按 Ctrl + C

Pressing Ctrl + C on computer.keyboard

所以我正在构建一个名为“剪贴板历史记录”的工具

我的目标是,当我从任何地方(网络、PC 文本等)复制某些内容时,我想将 my.computer.clipboard 添加到 datagridview。

一切都很好,所以我只需要一个命令或识别 Ctrl + C 的命令(在 pckeyboard 中,不仅是活动形式) 并执行一次 (Form1.DataGridView1.Rows.Add(DateAndTime.Now, Clipboard.GetText)

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    Dim keydata As Keys
    If My.Computer.Keyboard.CtrlKeyDown AndAlso keydata.C Then
        Form1.DataGridView1.Rows.Add(DateAndTime.Now, Clipboard.GetText)

    End If
End Sub

谢谢你:)

据我所知,没有标准的 .Net 库可以完成您想要的。通过使用 Windows API 函数 AddClipboardFormatListener 您可以收到放置在剪贴板上的项目的通知。

AddClipboardFormatListener 需要一个 Window 句柄来向其发送通知。我使用派生自 System.Windows.Forms.NativeWindow to listen for the WM_CLIPBOARDUPDATE. This class defined below, expose an Event named ClipBoardDataAvailable that can be subscribed too and it the passed argument is a DataObject 的 class,可用于检索剪贴板内容。

Imports System.Runtime.InteropServices

Public Class ClipboardListener : Inherits NativeWindow
    Implements IDisposable
    Public Event ClipBoardDataAvailable(data As DataObject)

    Public Sub New()
        Me.CreateHandle(New CreateParams())
        AddClipboardFormatListener(Me.Handle)
    End Sub

    Public Overrides Sub DestroyHandle()
        Me.Dispose()
        MyBase.DestroyHandle()
    End Sub

    Protected Overrides Sub WndProc(ByRef m As Message)
        Const WM_CLIPBOARDUPDATE As Int32 = &H31D
        If m.Msg = WM_CLIPBOARDUPDATE Then
            Dim data As DataObject = CType(Clipboard.GetDataObject, DataObject)
            RaiseEvent ClipBoardDataAvailable(data)
        End If
        MyBase.WndProc(m)
    End Sub

    <DllImport("user32.dll", SetLastError:=True)> _
    Public Shared Function AddClipboardFormatListener(hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True)> _
    Public Shared Function RemoveClipboardFormatListener(hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            RemoveClipboardFormatListener(Me.Handle)
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    Protected Overrides Sub Finalize()
         ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
         Dispose(False)
         MyBase.Finalize()
    End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class

用法示例:

Public Class Form1
    Private WithEvents listener As New ClipboardListener

    Private Sub listener_ClipBoardDataAvailable(data As DataObject) Handles listener.ClipBoardDataAvailable
        If data.ContainsText Then Debug.Print(data.GetText)
    End Sub
End Class