Simulate Keys with a Keyboard Hook

This is a discussion on Simulate Keys with a Keyboard Hook within the Windows Programming forums, part of the Platform Specific Boards category; Hey, I've got a keyboard hook that drops the keys A-Z when they're pressed (shows in console that they were ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Simulate Keys with a Keyboard Hook

    Hey,

    I've got a keyboard hook that drops the keys A-Z when they're pressed (shows in console that they were pressed, but they don't make it to the processing queue). I'm now trying to figure out how to make it so that it will send a different key to the queue instead of the one pressed.

    Here's my code that drops the keys and simulates another one
    Code:
    #define WIN32_LEAN_AND_MEAN
    
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    	PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) lParam;
    	BOOL fEatKeystroke = FALSE;
    
    	if (nCode == HC_ACTION)
    	{
    		switch (wParam)
    		{
    		case WM_KEYDOWN:
    		case WM_SYSKEYDOWN:
    		case WM_KEYUP:
    		case WM_SYSKEYUP:
    			{	
    				//block out keys A-Z
    				if ( p->vkCode <= 90 && p->vkCode >= 41 )
    				{
    					fEatKeystroke = true;
    				}
    
    				//if we're gonna drop the keystroke
    				if (fEatKeystroke)
    				{
    					//Show on the console that the key was pressed...
    					cout << (char)p->vkCode << " Pressed, scancode: " << (p->scanCode) << endl;
    
    					//Simulate another random key (random # that I came up with).
    					p->vkCode = 70;
    					p->scanCode = 30;
    
    					//This probably isn't necessary, but I'm doing it instead
    					lParam = (LPARAM)p;
    				}
    				break;
    			}
    		}
    	}
    
    	//Call the next hook if the key isn't going to be dropped...
    	//return(fEatKeystroke ? 1 : CallNextHookEx(NULL, nCode, wParam, lParam));
    	return CallNextHookEx ( NULL, nCode, wParam, lParam );
    }
    
    int main ( void )
    {
    	// Install the low-level keyboard & mouse hooks
    	HHOOK hhkLowLevelKybd  = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, 0);
    
    	// Keep this app running until we're told to stop
    	MessageBox(NULL,
    		TEXT("Click \"Ok\" to terminate this application."),
    		TEXT("Disable Low-Level Keys"), MB_OK);
    
    	UnhookWindowsHookEx(hhkLowLevelKybd);
    
    	return(0);
    }
    However, this code doesn't simulate a different key (still forwards the keys I pressed). How can I simulate keys with this hook?

    Thanks in advance for the help,

    Guitarist809
    ~guitarist809~

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    The following code is putting two characters in your buffer, one when the keydown message is processed and the second when the key up message is processed. In other words, hitting the A key once will put two A's in your buffer

    Code:
    switch (wParam)
    		{
    		case WM_KEYDOWN:
    		case WM_SYSKEYDOWN:
    		case WM_KEYUP:
    		case WM_SYSKEYUP:
    Remove WM_KEYUP and WM_SYKEYUP;

    Also, you really need to use the ToASCII function to translate the keyboard vitual codes to ASCII codes and then determine the numeric equivalent of the ASCII characters.
    The following code will not work.

    Code:
    //block out keys A-Z
    				if ( p->vkCode <= 90 && p->vkCode >= 41 )
    				{
    					fEatKeystroke = true;
    				}

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    I do wanna ask though, wouldn't it be just as good to use GetAsyncKeyState() ?
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by execute View Post
    I do wanna ask though, wouldn't it be just as good to use GetAsyncKeyState() ?
    Actually no.

    I'm trying to make a program that simulates keypress by pressing a key and simulating a whole other key. So if someone presses the left mouse button, it will instead simulate the right mouse button, completely blocking the left mouse button from ever reaching any applications.
    ~guitarist809~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. advanced lock/unlock keys on keyboard
    By fbs777 in forum Linux Programming
    Replies: 0
    Last Post: 09-22-2008, 08:54 PM
  2. blocking or passing keys with global hook
    By pmouse in forum Windows Programming
    Replies: 4
    Last Post: 08-29-2007, 02:54 PM
  3. Keyboard hook
    By joecaveman in forum Windows Programming
    Replies: 2
    Last Post: 09-03-2005, 08:07 AM
  4. Keyboard Hook
    By jmd15 in forum Windows Programming
    Replies: 19
    Last Post: 08-07-2005, 03:11 PM
  5. How to keep static data in a keyboard hook?
    By junbin in forum Windows Programming
    Replies: 1
    Last Post: 01-19-2003, 02:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21