以下程序实现用键盘上的方向键代替鼠标的移动,可以基本成功,但是我
想按住某个方向键不放让鼠标快速移动却不行,大家看看我的做法吧!在键盘钩子的DLL部份是如下简单的代码:
DllExport void WINAPI InstallLaunchEv() 

  Hook=(HHOOK)SetWindowsHookEx(WH_KEYBOARD, //键盘钩子
  (HOOKPROC)LauncherHook, 
  theApp.m_hInstance, 
  0); 
}LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam) 

        int x,y;
POINT point;
GetCursorPos (&point) ;//获得当前鼠标位置
          
        x = point.x;
y = point.y;

  LRESULT Result=CallNextHookEx(Hook,nCode,wParam,lParam); 
  if(nCode==HC_ACTION) 
  { 
   if(lParam & 0x80000000) 
   { 
     char c[1]; 
      c[0]=wParam; 
  switch (wParam)//分析按键
  {
          case VK_UP ://向上的键
               y=y-5 ;
               break ;
               
          case VK_DOWN ://向下的键
               y =y+5 ;
               break ;
               
          case VK_LEFT ://向左的键
               x = x-7;
               break ;
               
          case VK_RIGHT ://向右的键
               x = x+7 ;
               break ;   
   //SaveLog(c); 
  } 
          point.x = x;
  point.y = y;
  SetCursorPos (point.x, point.y) ;//鼠标移到新的坐标点
   } 
  return Result; 
  }
}在MFC的exe程序中调用该DLL,在VIEW中的OnKeyDown中:
 void CKeyHookView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
 {
    InstallLaunchEv();
    CView::OnKeyDown(nChar, nRepCnt, nFlags);
 }
实现键盘代替鼠标的移动,可以成功,但是只能每按一次移动一步,不能按住某个方向键
不放鼠标就一直移动,如果我要按住一个方向键快速移动光标,那该怎么办?

解决方案 »

  1.   

    在LauncherHook中判断lParam的0~15位(repeat count),如果该重复计数位不为0,表示该键一直被按下,那么就将偏移量加大,如x = x+15;等等。
      

  2.   

    void CKeyHookView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) nRepCnt:
    Repeat count (the number of times the keystroke is repeated as a result of the user holding down the key).或许你可以处理一下nRepCnt.
      

  3.   


    if(lParam & 0x80000000)
    改为
    if(!(lParam & 0x80000000))
      

  4.   

    TO:sans(sans):
    怎么判断?能否贴出代码?
      

  5.   

    WM_KEYDOWN Notification  ------------------------------------------------------The WM_KEYDOWN message is posted to the window with the keyboard focus when a nonsystem key is pressed. A nonsystem key is a key that is pressed when the ALT key is not pressed. SyntaxWM_KEYDOWN    WPARAM wParam
        LPARAM lParam;
        
    ParameterswParam
    Specifies the virtual-key code of the nonsystem key. 
    lParam
    Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table. 
    0-15
    Specifies the !!!repeat count!!!  for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
    16-23
    Specifies the scan code. The value depends on the OEM.
    24
    Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
    25-28
    Reserved; do not use.
    29
    Specifies the context code. The value is always 0 for a WM_KEYDOWN message.
    30
    Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is zero if the key is up.
    31
    Specifies the transition state. The value is always zero for a WM_KEYDOWN message.
      

  6.   

    我想可以這么做,當有按鍵按下的時候,置一個狀態值,例如: keyisdown.然后做一個定時器. ontimer里執行: 
    if(keyisdown) 
    {
       移動鼠標
    }
    在得到 keyup的時候,重置那個狀態值,就可以了。以為如何?