home *** CD-ROM | disk | FTP | other *** search
- /*
- SETEHOOK.C -- Example program for SetEventHook
-
- From Chapter 6 of "Undocumented Windows" (Addison-Wesley 1992)
- by Andrew Schulman, Dave Maxey and Matt Pietrek
-
- Build using: WINIOBC SETEHOOK EVNTHOOK (for Borland C++ v3.00)
- WINIOMS SETEHOOK EVNTHOOK (for Microsoft C/SDK)
-
- MUST be compiled SMALL model because of model
- dependency in EVNTHOOK.ASM
- */
-
- #include <windows.h>
- #include "wmhandlr.h"
- #include "winio.h"
-
- /* undocumented function */
- extern void FAR PASCAL SetEventHook(FARPROC lpfnEventHook);
-
- /* the Assembler routine from EVNTHOOK.ASM */
- extern void far EventHook(void);
-
- char * szEvent[] = { "WM_KEYUP", "WM_KEYDOWN", "WM_MOUSEMOVE",
- "WM_LBUTTONDOWN", "WM_LBUTTONUP", "WM_RBUTTONDOWN",
- "WM_RBUTTONUP", "WM_MBUTTONDOWN", "WM_MBUTTONUP", "WM_???" };
-
- int wEvent = -1;
- int wKeyInfo = -1;
- int iEvent;
-
- #include "checkord.c"
-
- BOOL DeinstHook(HWND hwnd)
- {
- puts("Deinstalling event hook function...");
- SetEventHook(NULL);
- puts("...successfully deinstalled.");
- return TRUE;
- }
-
- int main()
- {
- // Ord/name check
- if (! CheckOrdName("SetEventHook", "USER", 321))
- return 0;
-
- winio_about("SETEHOOK\nIllustrates SetEventHook"
- "\n\nFrom Chapter 6 of"
- "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
- "\nby Andrew Schulman, David Maxey and Matt Pietrek"
- );
-
- puts("Installing an event hook function...");
- SetEventHook((FARPROC) EventHook);
- puts("...successfully installed.");
-
- // Make sure that we always unhook...
- winio_onclose(winio_current(), (DESTROY_FUNC) DeinstHook);
-
- for (;;)
- {
- wmhandler_yield();
- if ((wEvent == -1) && (wKeyInfo == -1))
- continue;
-
- switch (wEvent) {
- case WM_KEYUP: iEvent = 0; break;
- case WM_KEYDOWN: iEvent = 1; break;
- case WM_MOUSEMOVE: iEvent = 2; break;
- case WM_LBUTTONDOWN: iEvent = 3; break;
- case WM_LBUTTONUP: iEvent = 4; break;
- case WM_RBUTTONDOWN: iEvent = 5; break;
- case WM_RBUTTONUP: iEvent = 6; break;
- case WM_MBUTTONDOWN: iEvent = 7; break;
- case WM_MBUTTONUP: iEvent = 8; break;
- default: iEvent = 9;
- }
-
- if (iEvent < 2)
- printf("%s - Scan=0x%02X, VK code=0x%02X\n",
- szEvent[iEvent],
- HIBYTE(wKeyInfo), LOBYTE(wKeyInfo));
- else
- puts(szEvent[iEvent]);
-
- if ((wEvent == WM_KEYDOWN) &&
- (LOBYTE(wKeyInfo) == VK_ESCAPE))
- {
- puts("** ESC pressed");
- DeinstHook(NULL);
- winio_onclose(winio_current(), (DESTROY_FUNC) NULL);
- break;
- }
- wEvent = -1;
- wKeyInfo = -1;
- }
-
- puts("\nProgram terminated");
- return 0;
- }
-
-