private static void SendMouseInput(int x, int y, int data, MySendKeys.SendMouseInputFlags flags) { PermissionSet permissions = new PermissionSet(PermissionState.Unrestricted); permissions.Demand(); int intflags = (int)flags; if ((intflags & (int)MySendKeys.SendMouseInputFlags.Absolute) != 0) { // Absolute position requires normalized coordinates. NormalizeCoordinates(ref x, ref y); intflags |= MySendKeys.MouseeventfVirtualdesk; } MySendKeys.INPUT mi = new MySendKeys.INPUT(); mi.type = MySendKeys.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 (MySendKeys.SendInput(1, ref mi, Marshal.SizeOf(mi)) == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
private static void SendKeyboardInput(Key key, bool press) { PermissionSet permissions = new PermissionSet(PermissionState.Unrestricted); permissions.Demand(); MySendKeys.INPUT ki = new MySendKeys.INPUT(); ki.type = MySendKeys.InputKeyboard; ki.union.keyboardInput.wVk = (short)KeyInterop.VirtualKeyFromKey(key); ki.union.keyboardInput.wScan = (short)MySendKeys.MapVirtualKey(ki.union.keyboardInput.wVk, 0); int dwFlags = 0; if (ki.union.keyboardInput.wScan > 0) { dwFlags |= MySendKeys.KeyeventfScancode; } if (!press) { dwFlags |= MySendKeys.KeyeventfKeyup; } ki.union.keyboardInput.dwFlags = dwFlags; if (ExtendedKeys.Contains(key)) { ki.union.keyboardInput.dwFlags |= MySendKeys.KeyeventfExtendedkey; } ki.union.keyboardInput.time = 0; ki.union.keyboardInput.dwExtraInfo = new IntPtr(0); if (MySendKeys.SendInput(1, ref ki, Marshal.SizeOf(ki)) == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } }