0

im trying to create a custom macro program in C# and I want to know how I can create a low level keyboard hook. I have looked around and have found some but i do not understand how they work or how i can customise it :/

can anyone show me how I can create a keyboard hook that basically does:

once any key is pressed, the int keycode is set to a method (i think the VK code is what i need?)

the exampels i find online seem too complicated for that :/

Thanks :)

Ozzy
  • 10,285
  • 26
  • 94
  • 138
  • Which ones have you found already? – payne Feb 08 '11 at 00:03
  • I have found http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx – Ozzy Feb 08 '11 at 00:04
  • and a few on SO but they seem too complicated :/ – Ozzy Feb 08 '11 at 00:04
  • The MSDN example seems straightforward -- I can't see how to make it much simpler. Have you tried using that as a starting point? Or just compiling it to see how it works? – payne Feb 08 '11 at 00:10
  • I tried compiling it but it just shows an empty cmd box and disappears :/ – Ozzy Feb 08 '11 at 00:20
  • Yeah, this question gets asked twice a week. The two samples that have been posted work fine, if you follow the instructions exactly. It's not going to get very simple; a low-level hook is a complicated thing by nature, and it's really not used all that often. – Cody Gray - on strike Feb 08 '11 at 07:18
  • possible duplicate of [spy keyboard (how to get keyboard digets via C# win forms app)](http://stackoverflow.com/questions/4815945/spy-keyboard-how-to-get-keyboard-digets-via-c-win-forms-app) – Cody Gray - on strike Feb 08 '11 at 07:20
  • @Ozzy: Just noticed that you mentioned you were trying to do this from a console app. I think the answer to [this question](http://stackoverflow.com/questions/1776664/c-low-level-keyboard-hook-not-working) solves your problem. The examples you've been finding on the web assume you're doing it from a WinForms application, not a Console app. – Cody Gray - on strike Feb 08 '11 at 08:29

2 Answers2

3

A Simple C# Global Low Level Keyboard Hook on CodeProject seems about right. It was the second hit on Google when I searched for 'keyboard hook' and the first when I searched for your exact title. Have you tried it?

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
1

I wrote a RAW INPUT example on code project here, it may be helpful as a starting point.

Mike
  • 395
  • 1
  • 4
  • 14