gmamaladze / globalmousekeyhook

This library allows you to tap keyboard and mouse, detect and record their activity even when an application is inactive and runs in background.
MIT License
1.05k stars 257 forks source link

Capturing events from background service #125

Closed ihorbond closed 4 years ago

ihorbond commented 4 years ago

Is there a way to make this work with background service ? Service is running on a Local System account and I had to run application loop on a separate thread so that it doesn't halt the service startup but nothing is recorded ?

    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("In OnStart.");
        try
        {

            Task.Run(() => {
                ApplicationContext msgLoop = new ApplicationContext();

                m_GlobalHook = Hook.GlobalEvents();
                m_GlobalHook.KeyPress += (object sender, KeyPressEventArgs e) => eventLog1.WriteEntry($"KeyPress: \t {e.KeyChar}");

                Application.Run(msgLoop);
            });

        }
        catch (Exception ex)
        {
            eventLog1.WriteEntry(ex.Message);
        }
    }

ihorbond commented 4 years ago

Closing since I found author's answer