Question with C #

I copied the following code from http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx ,

public int hookProc(int code, int wParam, ref keyboardHookStruct lParam) 
{
    if (code >= 0) 
    {
     Keys key = (Keys)lParam.vkCode;
     if (HookedKeys.Contains(key)) 
        {
         KeyEventArgs kea = new KeyEventArgs(key);
         if((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null)) 
            {KeyDown(this, kea) ;} 
            else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null)) 
            {KeyUp(this, kea); }
            if (kea.Handled)
            {return 1;}
      }
     }
     lParam.vkCode ++;
     lParam.scanCode ++;
     return CallNextHookEx(hhook, code, wParam, ref lParam);
}

      

It works great, but when I make a small change:

lParam.vkCode ++;
or
lParam.scanCode ++;

      

right before CallNextHookEx (...) returns, the original keys are still displayed in Notepad. Ex. If I press "a", I expect the letter in Notepad to be "b", but still "a". It looks like "lParam" is not changing. Couldn't understand why?

+2


source to share


1 answer


The lParam.vkCode value is not a simple int type, so I don't think you can increase it that way.

once you have your key object ...

Keys key = (Keys)lParam.vkCode;

      

You should be able to do something like getting a byte value and incrementing it.



int keyVal = Encoding.ASCII.GetByte(key.KeyCode);
keyVal++;

      

I think vkCode is not an actual key code, but maybe a pointer or something else, assigning it to a Key.Net variable that pulls this value in and with your lit, converts it to the type of the managed key.

Of course I could be wrong ...

+1


source







All Articles