开发者

Capture key events from a Windows Service in C#

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开发者_如何学Python 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


Try keyboard and mouse hook

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜