globalmousekeyhook
globalmousekeyhook copied to clipboard
Not possible to run on Console
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
Confirmed nothing works here.
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.
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.
will this work with ServiceBase.Run()
Is there still no support for console programs?
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 have you by any chance tried to make this work with background service ?