Capture key events from a Windows Service in C#

后端 未结 3 2025
时光取名叫无心
时光取名叫无心 2021-01-22 04:22

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

相关标签:
3条回答
  • 2021-01-22 04:51

    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

    0 讨论(0)
  • 2021-01-22 05:01

    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 讨论(0)
  • 2021-01-22 05:09

    Try keyboard and mouse hook

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

    0 讨论(0)
提交回复
热议问题