1

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? :-)

Beauvais
  • 2,149
  • 4
  • 28
  • 63
  • Preventing this from working is the *exact* reason why UAC elevation was invented. – Hans Passant Oct 17 '20 at 17:00
  • ??? This is working from other similar clipboard managers with the exact same hotkey setting, so I must be doing something wrong but I don't know what. – Beauvais Oct 17 '20 at 17:52
  • "My application is not elevated". Well, then how do you expect it hook elevated applications? You can't hook elevated applications from an unelevated application, otherwise unpriv'd users could watch anything and everything an admin types into the same computer. It's deeply unsafe. – antiduh Nov 01 '20 at 08:23

1 Answers1

0

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.

chrkon
  • 21
  • 5
  • OK, this may be correct but then how is it possible for other sil´milar appliications to do that exact thing? E.g. ClipboardFusion is a clipboard manager and it does not run with elevated rights - and its hotkeys works from an elevated application. So how is this possible if you are correct? – Beauvais Oct 20 '20 at 05:11
  • I have tested the ClipboardFusion manager and it doesn't use KeyLogging! In the settings of the Clipboard Manager you can only define **Hotkeys**. As you wrote correctly the **Hotkeys** are working in elevated applications. But **Hotkeys** does not mean **KeyLogging**! Perhaps this [Link](https://www.codeproject.com/articles/4345/net-system-wide-hotkey-component) will help to define your own **Hotkeys**: – chrkon Oct 21 '20 at 20:02
  • I have added the Link to my Answer, too. – chrkon Oct 21 '20 at 21:31
  • The page you refer to does not show C# code, so I implemented a similar project, https://www.fluxbytes.com/csharp/how-to-register-a-global-hotkey-for-your-application-in-c/ but still with the exact same behaviour - it cannot detect the global hotkey in e.g. an elevated command prompt. When I press `SHIFT+a` then it will just show `A` in the command prompt. Is there no way to detect the hotkey in an elevated application so I view my previous clipboard entries (this is what I need the hotkey for)? – Beauvais Oct 30 '20 at 19:03
  • @Beauvis You are right. That sample was in VB.net. I have added a C# sample to the answer. Perhaps you can find some hints in [this project](https://www.codeproject.com/Articles/442285/Global-Shortcuts-in-WinForms-and-WPF). – chrkon Nov 01 '20 at 08:11
  • @Beauvais : I have checked your code and you are partly right again. With this code you can not handle the HotKey "Shift-A" of an elevated command prompt, if your app is not running in elevated mode, too! BUT if you register "CTRL-A" as Hotkey in your sample, you can handle it. Even if the command prompt is running in elevated mode! It seems, that the elevated command prompt handles the "Shift-A" before it is recognized by the system as HotKey. (sorry for wrong spelling your name in my previous answer) – chrkon Nov 01 '20 at 09:21