How to Change Lparam before calling CallNextHookEx()?

N

nash

Hi All,
I am working on Message Hooking.I am trying to change the value of
lParam before calling CallNextHookEx() function.. and its value is
getting changed but the contents remains same.. Where as if I am able to
change wParam. For Eg: if I press any key 'S' and if I want to modify it
to 'B' by using wParam its possible.. Can u now please help me in
modifying this 'S' to 'B' using lParam?


Regards,
~Nash
 
B

Bruno van Dooren [MVP VC++]

I am working on Message Hooking.I am trying to change the value of
lParam before calling CallNextHookEx() function.. and its value is
getting changed but the contents remains same.. Where as if I am able to
change wParam. For Eg: if I press any key 'S' and if I want to modify it
to 'B' by using wParam its possible.. Can u now please help me in
modifying this 'S' to 'B' using lParam?

What hook ID are you using when you install the hook function?
Afaik, keyboard monitoring via a WH_KEYBOARD or WH_KEYBOARD_LL hook
is read only. i.e.you cannot change a B to an S.
The hook functions are just for monitoring afaik.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
T

Tamas Demjen

Bruno said:
What hook ID are you using when you install the hook function?
Afaik, keyboard monitoring via a WH_KEYBOARD or WH_KEYBOARD_LL hook
is read only. i.e.you cannot change a B to an S.
The hook functions are just for monitoring afaik.

On the other hand, you can discard a keyboard event from the keyboard
hook function but not calling CallNextHookEx, and use keybd_event to
emulate a substitute keystroke:

LRESULT CALLBACK Hook(int nCode, WPARAM wParam, LPARAM lParam)
{
if(wParam == 'S')
{
keybd_event('B', ..., 0, ...);
keybd_event('B', ..., KEYEVENTF_KEYUP, ...);
}
else
CallNextHookEx(KeyHook, nCode, wParam, lParam);
}

Be very careful, as you can cause pretty severe damage to the system if
you misuse this hook, or issue the wrong keydb_event. You could get the
control or alt key stuck forever, or get a keystroke repeating forever,
or even all keystrokes permenently disabled, after which you have to
reboot to get it fixed.

Do this at your own risk.

Tom
 
B

Bruno van Dooren [MVP VC++]

On the other hand, you can discard a keyboard event from the keyboard hook
function but not calling CallNextHookEx, and use keybd_event to emulate a
substitute keystroke:

Not calling CallNextHook only affects other hook functions, not regular apps
afaik.
LRESULT CALLBACK Hook(int nCode, WPARAM wParam, LPARAM lParam)
{
if(wParam == 'S')
{
keybd_event('B', ..., 0, ...);
keybd_event('B', ..., KEYEVENTF_KEYUP, ...);
}
else
CallNextHookEx(KeyHook, nCode, wParam, lParam);
}

Be very careful, as you can cause pretty severe damage to the system if
you misuse this hook, or issue the wrong keydb_event. You could get the
control or alt key stuck forever, or get a keystroke repeating forever, or
even all keystrokes permenently disabled, after which you have to reboot
to get it fixed.

Yeah. inserting keyboard events has the interesting side effect that your
hook function gets called.
So your keyboard hook inserts a char, for which it will get executed again,
for which it will insert...
I think that is primarily the reason why hooks are for monitoring purposes
only.
That and security of course.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
T

Tamas Demjen

Bruno said:
Not calling CallNextHook only affects other hook functions, not regular apps
afaik.

I stand corrected.

I've used a keyboard hook to capture a special key combination, and
called keybd_event to issue another keystroke. I was under the
impression that I practically redefined a key. It looks like I was
completely wrong. It worked, as the original keystroke I intended to
replace did nothing at all.

Sorry for the confusion I have caused.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top