Vb.net捕获Ctrl+;C
我想捕捉有人何时使用CtrlC,即使是在离焦时。我正在使用Visual Basic 2010。您需要创建一个低级挂钩。作品完美无瑕,我自己也将其用于学习目的。我稍微修改了代码,但这里有一个简单的示例,说明可以使用该库执行的操作。同样,这只是一个示例,可能不会反映最终的代码,但是可以很容易地进行修改以捕获Control+C,并且该库有很好的文档记录Vb.net捕获Ctrl+;C,vb.net,keyhook,Vb.net,Keyhook,我想捕捉有人何时使用CtrlC,即使是在离焦时。我正在使用Visual Basic 2010。您需要创建一个低级挂钩。作品完美无瑕,我自己也将其用于学习目的。我稍微修改了代码,但这里有一个简单的示例,说明可以使用该库执行的操作。同样,这只是一个示例,可能不会反映最终的代码,但是可以很容易地进行修改以捕获Control+C,并且该库有很好的文档记录 private static bool m_ControlKeyPressed = false; private static void
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
好的,我有一个解决方案给你,我验证了它的有效性。不过,您需要一个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
是的,这对我来说不起作用,你可以从下面的例子中看出,这段代码不是vb.net;最后,加上“private static”,您只需要引用项目中的库,您将编写的代码将是Vb.Net,这非常容易转换,因为它们的语言非常相似。此外,网络上还有一些转换器可以将C#转换为Vb.Net,反之亦然。编辑我注意到引用库可能有问题,我不确定这是否与部分类有关,但我回家后会看一看,然后给你一个解决方案。