【问题标题】:winapi postmessage WM_HOTKEYwinapi postmessage WM_HOTKEY
【发布时间】:2012-04-19 17:46:16
【问题描述】:

我需要制作某种虚拟键盘,将键盘信号发送到其他应用程序,但我无法模拟组合键。任何 WM_KEYDOWN 或 WM_KEYUP 消息都用作 keydown 和 keyup。我找到了 WM_HOTKEY 消息,但它没有任何作用。怎么了?

if (shift)
   mod |= MOD_SHIFT;
if (control)
   mod |= MOD_CONTROL;
if (alt)
   mod |= MOD_ALT;
if (win)
   mod |= MOD_WIN;
if(!shift && !win && !alt && !control)
   PostMessage(hFocus, WM_KEYDOWN, (UIntPtr)key, 1);
else
   PostMessage(hWindow, WM_HOTKEY, (UIntPtr)key, mod);

WM_KEYDOWN 可以正常工作,但是 WM_HOTKEY 没有任何作用

使用 SendInput 解决了我的问题。这是我发送关键事件的类

   class input
    {
        public IntPtr focus;
        public Form1 _this;

        [DllImport("User32.dll")]
        public static extern uint SendInput(uint numberOfInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] input, int structSize); 

        [DllImport("user32.dll")]
        public static extern IntPtr GetMessageExtraInfo();


        public struct KEYBDINPUT
        {
            public ushort wVk;
            public ushort wScan;
            public uint dwFlags;
            public long time;
            public uint dwExtraInfo;
        };
        [StructLayout(LayoutKind.Explicit, Size = 28)]
        public struct INPUT
        {
            [FieldOffset(0)]
            public uint type;
            [FieldOffset(4)]
            public KEYBDINPUT ki;
        };



        public const int INPUT_MOUSE = 0;
        public const int INPUT_KEYBOARD = 1;
        public const int INPUT_HARDWARE = 2;
        public const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
        public const uint KEYEVENTF_KEYUP = 0x0002;
        public const uint KEYEVENTF_UNICODE = 0x0004;
        public const uint KEYEVENTF_SCANCODE = 0x0008;
        public const uint XBUTTON1 = 0x0001;
        public const uint XBUTTON2 = 0x0002;
        public const uint MOUSEEVENTF_MOVE = 0x0001;
        public const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
        public const uint MOUSEEVENTF_LEFTUP = 0x0004;
        public const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
        public const uint MOUSEEVENTF_RIGHTUP = 0x0010;
        public const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        public const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
        public const uint MOUSEEVENTF_XDOWN = 0x0080;
        public const uint MOUSEEVENTF_XUP = 0x0100;
        public const uint MOUSEEVENTF_WHEEL = 0x0800;
        public const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
        public const uint MOUSEEVENTF_ABSOLUTE = 0x8000;

        public void SetFocus(IntPtr windowFocus)
        {
            uint id = 0;
            focus = windowFocus;

        }

        public input(IntPtr windowFocus)
        {
            SetFocus(windowFocus);
        }


        public bool KeyDown(ushort key)
        {
            INPUT[] inputs = new INPUT[1];
            inputs[0].type = INPUT_KEYBOARD;
            inputs[0].ki.dwFlags = 0;
            inputs[0].ki.wVk = key;
            return (SendInput(1, inputs, System.Runtime.InteropServices.Marshal.SizeOf(inputs[0])) == 1);
        }


        public bool KeyUp(ushort key)
        {
            INPUT[] inputs = new INPUT[1];
            inputs[0].type = INPUT_KEYBOARD;
            inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
            inputs[0].ki.wVk = key;
            return (SendInput(1, inputs, System.Runtime.InteropServices.Marshal.SizeOf(inputs[0])) == 1);
        }

        public bool KeyPress(ushort key)
        {
            return KeyDown(key) && KeyUp(key);

        }

        public bool HotKey(ushort key, bool ctrl, bool shift, bool alt, bool win, bool caps)
        {
            bool r = false;
            //KeyUp(0);
            if (ctrl) KeyDown((ushort)((uint)Keys.LControlKey  & 0xff ));
            if (shift) KeyDown((ushort)((uint)Keys.LShiftKey & 0xff));
            if (alt) KeyDown((ushort)((uint)Keys.Menu & 0xff));
            if (win) KeyDown((ushort)((uint)Keys.LWin & 0xff));
            if (caps) KeyPress((ushort)((uint)Keys.CapsLock & 0xff));
            r = KeyPress(key);
            if (caps) KeyPress((ushort)((uint)Keys.CapsLock & 0xff));
            if (win) KeyUp((ushort)((uint)Keys.LWin & 0xff));
            if (alt) KeyUp((ushort)((uint)Keys.Menu & 0xff));
            if (shift) KeyUp((ushort)((uint)Keys.LShiftKey & 0xff));
            if (ctrl) KeyUp((ushort)((uint)Keys.LControlKey & 0xff));
            return r;
        }
    }
}

【问题讨论】:

    标签: c# winapi postmessage


    【解决方案1】:

    在 Windows 上合成输入的正确方法是调用SendInput

    您有时可以通过PostMessage 获得有限的成功。至于WM_HOTKEY,这与您尝试做的事情完全无关。

    【讨论】:

      【解决方案2】:

      为什么要发送 WM_HOTKEY 消息?根据MSDN 的说法,情况有所不同...

      【讨论】:

      • 是的,这是错误的方式。谢谢,我会更深入地找出为什么任何 keydown 也会发送 keyup 消息
      • 为什么不呢?你看过 WinSpy++ 会发生什么吗?
      猜你喜欢
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 2016-01-23
      • 2014-02-03
      • 1970-01-01
      • 2011-09-06
      相关资源
      最近更新 更多