在C#Windows服务中捕获键盘事件 [英] Capturing Keyboard events in C# windows service

查看:128
本文介绍了在C#Windows服务中捕获键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用c#创建了Windows服务.每当按下一个键时如何调用函数,并在此函数中获取键的值.向上和向下键都需要它.

I have created a windows service in c#. How can I call a function whenever a key is pressed, and in this function get the value of the key. I need it for both key up and key down.

我的目标是在按下某些键序列后发送电子邮件.例如,如果您按h-e-l-l-o,则即使您在桌面上的任何位置键入它,程序都应发送电子邮件.

My goal is to send an email if a certain series of keys were pressed. For example if you press h-e-l-l-o, no matter where you type it even on the desktop, the program should send the email.

另外,我可以用另一种方式实现它.我需要一个在后台运行的程序,并在按下某个键时执行某些操作.

Also, it's ok for me to implement it in another way. I need a program that runs in the background and do something when a key is pressed.

我该怎么做?一个例子会有所帮助.

How can I do such thing? An example would be helpful.

推荐答案

我发现的最佳解决方案:
通常,作者使用 user32.dll 程序集的低级方法创建.NET包装器,这些方法使使用这些方法变得非常令人愉快.

The best solution I found:
Generally author creates .NET wrapper on a low-level methods from user32.dll assembly that make using those methods quite pleasant and enjoyable.

    private LowLevelKeyboardListener _listener;        
    _listener = new LowLevelKeyboardListener();
    _listener.OnKeyPressed += _listener_OnKeyPressed;
    _listener.HookKeyboard();

    void _listener_OnKeyPressed(object sender, KeyPressedArgs e)
    {
        this.textBox_DisplayKeyboardInput.Text += e.KeyPressed.ToString();
    }

此处原始链接

这篇关于在C#Windows服务中捕获键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆