2013-10-25 78 views
1

我打得周圍與Windows的問題掛鉤的時刻。我建立了一個全球低級別的鼠標鉤 ,我想反轉mousemovement。對於這個我有幾個問題(我希望這是正確的,我的英語不是很好,對不起)。第一個是修改lParam參數,並與CallNextHookEx方法函數轉發:與修改鼠標移動

LRESULT CALLBACK MouseInvertProc(int nCode, WPARAM wParam, LPARAM lParam) 
{ 
    MSLLHOOKSTRUCT* ms = NULL; 
    int dx = 0; //delta x 
    int dy = 0; //delta y 
    if(nCode == HC_ACTION) 
    { 
     if(wParam == WM_MOUSEMOVE) 
     { 
      ms = (MSLLHOOKSTRUCT*)lParam; 
      dx = ms->pt.x - g_oldMousePos.x; 
      dy = ms->pt.y - g_oldMousePos.y; 
      ms->pt.x = ms->pt.x - 2 * dx; 
      ms->pt.y = ms->pt.y - 2 * dy; 
      GetCursorPos(&g_oldMousePos); 

      return CallNextHookEx(g_MouseInvertHook,nCode,wParam, (LPARAM)ms);   
     } 
     else 
      return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam); 
    } 
    return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);    
} 

但這沒有任何效果可言。有人知道爲什麼嗎?
第二次嘗試是提供一個輸入與修改後的mousecoordinates:

LRESULT CALLBACK MouseInvertProc(int nCode, WPARAM wParam, LPARAM lParam) 
{ 
    MSLLHOOKSTRUCT* ms = NULL; 
    int dx = 0; //delta x 
    int dy = 0; //delta y 
    if(nCode == HC_ACTION) 
    { 
     if(wParam == WM_MOUSEMOVE) 
     { 
      ms = (MSLLHOOKSTRUCT*)lParam; 
      dx = ms->pt.x - g_oldMousePos.x; 
      dy = ms->pt.y - g_oldMousePos.y; 
      ms->pt.x = ms->pt.x - 2 * dx; 
      ms->pt.y = ms->pt.y - 2 * dy; 
      GetCursorPos(&g_oldMousePos); 

      if(!g_artificialma) //g_artificialma is true when i created a artificial mouse input. 
      { 
       g_artificialma = true; 
       mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, ms->pt.x, ms->pt.y, NULL, NULL); 
       return 1;       
      } 
      else 
      {    
       g_artificialma = false; 
       return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam); 
      }   
     } 
     else 
      return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam); 
    } 
    return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam); 
}   

隨着g_artificialma可變我想阻止一個無限循環,因爲當我打電話mouse_event的HOOKPROC被調用。
但這樣我讓我的鼠標卡在我的屏幕左上角。

的最後一次嘗試是簡單地調用SetCursorPos函數:

LRESULT CALLBACK MouseInvertProc(int nCode, WPARAM wParam, LPARAM lParam) 
{ 
    MSLLHOOKSTRUCT* ms = NULL; 
    int dx = 0; //delta x 
    int dy = 0; //delta y 
    if(nCode == HC_ACTION) 
    { 
     if(wParam == WM_MOUSEMOVE) 
     { 
      ms = (MSLLHOOKSTRUCT*)lParam; 
      dx = ms->pt.x - g_oldMousePos.x; 
      dy = ms->pt.y - g_oldMousePos.y; 
      ms->pt.x = ms->pt.x - 2 * dx; 
      ms->pt.y = ms->pt.y - 2 * dy; 
      GetCursorPos(&g_oldMousePos); 

      SetCursorPos(ms->pt.x, ms->pt.y);      
     } 
     else 
      return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam); 
    } 
    return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);    
} 

這工作得很好,但在例如3D應用,這引起了怪異的鼠標移動
,我認爲這種方式實在是太醜了,因爲我阻止其他應用程序安裝的鉤子。

那麼你有什麼建議來解決,在一個良好的工作和乾淨的方式(也許沒有窗戶鉤)?

回答

0

你的「最後一次嘗試」代碼有沒有效果都沒有在我的測試:

#define _WIN32_WINNT 0x0501 

#include <iostream> 
#include <windows.h> 
using namespace std; 

HHOOK g_MouseInvertHook; 
POINT g_oldMousePos; 
LRESULT CALLBACK MouseInvertProc(int nCode, WPARAM wParam, LPARAM lParam) 
{ 
    MSLLHOOKSTRUCT* ms = NULL; 
    int dx = 0; //delta x 
    int dy = 0; //delta y 
    if(nCode == HC_ACTION) 
    { 
     if(wParam == WM_MOUSEMOVE) 
     { 
      ms = (MSLLHOOKSTRUCT*)lParam; 
      dx = ms->pt.x - g_oldMousePos.x; 
      dy = ms->pt.y - g_oldMousePos.y; 
      ms->pt.x = ms->pt.x - 2 * dx; 
      ms->pt.y = ms->pt.y - 2 * dy; 
      GetCursorPos(&g_oldMousePos); 

      SetCursorPos(ms->pt.x, ms->pt.y);      
     } 
     else 
      return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam); 
    } 
    return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);    
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{ 
    GetCursorPos(&g_oldMousePos); 
    g_MouseInvertHook = SetWindowsHookEx(WH_MOUSE_LL, MouseInvertProc, hInstance, 0); 
    MSG msg; 
    while(GetMessage(&msg, NULL, 0, 0)) 
    { 
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
    } 
    return msg.wParam; 
} 

你能提出申請(像我)的一個小例子,它使用只是你的功能,並給出結果你在描述?

+0

EEEH,我剛看到這個問題是10個月大。我的答案可能沒有太多意義。 – Hallion