Capture key events from a Windows Service in C#

杀马特。学长 韩版系。学妹 提交于 2019-12-20 03:40:16

问题


I have to write an application in C# that listens to any keys being pressed. 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. The user will start the machine and walk away (i.e., 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


回答1:


Try keyboard and mouse hook

http://www.codeproject.com/KB/cs/globalhook.aspx




回答2:


You will have to learn pInvoke with combination of learning how to look for the right events produced at the lower level of OS. By calling into user32.dll using pInvoke your managed code can filter those events through the hooks.

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




回答3:


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. 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.



来源:https://stackoverflow.com/questions/4251023/capture-key-events-from-a-windows-service-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!