![]() |
| | #1 |
| Registered User Join Date: Mar 2006 Location: USA::Colorado
Posts: 148
| Simulate Keys with a Keyboard Hook 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);
}
Thanks in advance for the help, Guitarist809
__________________ ~guitarist809~ |
| guitarist809 is offline | |
| | #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:
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 | |
| | #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 | |
| | #4 | |
| Registered User Join Date: Mar 2006 Location: USA::Colorado
Posts: 148
| Quote:
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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |