Question

I'm somewhat new to C# on the whole and I'm developing a small program to render my backlog of CAD stuff when I'm idle. I'm using the MouseKeyboardActivityMonitor library found here: https://globalmousekeyhook.codeplex.com/ but having major problems such as input frozen upon the launch of my program and from there out no user input is detected by the hooks. I am using Windows 8.1 x64 and compiling both the DLL and my own executable towards .NET 4.0

Note: I am using Version 3 of the project

This is my code (just a small test of the hooks):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;
using System.Windows.Forms;

namespace HookTest1
{
class Program
{
    static MouseHookListener mouseListener;
    static KeyboardHookListener keyListener;

    static void Main(string[] args)
    {
        Activate();

        while (true)
        {
            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
        }
    }

    private static void OnUserActivity()
    {
        Console.Write("UserAct");
    }

    private static void Activate()
    {
        mouseListener = new MouseHookListener(new GlobalHooker());
        mouseListener.Enabled = true;
        mouseListener.MouseDown += OnMouseActivity;
        mouseListener.MouseMove += OnMouseActivity;
        mouseListener.MouseWheel += OnMouseActivity;

        keyListener = new KeyboardHookListener(new GlobalHooker());
        mouseListener.Enabled = true;
        keyListener.KeyDown += OnKeyActivity;
    }

    private static void OnMouseActivity(Object sender, System.Windows.Forms.MouseEventArgs e)
    {
        OnUserActivity();
    }

    private static void OnKeyActivity(Object sender, System.Windows.Forms.KeyEventArgs e)
    {
        OnUserActivity();
    }
}
}

Thanks for any and all help!

Was it helpful?

Solution

You may have added this somewhere and just removed it in your sample... but have you called

Application.Run();

in Main()? As I understand it, you're attempting to hook window events without a message loop going. Application.Run will get it running.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top