Vb.net 捕获 Ctrl+C

发布于 2024-09-18 08:16:30 字数 86 浏览 6 评论 0原文

我想捕捉有人使用 CtrlC 的情况,即使是在焦点未对准的情况下。我正在使用 Visual Basic 2010。

I want to capture when someone uses CtrlC even when off focus. im using Visual Basic 2010.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2024-09-25 08:16:30

好的,我为您提供了一个经过验证有效的解决方案。不过,您将需要一个 C# 库,并且需要一些额外的工作,但不会太多。创建一个 C# 类库并添加一个名为“MyHooks”的类,并添加对 System.Windows.Forms.dll 和我链接到的库的引用。将使用此库的主程序将引用此 C# 库和 System.Windows.Forms。

namespace HookManager.Interface {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gma.UserActivityMonitor;
using System.Windows.Forms;

public static class MyHooks {

    public static void HookControlC(KeyEventHandler keyDown, KeyEventHandler keyUp) {
        HookManager.KeyDown += keyDown;
        HookManager.KeyUp += keyUp;
    }

}
}

现在在你的程序中可以做类似的事情:

Imports hookmanager.interface
Imports System.Windows.Forms

Module Module1

Sub Main()
    MyHooks.HookControlC(AddressOf ControlC_KeyDown, AddressOf ControlC_KeyUp)

    While True
        Application.DoEvents()
    End While
End Sub

Private m_ControlKeyPressed As Boolean = False

Private Sub ControlC_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
    If e.KeyValue = 162 OrElse e.KeyValue = 163 Then
        m_ControlKeyPressed = True
    End If
    If m_ControlKeyPressed Then
        If e.KeyCode = Keys.C Then
            Console.WriteLine("You captured, control c!")
            Console.WriteLine(Clipboard.GetText())
        End If
    End If
End Sub

Private Sub ControlC_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
    If m_ControlKeyPressed Then
        If e.KeyValue = 162 OrElse e.KeyValue = 163 Then
            m_ControlKeyPressed = False
        End If
    End If
End Sub

End Module

Okay, so I have a solution for you that I verified works. You will need a C# library though, and a little extra work is required, but not much. Create a C# class library and add a class called 'MyHooks' and add a reference to both System.Windows.Forms.dll and the library I linked you to. Your main program that will use this will reference this C# library and System.Windows.Forms.

namespace HookManager.Interface {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gma.UserActivityMonitor;
using System.Windows.Forms;

public static class MyHooks {

    public static void HookControlC(KeyEventHandler keyDown, KeyEventHandler keyUp) {
        HookManager.KeyDown += keyDown;
        HookManager.KeyUp += keyUp;
    }

}
}

Now in your program can do something like:

Imports hookmanager.interface
Imports System.Windows.Forms

Module Module1

Sub Main()
    MyHooks.HookControlC(AddressOf ControlC_KeyDown, AddressOf ControlC_KeyUp)

    While True
        Application.DoEvents()
    End While
End Sub

Private m_ControlKeyPressed As Boolean = False

Private Sub ControlC_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
    If e.KeyValue = 162 OrElse e.KeyValue = 163 Then
        m_ControlKeyPressed = True
    End If
    If m_ControlKeyPressed Then
        If e.KeyCode = Keys.C Then
            Console.WriteLine("You captured, control c!")
            Console.WriteLine(Clipboard.GetText())
        End If
    End If
End Sub

Private Sub ControlC_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
    If m_ControlKeyPressed Then
        If e.KeyValue = 162 OrElse e.KeyValue = 163 Then
            m_ControlKeyPressed = False
        End If
    End If
End Sub

End Module
獨角戲 2024-09-25 08:16:30

您需要创建一个低级挂钩。 这个 CodeProject 示例 工作完美,我自己也用过它学习目的。我稍微修改了代码,但这里有一个简单的示例,说明您可以使用该库执行某些操作。同样,这只是一个示例,可能不反映最终代码,但可以轻松修改以捕获 Control+C,并且该库有详细记录。

   private static bool m_ControlKeyPressed = false;

   private static void ControlC_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyValue == 162 || e.KeyValue == 163) {
            m_ControlKeyPressed = true;
        }
        if (m_ControlKeyPressed) {
            if (e.KeyCode == Keys.C) {
                e.SuppressKeyPress = true; // Suppress, or do something with it
            }
        }
    }

    private static void ControlC_KeyUp(object sender, KeyEventArgs e) {
        if (m_ControlKeyPressed) {
             if (e.KeyValue == 162 || e.KeyValue == 163) {
                m_ControlKeyPressed = false;
            }
        }
    }

转换为 Vb.Net

Private Shared m_ControlKeyPressed As Boolean = False

Private Shared Sub ControlC_KeyDown(sender As Object, e As KeyEventArgs)
    If e.KeyValue = 162 OrElse e.KeyValue = 163 Then
        m_ControlKeyPressed = True
    End If
    If m_ControlKeyPressed Then
        If e.KeyCode = Keys.C Then
            e.SuppressKeyPress = True
        End If
    End If
End Sub

Private Shared Sub ControlC_KeyUp(sender As Object, e As KeyEventArgs)
    If m_ControlKeyPressed Then
        If e.KeyValue = 162 OrElse e.KeyValue = 163 Then
            m_ControlKeyPressed = False
        End If
    End If
End Sub

You need to create a low-level hook. This CodeProject example works perfect and I have used it myself for learning purposes. I modified the code slighty, but here is a simple example of something you could do with that library. Again, this is just an example and may not reflect the final code, but could be easily modified to capture Control+C, and the library is well documented.

   private static bool m_ControlKeyPressed = false;

   private static void ControlC_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyValue == 162 || e.KeyValue == 163) {
            m_ControlKeyPressed = true;
        }
        if (m_ControlKeyPressed) {
            if (e.KeyCode == Keys.C) {
                e.SuppressKeyPress = true; // Suppress, or do something with it
            }
        }
    }

    private static void ControlC_KeyUp(object sender, KeyEventArgs e) {
        if (m_ControlKeyPressed) {
             if (e.KeyValue == 162 || e.KeyValue == 163) {
                m_ControlKeyPressed = false;
            }
        }
    }

Conversion to Vb.Net

Private Shared m_ControlKeyPressed As Boolean = False

Private Shared Sub ControlC_KeyDown(sender As Object, e As KeyEventArgs)
    If e.KeyValue = 162 OrElse e.KeyValue = 163 Then
        m_ControlKeyPressed = True
    End If
    If m_ControlKeyPressed Then
        If e.KeyCode = Keys.C Then
            e.SuppressKeyPress = True
        End If
    End If
End Sub

Private Shared Sub ControlC_KeyUp(sender As Object, e As KeyEventArgs)
    If m_ControlKeyPressed Then
        If e.KeyValue = 162 OrElse e.KeyValue = 163 Then
            m_ControlKeyPressed = False
        End If
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文