Thread: blocking or passing keys with global hook

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    3

    blocking or passing keys with global hook

    I am working on a project that uses a global keyboard hook that looks for the number keys. The program will pass all keys unless it gets a number from the number row or the 10-key pad. When it gets a number it will pop a message box asking if they are sure that is the number they wanted. If it is not it will not pass the number but if it is it will.

    I got the global hook to work well enough but I can't get it to pass or block the keys. It will either block all keys or pass all keys. This is pretty much my first real windows program so I guess I jumped in over my head.

    This is the code I have for my KBHookProc.

    Code:
    EXPORT LRESULT CALLBACK KBHookProc(int Code, WPARAM wParam, LPARAM lParam)
    {
        char yesno=0;
    	
        if (Code < 0)
            return(CallNextHookEx(kbhook, Code, wParam, lParam));
    
        if (lParam & (1 << 30)) // Check whether key was pressed(not released) 31 = key up, 30 = key down
        {   // check if the key is one of the number keys (number row or 10-key)
            if(((wParam>=48) && (wParam<=57)) || ((wParam>=96) && (wParam<=105)))
            {
                switch(wParam)
                {
                    case 48:    // 0 on num row
                    case 96:    // 0 on 10-key
                        yesno = MessageBox(NULL, "You pressed 0\nIs this what you want?", 
                                                 "Are you sure?", MB_YESNO + MB_ICONQUESTION);
                        break;
                    case 49:    // 1 on num row
                    case 97:    // 1 on 10-key
                        yesno = MessageBox(NULL, "You pressed 1\nIs this what you want?",
                                                 "Are you sure?", MB_YESNO + MB_ICONQUESTION);
                        break;
                    case 50:    // 2 on num row
                    case 98:    // 2 on 10-key
                        yesno = MessageBox(NULL, "You pressed 2\nIs this what you want?",
                                                 "Are you sure?", MB_YESNO + MB_ICONQUESTION);
                        break;
                    case 51:    // 3 on num row
                    case 99:    // 3 on 10-key
                        yesno = MessageBox(NULL, "You pressed 3\nIs this what you want?",
                                                 "Are you sure?", MB_YESNO + MB_ICONQUESTION);
                        break;
                    case 52:    // 4 on num row
                    case 100:   // 4 on 10-key
                        yesno = MessageBox(NULL, "You pressed 4\nIs this what you want?",
                                                 "Are you sure?", MB_YESNO + MB_ICONQUESTION);
                        break;
                    case 53:    // 5 on num row
                    case 101:   // 5 0n 10-key
                        yesno = MessageBox(NULL, "You pressed 5\nIs this what you want?",
                                                 "Are you sure?", MB_YESNO + MB_ICONQUESTION);
                        break;
                    case 54:    // 6 on num row
                    case 102:   // 6 on 10-key
                        yesno = MessageBox(NULL, "You pressed 6\nIs this what you want?",
                                                 "Are you sure?", MB_YESNO + MB_ICONQUESTION);
                        break;
                    case 55:    // 7 on num row
                    case 103:   // 7 on 10-key
                        yesno = MessageBox(NULL, "You pressed 7\nIs this what you want?",
                                                 "Are you sure?", MB_YESNO + MB_ICONQUESTION);
                        break;
                    case 56:    // 8 on num row
                    case 104:   // 8 on 10-key
                        yesno = MessageBox(NULL, "You pressed 8\nIs this what you want?",
                                                 "Are you sure?", MB_YESNO + MB_ICONQUESTION);
                        break;
                    case 57:    // 9 on num row
                    case 105:   // 9 on 10-key
                        yesno = MessageBox(NULL, "You pressed 9\nIs this what you want?",
                                                 "Are you sure?", MB_YESNO + MB_ICONQUESTION);
                        break;
                    default:
                        break;
                }
    			
                if(yesno == 7)
                    return(CallNextHookEx(kbhook, 0, wParam, lParam));
            }
        }
        return(CallNextHookEx(kbhook, Code, wParam, lParam));
    }
    I searched the boards and couldn't find any information, I may have just missed it, if this is the case then please post a link to that thread. If you know any way I can do this please let me know. Thanks.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    if(yesno == IDYES)
        return(CallNextHookEx(kbhook, Code, wParam, lParam));
    }
    else{
        return -1
    }
    Your code calls CallNextHookEx anyway, if that "if yesno" failed in your code it called the one in the end of the function.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you do a switch statement, you don't REALLY need to do
    Code:
            if(((wParam>=48) && (wParam<=57)) || ((wParam>=96) && (wParam<=105)))
    Any case outside those values will end up in default because the compiler will produce code to do something very similar to the above if-statement to jump to default if it's outside the values in your switch [this applies if the case-statements produce a reasonable sequence of numbers, rather than soemthing that is very spread-out (e.g. 10, 1000, 12654 and 47111923), where the compiler will make a branch-tree comparing the numbers based on a binary search tree or similar].


    --
    Mats

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    @maxorator:
    I tried that and I am still either passing all keys or blocking all. I dont know.

    @matsp:
    of course, thank you. it doesn't hurt, but it doesn't help so its wasted code.

    I'm not getting it to work so I've been thinking istead of trying to block specific keys, maybe I should just block all keys and then simulate all the ones that aren't numbers. Is there a method to pass a simulated keyhook?

    or if you think it may be useful to look through all my code to see what I'm doing and then think of a way let me know and I can send it or I can figure out the forums upload functionality.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    I got it to do what I wanted. Thank you everyone for your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulate Keys with a Keyboard Hook
    By guitarist809 in forum Windows Programming
    Replies: 3
    Last Post: 11-14-2008, 08:14 PM
  2. C++ Global Keyboard Hook
    By darknite135 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2008, 07:36 PM
  3. Global scope and passing pointer
    By Dave++ in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2007, 03:52 PM
  4. Global hook only works on one window?
    By CornedBee in forum Windows Programming
    Replies: 5
    Last Post: 01-19-2004, 12:25 PM
  5. Passing Keys (macro program)
    By Coder87C in forum C Programming
    Replies: 3
    Last Post: 07-08-2003, 06:54 PM