使用Windows服务读取击键 [英] Reading keystrokes using windows service

查看:89
本文介绍了使用Windows服务读取击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Techies,



当用户从键盘按下特定键时,我想要启动一些应用程序,为此我想使用WndProc方法。



我在WinForms应用程序中取得了成功。我想开发一个Windows服务来实现这个目标。

尝试在Windows服务中使用代码时出现以下错误。



1. RegHotKeys.CreateParams:找不到合适的方法来覆盖

2. RegHotKeys.WndProc(参考消息):找不到合适的方法来覆盖

3. ServiceBase没有包含WndProc的定义

4. ServiceBase不包含CreateParams的定义



以下是代码片段:



Hi Techies,

I have some applications which I want to launch when user presses specific keys from the keyboard, and for this I want to use WndProc method.

I am successful in this in a WinForms application. I want to develop a Windows Service to achieve this.
I get the following errors while trying to use the code in a Windows Service.

1. RegHotKeys.CreateParams : no suitable method found to override
2. RegHotKeys.WndProc(ref Message) : no suitable method found to override
3. ServiceBase does not contain a definition for WndProc
4. ServiceBase does not contain a definition for CreateParams

Following is the code snippet:

protected override void WndProc(ref Message keyPressed)
        {
            Keys keyData = ((Keys)((int)((long)keyPressed.WParam))) | Control.ModifierKeys;

            if (keyPressed.LParam.ToInt32() == hotKey1)
            {
               //ACTION 1
            }
            else if (keyPressed.LParam.ToInt32() == hotKey2)
            {
               //ACTION 2
 
            }
            else if (keyPressed.LParam.ToInt32() == hotKey3)
            {
               //ACTION 3
 
            }
            else if (keyPressed.LParam.ToInt32() == hotKey4)
            { 
               //ACTION 4
            }

            base.WndProc(ref keyPressed);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                var cp = base.CreateParams;
                cp.ExStyle |= 0x80;  // Turn on WS_EX_TOOLWINDOW
                return cp;
            }
        }



所以有人可以建议一下这可能是什么解决方案吗?



我尝试了什么:



我在WinForms应用程序中取得了成功。


So can anybody suggest what could be the possible solution to this please?

What I have tried:

I am successful in this in a WinForms application.

推荐答案

你不能。



服务在一个永远不会收到输入焦点的桌面下运行,因此它永远不会得到任何键盘通知或者鼠标消息。
You can't.

Services run under a desktop that never receives the input focus so it never gets any notification of keyboard or mouse messages.


你不能这样做。



Windows服务是一个非交互式过程。它无法显示任何用户界面,因为它不是交互式会话的一部分。因此,它无法响应窗口消息,因为它无法有一个窗口来接收它们。



坚持用户登录时启动的应用程序如果您希望用户能够触发快捷方式,即使您的应用程序未处于活动状态,您也需要查看使用全局键盘钩子:

一个简单的C#全局低级键盘挂钩 [ ^ ]
You can't do that.

A Windows service is a non-interactive process. It cannot display any user interface, because it's not part of an interactive session. Therefore, it can't respond to window messages, because it can't have a window to receive them.

Stick with an application which starts when the user logs in. If you want the user to be able to trigger the shortcut even if your application isn't active, you would need to look at using a global keyboard hook:
A Simple C# Global Low Level Keyboard Hook[^]


这篇关于使用Windows服务读取击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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