Sign in to follow this  
Buttacup

lParam Keyboard Hook

Recommended Posts

A few months ago I set up my keyboard input and I noticed I wasn't getting an repeat count in my lParam of my keyboard hook. Looking at it again it's still an issue! Is there a known reason why or is my ugly code(I swear I did a number of different tests using a variety of code to make sure it wasn't my ugly code) just that ugly? I guess I could increment the count myself in the injection call....
    byte* ptr = (byte*)&vHookedKeyboard.at(i).lParam;
    memcpy(&BufferedKeyStroke.repCount, &ptr, 2);ptr++;ptr++;
    BufferedKeyStroke.scanCode = (char)*ptr++;
    BufferedKeyStroke.keyState = (char)*ptr;


yup :P

Share this post


Link to post
Share on other sites
Is all that pointer arithmetic necessary?

With this

byte* ptr = (byte*)&vHookedKeyboard.at(i).lParam;

you can use array syntax [] to access the bytes of the lParam

ptr[0]
ptr[1]
ptr[2]
ptr[3]

Do you need to take the address of ptr here?

memcpy(&BufferedKeyStroke.repCount, &ptr, 2)

Try this

byte* ptr = (byte*)&vHookedKeyboard.at(i).lParam;
memcpy(&BufferedKeyStroke.repCount, ptr, 2);
BufferedKeyStroke.scanCode = (char)ptr[2];
BufferedKeyStroke.keyState = (char)ptr[3];

Share this post


Link to post
Share on other sites
Yes, no it wasn't necessary pointer arithmetic and referencing by [n] does look a lot cleaner. It doesn't however make a change to the counting of repetitions for me, it still reads 0? Do you get a count? I guess knowing that someone else does have one would tell me to keep looking.

Weird looking at code I did four months ago, I barely even knew what that did then!

Thx

Share this post


Link to post
Share on other sites
I don't have your keyboard hook so I couldn't get a count if I wanted to. I just figured that an lParam was a long integer made of four bytes and worked from there.

Try dumping vHookedKeyboard.at(i).lParam to output to see what you get.

Also note that if repCount is a short integer and lParam is a long integer where the first 16 bits hold the value for repCount, then you can shift bits in lParam to gain access.

BufferedKeyStroke.repCount = (short)(vHookedKeyboard.at(i).lParam >> 16);

Moreover, you can use a bitwise mask with a shift to isolate the other bytes of lParam.

BufferedKeyStroke.scanCode = (char)((vHookedKeyboard.at(i).lParam & 0x0000FF00) >> 8);

BufferedKeyStroke.keyState = (char)(vHookedKeyboard.at(i).lParam & 0x000000FF);

And to simplify and possibly reduce some access overhead, you can use a temp variable to hold lParam.

unsigned long temp = vHookedKeyboard.at(i).lParam;
BufferedKeyStroke.repCount = (short)((temp & 0xFFFF0000) >> 16);
BufferedKeyStroke.scanCode = (char)((temp & 0x0000FF00) >> 8);
BufferedKeyStroke.keyState = (char)(temp & 0x000000FF);

Of course, none of that will matter if lParam is garbage to start with.

Share this post


Link to post
Share on other sites
I am most definitely taking notes.... but as you say none of that will matter if lParam is garbage to start with! So the question still remains does windows actually send the rep count because all other information is being received intact.

Thx for the heads up LessBread!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this