• 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?
1 Solution
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://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)
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