• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Intercept system wide keyboard event

I tried to use SetWindowsHookEx API to intercept keyboard event but it works only in the context of the application setting the hook. Any idea?
0
kctan100
Asked:
kctan100
1 Solution
 
alexoCommented:
The usual (99%) cause for this behavior is not using *shared* variables to hold the hook handle, etc. or not putting the hook in a DLL.

First, put the hook into a DLL.

Second, use shared variables.

If you're programming under MSVC do:

  #pragma comment(linker, "-section:.shared,rws")
  #pragma data_seg(".shared")
  HHOOK hHook = NULL; // must be initialized!!!
  // Other shared variables here...
  #pragma data_seg()

For Inprise/Borland compilers, the procedure is similar but a bit more complicated.

For more nformation about Windows hooks (including examples):
    http://msdn.microsoft.com/library/sdkdoc/winbase/hooks_9rg3.htm
    http://msdn.microsoft.com/library/techart/msdn_hooks32.htm
    http://www.vcdj.com/online/articles/aug99/hooks1.asp
    http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10063288  (costs 5 points)
    http://www.experts-exchange.com/jsp/qShow.jsp?ta=winprog&qid=10069661  (costs 4 points)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now