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

.net core compatibility #98

Open hrkrx opened 6 years ago

hrkrx commented 6 years ago

I stumbled upon this project via the old code project (https://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C) and i made it work in .net core

Basically i made a very simple MessageLoop in a seperate Thread where the globalhook is called from.

The benefits in my opinion are that it removes many all of the required dependencies on Forms/WPF for the hooks to work.

I didn't dive in this git deep yet, but if there is interest i could help to improve the library.

This could also make it easier to port it to Linux/Mac

zaafar commented 5 years ago

@hrkrx Is there an example program on how to use it in .net core framework?

ge-xin commented 5 years ago

I really doubt on the idea of using it in .NET Core. Because I noticed some Win32 API calls in this project... I don't think Win32 APIs would work in Mac OS and Linux...

hrkrx commented 5 years ago

@zaafar basically i removed all Forms/WPF dependencies and fixed all errors by implementing a MessageLoop and the enums for the interaction codes (eg. some number => some KeyPress)

@ge-xin i actually have a prototype working on Linux which detects the OS and if on Windows uses the Win32 API calls and if not tries to open the special devices file in /dev/input... but it still is a prototype so it has issues like it only supports keyboard input for now

zaafar commented 5 years ago

Hi @hrkrx , thank you for the reply. I tried using your fork of the library to hook into the global keyboard/mouse events but failed. Basically, I created a new .NET Core 2.1 Console Application project with following code in it. Let me know what I did wrong.

    using FormsReplacementLib;
    using Gma.System.MouseKeyHook;
    using System;
    using System.Threading;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Hook.GlobalEvents().MouseClick += Hook_MouseClick;
            Console.WriteLine("Bye World!.");
            -> Application.Run(new ApplicationContext());
        }

        private static void Hook_MouseClick(object sender, MouseEventArgs e)
        {
            Console.WriteLine($"{e.X}, {e.Y}");
        }
    }

EDIT: I am using windows10 x64.

EDIT2: Updated the code, now I need to understand how can I do Application.Run(new ApplicationContext()); in .NET Core?

hrkrx commented 5 years ago

@zaafar the code in my Fork is a bit older, i will update this in the next few days and then i'll add some example code here

zaafar commented 5 years ago

Thanks! Looking forward to the example.

magol commented 5 years ago

Excellent work. I hope you reach the goal of this work.

tebjan commented 5 years ago

@hrkrx did you update already? i would also be interested in seeing the latest code.

imukai commented 4 years ago

@hrkrx has it been a few days yet? Going on 11 months... was curious about the .core implementation of this as well as the others.

Meetsch commented 4 years ago

@hrkrx Could you update your fork to provide to latest code for GlobalMouseKeyHook .NET Core compatibility?