globalmousekeyhook icon indicating copy to clipboard operation
globalmousekeyhook copied to clipboard

Not possible to run on Console

Open JPVenson opened this issue 9 years ago • 7 comments

Hey,

I Tried to install a hook from a Console and simply nothing happens.

                _keyListener = new KeyboardHookListener(new GlobalHooker());
                _keyListener.Enabled = true;
                _keyListener.KeyPress += KeyListenerOnKeyPress;

the event is never invoked. Could you please add support for Non Windows Forms/WPF Applications?

regards

JPVenson avatar Apr 08 '15 10:04 JPVenson

Confirmed nothing works here.

paralin avatar May 06 '15 03:05 paralin

I remember we have investigated that issue before. The result was that a windows handle is required to install hooks. We could do further investigation and document results on wiki under FAQ.

gmamaladze avatar May 06 '15 12:05 gmamaladze

I believe this is similar to an issue we had with our implementation of a keylogging feature. Our client application was using ShellExecute = true when starting the process. To get this working, we called ApplicationContext _msgLoop = new ApplicationContext(); Application.Run(_msgLoop); on the thread creating the class that interacts with the key hook logic. The console application just doesn't have a message pipe to output to; you need to start it yourself.

yankejustin avatar May 26 '15 02:05 yankejustin

will this work with ServiceBase.Run()

asulwer avatar Aug 10 '15 20:08 asulwer

Is there still no support for console programs?

arcadeblast avatar May 16 '16 18:05 arcadeblast

In case anyone runs into this, here is a sample that works

class Program
{
    static IKeyboardMouseEvents _hook;

    static void Main(string[] args)
    {
        ApplicationContext msgLoop = new ApplicationContext();

        _hook = Hook.GlobalEvents();
        _hook.KeyDown += _hook_KeyDown;

        Application.Run(msgLoop);
    }

    private static void _hook_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        Console.WriteLine("key {0}", e.KeyCode);

        if (e.KeyCode == Keys.X)
        {
            Application.Exit();
        }
    }
}

tofutim avatar Jul 06 '16 21:07 tofutim

@tofutim have you by any chance tried to make this work with background service ?

ihorbond avatar Apr 24 '20 16:04 ihorbond