tags:

views:

25

answers:

2

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

A: 

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
David Anderson
thanks ill test this out now.
Joseph
yea this wont work for me, that code isnt vb.net, you can tell from the ; at the end, plus "private static"
Joseph
You just need to reference the library in your project, the code you will write will be Vb.Net, which is extremely easy to convert as they languages are extrordinarily similar. Plus, there are converters on the web that will convert C# to Vb.Net and visa-versa. You just have to make a little effort.
David Anderson
*edit* I noticed there may be an issue with referencing the library, I'm not sure if it has to do with the partial classes or what, but I will take a look when I get home and post you a resolution.
David Anderson
+1  A: 

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
David Anderson