globalmousekeyhook icon indicating copy to clipboard operation
globalmousekeyhook copied to clipboard

.net core compatibility

Open hrkrx opened this issue 6 years ago • 10 comments

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

hrkrx avatar Apr 20 '18 07:04 hrkrx

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

zaafar avatar Jan 05 '19 23:01 zaafar

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...

ge-xin avatar Jan 05 '19 23:01 ge-xin

@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

hrkrx avatar Jan 07 '19 15:01 hrkrx

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?

zaafar avatar Jan 07 '19 23:01 zaafar

@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

hrkrx avatar Jan 08 '19 18:01 hrkrx

Thanks! Looking forward to the example.

zaafar avatar Jan 08 '19 22:01 zaafar

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

magol avatar Jan 19 '19 21:01 magol

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

tebjan avatar Apr 11 '19 19:04 tebjan

@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.

imukai avatar Oct 27 '19 20:10 imukai

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

Meetsch avatar Jan 27 '20 15:01 Meetsch