在Win7 x64上使用SendInput,SHIFT不起作用

时间:2011-11-22 02:59:02

标签: c# windows-7-x64

我有一个虚拟键盘作为我正在开发的更大应用程序的一部分。使用SendInput实现合成的击键。最初键盘是为赢得7 x86而开发的,一切都很好,但仍然如此。但是,我遇到了x64的问题。

首先,Caps Lock命令会卡住。它可以启用,但从不禁用。此时我正在定义INPUT结构,而不是MOUSEINPUTHARDWAREINPUT

    [StructLayoutAttribute(LayoutKind.Explicit)]
    public struct KEYBDINPUT
    {
        [FieldOffset(0)]
        public ushort wVk;
        [FieldOffset(4)]
        public ushort wScan;
        [FieldOffset(8)]
        public uint dwFlags;
        [FieldOffset(16)]
        public long time;
        [FieldOffset(20)]
        public IntPtr dwExtraInfo;
    };

    [StructLayout(LayoutKind.Explicit, Size = 28)]
    public struct INPUT
    {
        [FieldOffset(0)]
        public uint type;
#if WIN64
        [FieldOffset(8)]
#else 
        [FieldOffset(4)]
#endif
        public KEYBDINPUT ki;
    };

为了使Caps Lock功能正常工作,我转而将INPUT定义为:

[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT {
    public int dx;
    public int dy;
    public uint mouseData;
    public uint dwFlags;
    public uint time;
    public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT {
    public ushort wVk;
    public ushort wScan;
    public uint dwFlags;
    public uint time;
    public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
public struct HARDWAREINPUT {
    public int uMsg;
    public short wParamL;
    public short wParamH;
}

[StructLayout(LayoutKind.Explicit)]
public struct MouseKeybdHardwareInputUnion {
    [FieldOffset(0)]
    public MOUSEINPUT mi;

    [FieldOffset(0)]
    public KEYBDINPUT ki;

    [FieldOffset(0)]
    public HARDWAREINPUT hi;
}

[StructLayout(LayoutKind.Sequential)]
public struct INPUT {
    public uint type;
    public MouseKeybdHardwareInputUnion mkhi;
}

但是,现在SHIFT命令不起作用。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

想出来。在某些时候,对于KeyDown调用,我已将SendInput的'size'参数更改为:

intReturn = SendInput(1, ref structInput, 28); 

应该是:

intReturn = SendInput(1, ref structInput, Marshal.SizeOf(structInput));

除了SHIFT之外,这对任何关键都没有影响。