我试了修改wParam,象下面那样,不过不知为什么没用
LRESULT CALLBACK KeyboardProc(
  int code,       // hook code
  WPARAM wParam,  // virtual-key code
  LPARAM lParam   // keystroke-message information
)
{
if('1'==wParam&&(lParam>>30&1))
{
 wParam='2';
 return CallNextHookEx(g_keyHook,code,wParam,lParam);
        }
        return CallNextHookEx(g_keyHook,code,wParam,lParam); }

解决方案 »

  1.   

    这样怎么可以呢,你应该在在回辟1时向窗口发送keyborad消息,而不是简单的把参数改为2.
      

  2.   

    发keybd_event
    LRESULT WINAPI KeyProc(int nCode, WPARAM wParam, LPARAM lParam) 

    if (nCode == HC_ACTION)
    {
    if('1' == wParam)
    {
    if ((lParam & 0x80000000) == 0)
    {
    keybd_event('2', 0, KEYEVENTF_EXTENDEDKEY, 0);
    return TRUE;
    }
    else 
    {
    keybd_event('2', 0 , KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);  
    return TRUE;
    }
    }
    }
    return CallNextHookEx(glhHook, nCode, wParam, lParam); 

      

  3.   

    你的代码是正确的,谢谢。
    但我还是有一些问题,想请教一下,如果这些问题很菜的话,请不要笑话小弟(呵)
    代码一:LRESULT CALLBACK KeyboardProc( int code, // hook code 
                                  WPARAM wParam, // virtual-key code 
                                  LPARAM lParam // keystroke-message information ) 

       if('1'==wParam)
    {
    keybd_event('2',0,KEYEVENTF_EXTENDEDKEY,0);
    return 1;
     
    }
        return CallNextHookEx(g_keyHook,code,wParam,lParam); 
     }当我按下“1”键的时候,它会产生两个“2”,为什么是两个呢?难道是按下键产生一个消息,松开键产生一个消息?如果是这样,那请看代码二。代码二:LRESULT CALLBACK KeyboardProc( int code, // hook code 
                                  WPARAM wParam, // virtual-key code 
                                  LPARAM lParam // keystroke-message information ) 

       if('1'==wParam&&!(lParam>>30&1))
    {
    keybd_event('2',0,KEYEVENTF_EXTENDEDKEY,0);
    return 1;
     
    }
        return CallNextHookEx(g_keyHook,code,wParam,lParam); 
     }当我按下“1”键时,会变成字符“2”,按着不放的时候,则产生“1”。在这里我加了个条件“
    !(lParam>>30&1)”表明键的先前状态是松开的,所以当执行if('1'==wParam&&!(lParam>>30&1))
    {
    keybd_event('2',0,KEYEVENTF_EXTENDEDKEY,0);
    return 1;
     
    }时,按下“1”键,显示“2”,但当我松开键时,这个条件是不成立的,如果按代码一的解释的话,我松开键会
    再显示个“1”!实际上,在按下“1”键,马上松开的整个过程中,只显示“2”,而不是“21”.只有当我按住“1”键不放时,才不断地显示“1”。对于这个现象,用代码是可以解释的,因为
    [code=C/C++]
    if('1'==wParam&&!(lParam>>30&1))
    [/code]
    这个条件不成立,程序执行了“return CallNextHookEx(g_keyHook,code,wParam,lParam);”这条语句。
    但我们再来看代码三:
    代码三:LRESULT CALLBACK KeyboardProc( int code, // hook code 
                                  WPARAM wParam, // virtual-key code 
                                  LPARAM lParam // keystroke-message information ) 

       if('1'==wParam&&(lParam>>30&1))
    {
    keybd_event('2',0,KEYEVENTF_EXTENDEDKEY,0);
    return 1;
     
    }
        return CallNextHookEx(g_keyHook,code,wParam,lParam); 
     }代码三与代码二的区别仅仅是“!(lParam>>30&1)”变成了“(lParam>>30&1)”,别的一样。
    当我按下“1”键的时候,显示“12”,这貌似又印证了代码一的结论,即按下键产生一个消息,松开键产生一个消息。
    在假设代码一的结论是正确的情况下,我们可以这样来解释:在没按“1”键时,“1”键处于松开状态,
    而“if('1'==wParam&&(lParam>>30&1))”这个判断是在我按下“1”键,且“1”键的先前状态处于按下时,才成立的,所以当我按下,又松开时,显示了“12”,可为什么代码二里,按下“1”键,立即松开时,不显示的是“21”,而是“2”呢?请高手指点迷津!!
      

  4.   

    lParam
    [in] Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag. For more information about the lParam parameter, see Keystroke Message Flags. This parameter can be one or more of the following values. 
    0-15
    Specifies the repeat count. The value is the number of times the keystroke is repeated as a result of the user's holding down the key.
    16-23
    Specifies the scan code. The value depends on the OEM.
    24
    Specifies whether the key is an extended key, such as a function key or a key on the numeric keypad. The value is 1 if the key is an extended key; otherwise, it is 0.
    25-28
    Reserved.
    29
    Specifies the context code. The value is 1 if the ALT key is down; otherwise, it is 0.
    30
    Specifies the previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up.
    31
    Specifies the transition state. The value is 0 if the key is being pressed and 1 if it is being released.
    你位判断的有问题,lParam>>30&1 是第30位,前一个key的状态。而我判断的是31位,过渡状态