简体   繁体   English

当我的应用程序在后台时监视 C# 中的键盘活动

[英]Monitoring keyboard activity in C# while my application is in the background

First of all I need to make it clear that I have no interest in keylogging.首先,我需要明确表示我对键盘记录不感兴趣。

I need a way to monitor keyboard activity at the most basic level while my application is in the background.当我的应用程序在后台时,我需要一种方法来在最基本的级别监视键盘活动。 I don't need to know which keys, I don't need to save any data, I don't need or plan to hide my application at all, all I need is to know when keys are pressed and invoke a method.我不需要知道哪些键,我不需要保存任何数据,我根本不需要或计划隐藏我的应用程序,我只需要知道什么时候按下键并调用一个方法。

I'm looking for the simplest way to do this possible, I know a reasonable amount of C# but nothing too complex as most of my knowledge is self-taught.我正在寻找最简单的方法来做到这一点,我知道相当数量的 C#,但没有什么太复杂的,因为我的大部分知识都是自学的。

I've looked around for some appropriate ways of doing this and I've found nothing useful.我环顾四周寻找一些合适的方法来做到这一点,但我没有发现任何有用的东西。 All I've found is a bunch of people saying "No, that's illegal" on forums and source code for in depth keyloggers.我发现的只是一堆人在论坛和深入键盘记录器的源代码上说“不,这是非法的”。

If any of you could advise me on a way to achieve this then I would be most appreciative.如果你们中的任何人可以就实现这一目标的方式向我提出建议,那么我将不胜感激。

There is an article that has working sample for what you require(It also monitors keyboard and mouse events in the background) at : 有一篇文章提供了您所需的工作示例(它还在后台监视键盘和鼠标事件):

Processing Global Mouse and Keyboard Hooks in C# 在C#中处理全局鼠标和键盘挂钩

You'll need to use Window Hooks:您需要使用窗口挂钩:

Low-Level Keyboard Hook in C# C# 中的低级键盘钩子

But beware, Windows security, may be protecting us from doing what you want!但请注意,Windows 安全性可能会保护我们不做您想做的事!

Microsoft tells you How to: Handle Keyboard Input at the Form Level . Microsoft 告诉您如何:在表单级别处理键盘输入 As long as you handle the same event(s) this works for any non web application.只要您处理相同的事件,这适用于任何非 Web 应用程序。

You should also take a look at the other questions here on SO, such as Handling Input from a Keyboard Wedge您还应该查看 SO 上的其他问题,例如从键盘楔处理输入

You can monitor keyboard and mouse activity in the background with the Nuget package MouseKeyHook ( GitHub ).您可以与NuGet包的后台监控器的键盘和鼠标活动MouseKeyHookGitHub上)。

This code detects when a key is pressed:此代码检测何时按下某个键:

    private IKeyboardMouseEvents _globalHook;

    private void Subscribe()
    {
        if (_globalHook == null)
        {
            // Note: for the application hook, use the Hook.AppEvents() instead
            _globalHook = Hook.GlobalEvents();
            _globalHook.KeyPress += GlobalHookKeyPress;
        }
    }

    private static void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
    {
        Console.WriteLine("KeyPress: \t{0}", e.KeyChar);
    }

    private void Unsubscribe()
    {
        if (_globalHook != null)
        {
            _globalHook.KeyPress -= GlobalHookKeyPress;
            _globalHook.Dispose();
        }
    }

You will need to call Subscribe() to start listening, and Unsubscribe() to stop listening.您需要调用Subscribe()开始收听,并调用Unsubscribe()停止收听。 Obviously you need to modify GlobalHookKeyPress() to do useful work.显然你需要修改GlobalHookKeyPress()来做有用的工作。

I needed this functionality in order to write a utility which will turn on the keyboard backlight on a Lenovo Thinkpad when any key is pressed, including CTRL (which KeyPress doesn't catch).我需要这个功能来编写一个实用程序,当按下任何键时,该实用程序将打开联想 Thinkpad 上的键盘背光,包括 CTRL( KeyPress没有捕捉到)。 For this purpose, I had to monitor for key down instead.为此,我不得不监视按键关闭。 The code is the same except we attach to a different event...除了我们附加到不同的事件之外,代码是相同的......

    _globalHook.KeyDown += GlobalHookOnKeyDown;

and the event handler signature is different:并且事件处理程序签名不同:

    private static void GlobalHookOnKeyDown(object sender, KeyEventArgs e)
    {
        Console.WriteLine("KeyDown: \t{0}", e.KeyCode);
    }

The library can also detect specific key combinations and sequences .该库还可以检测特定的组合键和序列 For example:例如:

    Hook.GlobalEvents().OnCombination(new Dictionary<Combination, Action>
    {
        { Combination.TriggeredBy(Keys.A).Control(), () => { Console.WriteLine("You Pressed CTRL+A"); } },
        { Combination.FromString("Shift+Alt+Enter"), () => { Console.WriteLine("You Pressed FULL SCREEN"); } }
    });

You could register Windows Hot Key with RegisterHotKey windows API, look at this blog post :您可以使用 RegisterHotKey windows API 注册 Windows 热键,请查看此博客文章:

http://www.liensberger.it/web/blog/?p=207 http://www.liensberger.it/web/blog/?p=207

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM