C# keyhook question
I copied the following code from http://www.c开发者_开发知识库odeproject.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 fine but when I make a little change:
lParam.vkCode ++;
or
lParam.scanCode ++;
right before the return CallNextHookEx(...), the original keys still appears in Notepad. Ex. If I press "a", I expect the letter in Notepad will be "b" but it still "a". It seems like "lParam" doesn't change. Couldn't understand why?
The value lParam.vkCode is not a simple int type so I don't think you can increment it in this fashion.
once you have your key object however ...
Keys key = (Keys)lParam.vkCode;
You should be able to do something like get the byte value and increment that.
int keyVal = Encoding.ASCII.GetByte(key.KeyCode);
keyVal++;
I think vkCode is not the actual key code but maybe a pointer or something, by assigning it to the variable Key .Net pulls that value in and with your cast translates it to the managed key type.
Of course i could be wrong ...
精彩评论