C# 隐藏 ctrl 键在mouse_event

本文关键字:event mouse 键在 隐藏 ctrl | 更新日期: 2023-09-27 18:35:45

我遇到一种情况,当按下ctrl键时,我试图发送mouse-click

我发现接收鼠标单击事件的应用程序将 ctrl 键解释为向下。

在发送鼠标事件之前,我可以执行哪些操作才能在代码中release ctrl 键?

我正在使用mouse_event发送LeftDown消息,如果这是一个有用的线索。

谢谢!

C# 隐藏 ctrl 键在mouse_event

如果要阻止默认行为,请从控件中的此重写方法返回 true:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Ctrl)
    {
        //send mouse event
        return true;
    }
}

在使用mouse_event之前尝试使用 keybd_event():

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
    public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int extraInfo);
    [DllImport("user32.dll")]
    static extern short MapVirtualKey(int wCode, int wMapType);
    // ...
        keybd_event((int)Keys.ControlKey, (byte)MapVirtualKey((int)Keys.ControlKey, 0), 2, 0); // Control Up                
        // ... call mouse_event() ...

感谢您的回答。 我担心有人会认为我正在使用 WinForms 控件:)

我发现 Windows 输入模拟器库 (http://inputsimulator.codeplex.com/) 能够获得我需要的东西。 在我执行"鼠标按下"消息之前,我使用它来发送"向上键"消息,一切正常。

再次感谢您的回答!