2010-09-09 190 views

回答

1

好了,所以我对你有一个解决方案,我验证了的作品。尽管你需要一个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 
0

您需要创建一个低级挂钩。 This CodeProject example作品完美,我自己用于学习目的。我稍微修改了一下代码,但这里有一个简单的例子,你可以用这个库做些什么。再次,这仅仅是一个例子,并不能反映最终的代码,但可以很容易地修改以捕获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 
+0

由于生病测试了这一点现在。 – Joseph 2010-09-09 05:49:21

+0

是的,这不会为我工作,该代码不是vb.net,你可以告诉从;最后加上“私有静态” – Joseph 2010-09-09 09:16:32

+0

您只需要在您的项目中引用库,您将编写的代码将是Vb.Net,由于它们的语言非常相似,因此非常容易转换。此外,网络上还有转换器可将C#转换为Vb.Net,反之亦然。你只需要付出一点努力。 – 2010-09-09 11:21:02