将键击发送到WPF应用程序本身

时间:2017-09-13 09:54:50

标签: c# wpf

在我的应用程序中,我正在捕捉键击,当键击不是条形码的一部分时,我想将它们发送到应用程序。键击需要发送到应用程序本身。

我尝试了不同的方法但没有成功。

internal class InputSimulator
{
    #pragma warning disable SA1305 // Field names should not use Hungarian notation
    #pragma warning disable SA1307 // Accessible fields should begin with upper-case letter
    #pragma warning disable SA1400 // Access modifier should be declared

    [DllImport("user32.dll", SetLastError = true)]
    private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

    [DllImport("user32.dll")]
    internal static extern uint MapVirtualKey(uint uCode, uint uMapType);

    struct INPUT
    {
        public INPUTType type;
        public INPUTUnion Event;
    }

    [StructLayout(LayoutKind.Explicit)]
    struct INPUTUnion
    {
        [FieldOffset(0)]
        internal MOUSEINPUT mi;
        [FieldOffset(0)]
        internal KEYBDINPUT ki;
        [FieldOffset(0)]
        internal HARDWAREINPUT hi;
    }

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

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

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

    enum INPUTType : uint
    {
        INPUT_KEYBOARD = 1
    }

    [Flags]
    enum KEYEVENTF : uint
    {
        EXTENDEDKEY = 0x0001,
        KEYUP = 0x0002,
        SCANCODE = 0x0008,
        UNICODE = 0x0004
    }

    public static void ProcessKeys(Key[] keys)
    {
        INPUT[] inputs = new INPUT[keys.Length + 1];
        for (int i = 0; i < keys.Length; i++)
        {
            var virtualKey = KeyInterop.VirtualKeyFromKey(keys[i]);
            uint skey = MapVirtualKey((uint)virtualKey, 0x0);
            inputs[i].type = INPUTType.INPUT_KEYBOARD;
            inputs[i].Event.ki.dwFlags = KEYEVENTF.SCANCODE;
            inputs[i].Event.ki.wScan = (ushort)skey;
        }
        inputs[keys.Length].type = INPUTType.INPUT_KEYBOARD;
        inputs[keys.Length].Event.ki.dwFlags = KEYEVENTF.UNICODE;
        inputs[keys.Length].Event.ki.dwFlags |= KEYEVENTF.KEYUP;
        uint cSuccess = SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
    }

    public static void Send(Key key)
    {
        if (Keyboard.PrimaryDevice != null)
        {
            if (Keyboard.PrimaryDevice.ActiveSource != null)
            {
                var e = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, key)
                {
                    RoutedEvent = Keyboard.PreviewKeyDownEvent
                };
                InputManager.Current.ProcessInput(e);
            }
        }
    }

    #pragma warning restore SA1305 // Field names should not use Hungarian 
    notation
    #pragma warning restore SA1307 // Accessible fields should begin with 
    upper-case letter
    #pragma warning restore SA1400 // Access modifier should be declared
}

在下面的代码中有两种方法ProcessKeysSend,但它们不起作用。我究竟做错了什么?还有另一种方法来实现这一目标吗?目前我使用System.Windows.Forms.SendKeys.SendWait,但我更喜欢不引用System.Windows.Forms

的方式 编辑:我的问题被标记为可能重复:SendKeys.Send Method in WPF application我尝试了两种解决方案,但没有一种方法可行,也许我的SendInput实现有问题,但我找不到有什么问题

0 个答案:

没有答案