简体   繁体   English

从 C# 中的 Windows 服务捕获关键事件

[英]Capture key events from a Windows Service in C#

I have to write an application in C# that listens to any keys being pressed.我必须用 C# 编写一个应用程序来监听任何被按下的键。 In actuality I have a bar code scanner sending the "key pressed" event, and I need to listen it... what it does from there is beyond the scope of my question.实际上,我有一个条形码扫描仪发送“按键按下”事件,我需要听它……它的作用超出了我的问题范围。

My security requirements are that there is not allowed to be any sign-on to the machine in any way shape or form AND this must run as a windows service .我的安全要求是不允许以任何形式或形式对机器进行任何登录,并且这必须作为 Windows 服务运行 The user will start the machine and walk away (ie, no desktop session).用户将启动机器并走开(即,没有桌面会话)。

I'm assuming I'm going to have to go unmanaged for this.我假设我将不得不为此不受管理。

Given the security requirements is this even possible?鉴于安全要求,这甚至可能吗? If so, any pointers on where to start would be great.如果是这样,任何关于从哪里开始的指示都会很棒。

Thanks in advance, Jay提前致谢,杰

If you need to see keyboard presses for all apps, system-wide Hooks would be the way to go normally, but the problem is that the security changes in Vista and above make hooks rather less useful.如果您需要查看所有应用程序的键盘按下情况,系统范围的 Hooks 将是正常使用的方法,但问题是 Vista 及更高版本中的安全更改使 hooks 变得不太有用。 A process cannot hook another process at a higher integrity level, so there's no guarantee you will see all events.一个进程不能在更高的完整性级别挂钩另一个进程,因此不能保证您会看到所有事件。

However if you only need to see events going to a particular app - the one reading the bar codes - then provided you can identify that process, a thread-specific hook will suffice and the integrity question will not arise.但是,如果您只需要查看进入特定应用程序的事件(读取条形码的应用程序),那么只要您可以识别该进程,线程特定的挂钩就足够了,并且不会出现完整性问题。

You will have to learn pInvoke with combination of learning how to look for the right events produced at the lower level of OS.您将必须学习 pInvoke 并结合学习如何查找在较低级别的操作系统中生成的正确事件。 By calling into user32.dll using pInvoke your managed code can filter those events through the hooks.通过使用 pInvoke 调用 user32.dll,您的托管代码可以通过钩子过滤这些事件。

http://pinvoke.net/default.aspx/user32.SetWindowsHookEx http://pinvoke.net/default.aspx/user32.SetWindowsHookEx

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

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