挂钩鼠标输入和移动光标

时间:2016-02-02 23:35:35

标签: windows winapi

我正在设置全局鼠标挂钩:

    int taps = 1;

- (IBAction)btnActivate {

            taps = taps + 1;

            if (taps == 2){
                [activateBtn setBackgroundColor:[UIColor grayColor]];
                [self performSelector:@selector(tapOne:) withObject:self afterDelay:0.5];

            }
            else if (taps == 3){
                [self performSelector:@selector(tapTwo:) withObject:self afterDelay:0];
                [activateBtn setEnabled:NO];
                [activateBtn setBackgroundColor:[UIColor redColor]];

            } else {
               //Do other things here if you want 
            }
        }
- (void) tapOne: (id) sender{

            if (taps == 2){

                 [self performSelector:@selector(afterTapDone:) withObject:self afterDelay:1];
            } else {

            }

        }


- (void) tapTwo: (id) sender {

        //Perform button actions here when user double taps 

            [self performSelector:@selector(afterTapDone:) withObject:self afterDelay:10];


        }

- (void) afterTapDone: (id) sender {
            taps = 1;
            [activateBtn setEnabled:YES];
            [activateBtn setBackgroundColor:[UIColor colorWithRed:0/255.0f green:174/255.0f blue:239/255.0f alpha:1.0f]];
            [activateBtn setTitle:@"Activate"];

        }

并挂钩所有鼠标移动消息并移动鼠标光标。

hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, LLMouseProc, GetModuleHandle(0), 0);

如果比例因子为1.0,那么一切都很完美......好吧它是如何工作的。我们将鼠标移动到物理坐标[29,94],比方说,我们的比例因子是1.5,因此逻辑坐标是[19,62]。我们打电话给

LRESULT CALLBACK LLMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    LPMSLLHOOKSTRUCT lpmh = reinterpret_cast<LPMSLLHOOKSTRUCT>(lParam);
    UINT             nMsg = static_cast<UINT>(wParam);
    POINT            ptScreen = { 0 };

    if (nCode >= 0)
    {
        ptScreen = lpmh->pt;
        if (WM_MOUSEMOVE == nMsg)
        {
            // ptScreen coordinates we receiving are in physical units (pixels)
            // convert them to logical
            POINT logPos = { ptScreen.x, ptScreen.y };
            ConvertPhysicalToLogical(&logPos);

            // Move mouse cursor to the new position
            // SetCursotPos() requires coordinates in logical units
            SetCursorPos(logPos.x, logPos.y);

            // We don't want to propagate this message
            return 1L;
        }
    }
    return 0L;
}

它触发发送WM_MOUSEMOVE并在某处内部将我们的逻辑坐标转换为物理19 * 1.5 = 28,62 * 1.5 = 93,所以我们的新输入坐标现在是[28,93]而不是[29,94]。现在我们重复所有内容,将[28,93]转换为逻辑[18,62]并调用

SetCursorPos(19, 62);

依旧......

所以,我知道从LLMouseProc调用SetCursorPos()并不正确,我试图找到解决方案 - 有没有正确的方法呢?

P.S.&GT; ClipCursor()没有帮助,因为它也调用SetCursorPos()

谢谢!

0 个答案:

没有答案