Search code examples
c#winapipostmessage

winapi postmessage WM_HOTKEY


i need to make some kind of virtual keyboard, to send keyboard signals to other apps, but i can't simulate key combinations. Any WM_KEYDOWN or WM_KEYUP message work as keydown and keyup. I've found WM_HOTKEY message, but it has no any effect. What's wrong?

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 works fine, but WM_HOTKEY has no any effect

Using SendInput solwed my poblem. Here is my class to send key events

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

Solution

  • The correct way to synthesize input on Windows is to call SendInput.

    You can sometimes achieve limited success with PostMessage. As for WM_HOTKEY, that's something completely unrelated to what you are attempting to do.