简体   繁体   中英

Using SendInput on Win7 x64, SHIFT doesn't work

I have a virtual keyboard as part of a larger application I am developing. The synthesized keystrokes are achieved using SendInput . Originally the keyboard was developed for win 7 x86 and everything worked fine and still does. However, I'm having issue with x64.

At first, the Caps Lock command would get stuck. It could be enabled, but never disabled. At this point I was defining the INPUT structure without MOUSEINPUT and HARDWAREINPUT .

    [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;
    };

In an attempt to get the Caps Lock functionality working I switched to defining INPUT as:

[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;
}

However, now the SHIFT command doesn't work. Any help would be greatly appreciated.

Figured it out. At some point, for the KeyDown call, I had changed the 'size' parameter of SendInput to:

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

when it should be:

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

This didn't effect any key except the SHIFT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM