I am creating a clipboard monitor that will catch changes in clipboard and then convert the clipboard to pure text only. As such this works fine but I really cannot figure out why my application does not hook the keyboard when being inside an elevated application like e.g. an admin command prompt or Task Manager. It just simply ignores my keypress!?
I have tried doing the keyboard "manually" via this post, How to stop further processing global hotkeys in C# but now I am trying out another project named globalmousekeyhook
, https://github.com/gmamaladze/globalmousekeyhook
I get the same problem in both solutions.
My question is, "how can I hook and get the key combination ALT+H
visible in my application from within an application that has been elevated"? It seems to work fine from anywhere else.
This code is more or less from the example code from globalmousekeyhook
:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Define the global hotkey combination
var altH = Combination.FromString("Alt+H");
// 2. Define actions
Action actionAltH = () => { Console.WriteLine("You Pressed ALT+H"); };
// 3. Assign actions to key combinations
var assignment = new Dictionary<Combination, Action>
{
{altH, actionAltH}
};
// 4. Install listener
Hook.GlobalEvents().OnCombination(assignment);
}
}
}
My application is not elevated - it just runs normally when launched.
What am I missing here? Can anyone point me in the right direction? :-)
The comment of Hans Passant is correct!
The UAC elevation was invented to prevent keylogging. So it is correct, that an application which have no elevated rights can not hook into an elevated application.
If you run your application with elevated rights, it will work.
If you just want to hook some Hotkeys you can register system wide Hotkeys. See this article at code project (VB.net).
You can find a C# sample for Winform and WPF in this code project article. Take a look into the comments of this C# article, too.