C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-10-2008, 05:54 PM   #1
Registered User
 
Join Date: Mar 2006
Location: USA::Colorado
Posts: 148
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~
guitarist809 is offline   Reply With Quote
Old 11-11-2008, 01:43 PM   #2
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,054
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;
				}
BobS0327 is offline   Reply With Quote
Old 11-14-2008, 07:34 PM   #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. ★
execute is offline   Reply With Quote
Old 11-14-2008, 08:14 PM   #4
Registered User
 
Join Date: Mar 2006
Location: USA::Colorado
Posts: 148
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~
guitarist809 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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