Changeset 2271 for keyHook/keyHook.c

Show
Ignore:
Timestamp:
07/18/08 05:17:06 (6 months ago)
Author:
jteh
Message:

keyHook and mouseHook:

  • Remove the isInitialized variable, which is unnecessary and was never used properly anyway. The hook ID Is now used to determine whether the hook has been initialized.
  • initialize():
    • SetWindowsHookEx? returns NULL on failure, so only check for NULL instead of <= 0.
    • Can no longer be called twice without an intervening call to terminate().
  • terminate():
    • Fix the issue where this function never unregistered the hook at all due to the broken use of isInitialized.
    • Now returns an int like initialize(): 0 on success, -1 on failure.
    • -1 is returned if already initialized or the hook could not be unregistered.
    • Clean up state so that initialize() can be called again if desired.
  • Update copyright headers.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • keyHook/keyHook.c

    r1146 r2271  
    11//keyHook.c 
    2 //Copyright (c) 2007 Michael Curran <mick@kulgan.net> 
     2//Copyright (c) 2007-2008 Michael Curran <mick@kulgan.net> 
    33//This file is covered by the GNU General Public Licence 
    4 //See the file Copying for details. 
     4//See the file copying.txt for details. 
    55 
    66#define UNICODE 
     
    1313HINSTANCE moduleHandle; 
    1414HHOOK keyHookID=0; 
    15 BOOL isInitialized=FALSE; 
    1615 
    1716#pragma comment(linker,"/entry:_DllMainCRTStartup@12") 
     
    4746 
    4847int initialize(keyCallback_t downCallback, keyCallback_t upCallback) { 
    49         if(isInitialized) { 
     48        if(keyHookID) { 
    5049                fprintf(stderr,"Already initialized\n"); 
    5150                return -1; 
     
    5352        keyDownCallback=downCallback; 
    5453        keyUpCallback=upCallback; 
    55         if((keyHookID=SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)keyHook,moduleHandle,0))<=0) { 
     54        if(!(keyHookID=SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)keyHook,moduleHandle,0))) { 
    5655                fprintf(stderr,"Error registering key hook\n"); 
    5756                return -1; 
     
    6059} 
    6160 
    62 void terminate() { 
    63         if(isInitialized) { 
    64                 UnhookWindowsHookEx(keyHookID); 
     61int terminate() { 
     62        if(keyHookID) { 
     63                if(UnhookWindowsHookEx(keyHookID)) { 
     64                        keyHookID=0; 
     65                        return 0; 
     66                } 
    6567        } 
     68        return -1; 
    6669}