【问题标题】:Vb.net Capture Ctrl+CVB.net 捕获 Ctrl+C
【发布时间】:2010-09-09 05:27:34
【问题描述】:

我想在有人使用 CtrlC 时进行捕捉,即使焦点不在。我正在使用 Visual Basic 2010。

【问题讨论】:

    标签: vb.net keyhook


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      您需要创建一个低级挂钩。 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
      

      【讨论】:

      • 是的,这对我不起作用,该代码不是 vb.net,您可以从 ;最后,加上“私有静态”
      • 您只需要在项目中引用该库,您将编写的代码将是 Vb.Net,由于它们的语言非常相似,因此非常容易转换。另外,网络上有一些转换器可以将 C# 转换为 Vb.Net,反之亦然。你只需要付出一点努力。
      • edit 我注意到引用库可能存在问题,我不确定它是否与部分类或什么有关,但我会采取看看我什么时候回家给你发个决议。
      猜你喜欢
      • 1970-01-01
      • 2014-01-09
      • 2011-01-06
      • 2012-10-19
      • 1970-01-01
      • 2012-06-14
      • 2013-11-04
      • 2015-09-13
      • 2011-11-22
      相关资源
      最近更新 更多