Ejemplo n.º 1
0
        private static void SendMouseInput(int x, int y, int data, NativeMethods.SendMouseInputFlags flags)
        {
            var permissions = new PermissionSet(PermissionState.Unrestricted);

            permissions.Demand();

            var intflags = (int)flags;

            if ((intflags & (int)NativeMethods.SendMouseInputFlags.Absolute) != 0)
            {
                // Absolute position requires normalized coordinates.
                NormalizeCoordinates(ref x, ref y);
                intflags |= NativeMethods.MouseeventfVirtualdesk;
            }

            var mi = new NativeMethods.Input();

            mi.type = NativeMethods.InputMouse;
            mi.union.mouseInput.dx          = x;
            mi.union.mouseInput.dy          = y;
            mi.union.mouseInput.mouseData   = data;
            mi.union.mouseInput.dwFlags     = intflags;
            mi.union.mouseInput.time        = 0;
            mi.union.mouseInput.dwExtraInfo = new IntPtr(0);

            if (NativeMethods.SendInput(1, ref mi, Marshal.SizeOf(mi)) == 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Ejemplo n.º 2
0
        private static void SendKeyboardInput(Key key, bool press)
        {
            var permissions = new PermissionSet(PermissionState.Unrestricted);

            permissions.Demand();

            var ki = new NativeMethods.Input();

            ki.type = NativeMethods.InputKeyboard;
            ki.union.keyboardInput.wVk   = (short)KeyInterop.VirtualKeyFromKey(key);
            ki.union.keyboardInput.wScan = (short)NativeMethods.MapVirtualKey(ki.union.keyboardInput.wVk, 0);

            var dwFlags = 0;

            if (ki.union.keyboardInput.wScan > 0)
            {
                dwFlags |= NativeMethods.KeyeventfScancode;
            }

            if (!press)
            {
                dwFlags |= NativeMethods.KeyeventfKeyup;
            }

            ki.union.keyboardInput.dwFlags = dwFlags;

            if (ExtendedKeys.Contains(key))
            {
                ki.union.keyboardInput.dwFlags |= NativeMethods.KeyeventfExtendedkey;
            }

            ki.union.keyboardInput.time        = 0;
            ki.union.keyboardInput.dwExtraInfo = new IntPtr(0);

            if (NativeMethods.SendInput(1, ref ki, Marshal.SizeOf(ki)) == 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }