如何通过c#代码启用“鼠标键”辅助功能?

问题描述 投票:0回答:1

我正在尝试使用 user32.dll 方法启用鼠标辅助功能“鼠标键”,但我无法弄清楚我做错了什么。

我使用下面的代码来启用它,但它不起作用。

PS:我尝试使用 SPI_GETMOUSEKEYS (0x0036),并且能够将鼠标辅助功能设置检索到 MOUSEKEYS 结构中,但我无法使用 SPI_SETMOUSEKEYS 更改当前设置

class Program
{
    const int SPI_SETMOUSEKEYS = 0x0037;
    const int SPIF_SENDCHANGE = 0x002;

    [DllImport("user32.dll", SetLastError = true)]
   // [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SystemParametersInfo(int uiAction, int uiParam, IntPtr pvParam, int fWinIni);

    [StructLayout(LayoutKind.Sequential)]
    public struct MOUSEKEYS
    {
        public uint cbSize;
        public uint dwFlags;
        public uint iMaxSpeed;
        public uint iTimeToMaxSpeed;
        public uint iCtrlSpeed;
        public uint dwReserved1;
        public uint dwReserved2;
    }

    static void Main()
    {

        try
        {
            // Enable Mouse Keys
            EnableMouseKeys();

            Console.WriteLine("Mouse Keys enabled.");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    public static void EnableMouseKeys()
    {
        MOUSEKEYS mk = new MOUSEKEYS();
        mk.cbSize = (uint)Marshal.SizeOf(typeof(MOUSEKEYS));
        mk.dwFlags = 0x00000001;
        bool retVal;
        IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(mk));

        Marshal.StructureToPtr(mk, ptr, true);

        retVal = SystemParametersInfo(SPI_SETMOUSEKEYS, Marshal.SizeOf(typeof(MOUSEKEYS)), ptr, SPIF_SENDCHANGE);
    }
c# windows accessibility mouse
1个回答
0
投票

当您从头开始创建 MOUSEKEYS 结构时,您必须指定所有参数。
更好地阅读鼠标键功能的当前状态,然后仅更改必要的标志:

MKF_MOUSEKEYSON
,最终在本例中为
MKF_REPLACENUMBERS

您可以在此处通过 ref 传递 MOUSEKEYS 结构,无需打扰

Marshal.AllocHGlobal()

您还需要验证是否设置了
MKF_AVAILABLE
标志,否则该功能不可用。

当您更改当前参数时,指示保存更改并将通知广播到顶级窗口。即,同时设置

SPIF_UPDATEINIFILE
SPIF_SENDCHANGE

public static bool EnableMouseKeys(bool enabled) {
    if (GetMouseKeys(out MOUSEKEYS mouseKeys)) {
        if (!mouseKeys.dwFlags.HasFlag(MouseKeysFlags.AVAILABLE)) {
            throw new InvalidOperationException("Mouse Keys feature not available");
        }
        mouseKeys.dwFlags = enabled ? 
            mouseKeys.dwFlags | MouseKeysFlags.MOUSEKEYSON : 
            mouseKeys.dwFlags &~MouseKeysFlags.MOUSEKEYSON;
        var result = SetMouseKeys(mouseKeys);
        return result;
    }
    return false;
}

public static bool GetMouseKeys(out MOUSEKEYS mouseKeys) {
    mouseKeys = new MOUSEKEYS();
    return SystemParametersInfo(SPI.GETMOUSEKEYS, 
        (uint)Marshal.SizeOf<MOUSEKEYS>(), ref mouseKeys, SPIF.NONE);
}

public static bool SetMouseKeys(MOUSEKEYS mouseKeys) {
    return SystemParametersInfo(SPI.SETMOUSEKEYS, 
        (uint)Marshal.SizeOf<MOUSEKEYS>(), ref mouseKeys, SPIF.UPDATEANDSENDCHANGE);
}

声明:

public enum MouseKeysFlags : uint {
    MOUSEKEYSON = 0x00000001,
    AVAILABLE = 0x00000002
}

public enum SPI : uint {
    GETMOUSEKEYS = 0x0036,
    SETMOUSEKEYS = 0x0037
}

public enum SPIF : uint
{
    NONE = 0x00,
    UPDATEINIFILE = 0x01,
    SENDCHANGE = 0x02,
    SENDWININICHANGE = 0x02,
    UPDATEANDSENDCHANGE = UPDATEINIFILE | SENDCHANGE
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MOUSEKEYS {
    public MOUSEKEYS() { }
    public uint cbSize = (uint)Marshal.SizeOf<MOUSEKEYS>();
    public int dwFlags;
    public int iMaxSpeed;
    public int iTimeToMaxSpeed;
    public int iCtrlSpeed;
    public int dwReserved1;
    public int dwReserved2;
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, ref MOUSEKEYS pvParam, SPIF fWinIni);
© www.soinside.com 2019 - 2024. All rights reserved.