/// <summary> /// Send array of System.Window.Forms.Keys to client as keypresses. Restores and activates window. /// </summary> /// <param name="client">Target client.</param> /// <param name="keys">System.Window.Forms.Keys to send.</param> /// <returns>True if all keys were successfully sent.</returns> public static bool SendKeys(int client, Keys[] keys) { if (keys.Length == 0) { return(false); } ClientInfo ci; if (ClientInfoCollection.GetClient(client, out ci)) { Win32.INPUT[] inputs = new Win32.INPUT[keys.Length * 2]; Win32.KEYBDINPUT kbi = new Win32.KEYBDINPUT(); if (!ci.PrepareWindowForInput()) { ci.DetachFromWindow(); return(false); } for (int x = 0; x < keys.Length; x++) { kbi.dwFlags = 0; kbi.wVk = (ushort)keys[x]; inputs[x * 2].mkhi.ki = kbi; inputs[x * 2].type = Win32.INPUT_KEYBOARD; kbi.dwFlags = Win32.KEYEVENTF_KEYUP; inputs[x * 2 + 1].mkhi.ki = kbi; inputs[x * 2 + 1].type = Win32.INPUT_KEYBOARD; } uint success = Win32.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0])); ci.DetachFromWindow(); return(success == inputs.Length); } return(false); }
/// <summary> /// Send text to client as keypresses. Restores and activates window. /// </summary> /// <param name="client">Target client.</param> /// <param name="text">String to send.</param> /// <returns>True if all text was successfully sent.</returns> public static bool SendText(int client, string text) { if (string.IsNullOrEmpty(text)) { return(false); } ClientInfo ci; if (ClientInfoCollection.GetClient(client, out ci)) { if (!ci.PrepareWindowForInput()) { ci.DetachFromWindow(); return(false); } Win32.INPUT[] inputs = new Win32.INPUT[text.Length * 2]; Win32.KEYBDINPUT kbi = new Win32.KEYBDINPUT(); for (int x = 0; x < text.Length; x++) { kbi.wScan = text[x]; kbi.dwFlags = Win32.KEYEVENTF_UNICODE; inputs[x * 2].mkhi.ki = kbi; inputs[x * 2].type = Win32.INPUT_KEYBOARD; kbi.dwFlags = Win32.KEYEVENTF_KEYUP | Win32.KEYEVENTF_UNICODE; inputs[x * 2 + 1].mkhi.ki = kbi; inputs[x * 2 + 1].type = Win32.INPUT_KEYBOARD; } uint success = Win32.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0])); ci.DetachFromWindow(); return(success == inputs.Length); } return(false); }
public void ExecuteKeyList(MouseAction mouseAction, Point location) { Win32.INPUT[][] inputRange = Features.KeyInput.CreateInput(ExtractKeyList(mouseAction)); Point currentLocation = new Point(); foreach (Win32.INPUT[] input in inputRange) { foreach (Win32.INPUT one_input in input) { Win32.INPUT[] one = new Win32.INPUT[1]; one[0] = one_input; if (one_input.type == Win32.INPUT_MOUSE) { currentLocation = Cursor.Position; Win32.SetCursorPos(location.X, location.Y); } Win32.SendInput((uint)one.Length, one, (int)System.Runtime.InteropServices.Marshal.SizeOf(one[0].GetType())); System.Threading.Thread.Sleep(1); if (one_input.type == Win32.INPUT_MOUSE) { Win32.SetCursorPos(currentLocation.X, currentLocation.Y); } } } //Win32.INPUT[][] inputRange = KeyInput.CreateInput(ExtractKeyList(action, MouseAction.ModifierClick)); //foreach (Win32.INPUT[] input in inputRange) //{ // Win32.SendInput((uint)input.Length, input, (int)System.Runtime.InteropServices.Marshal.SizeOf(input[0].GetType())); //} }
private Win32.INPUT CreateInputStruct(ushort virtualKeyCode, bool keyDown) { Win32.INPUT input = new Win32.INPUT(); //input.header.dwType = Win32.INPUTTYPE.KEYBOARD; input.header.dwTypePtr = new IntPtr((int)Win32.INPUTTYPE.KEYBOARD); input.data.keyboard.VKey = virtualKeyCode; if ((virtualKeyCode >= 0xE0) && (virtualKeyCode < 0xE2)) { input.data.keyboard.dwFlags = Win32.KEYEVENTF.EXTENDEDKEY; } if (keyDown == false) { input.data.keyboard.dwFlags |= Win32.KEYEVENTF.KEYUP; } return(input); }
// Mephobia HF reported that this function fails to send mouse clicks to hidden windows public static void ClickOnPoint(IntPtr wndHandle, Win32.POINT clientPoint) { //var oldPos = Cursor.Position; // ref to System.Windows.Forms /// get screen coordinates Win32.Win32.ClientToScreen(wndHandle, ref clientPoint); /// set cursor on coords, and press mouse //Cursor.Position = new Point(clientPoint.x, clientPoint.y); // ref to System.Windows.Forms var inputMouseUp = new Win32.INPUT(); inputMouseUp.Type = 0; /// input type mouse inputMouseUp.Data.Mouse.Flags = 0x0004; /// left button up var inputs = new Win32.INPUT[] { inputMouseUp }; Win32.Win32.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(Win32.INPUT))); /// return mouse //Cursor.Position = oldPos; // ref to System.Windows.Forms }
internal static void SendCharacter(Window window, View.Character c) { string value = c.ModelObject.Value; // Build key data for SendInput // FIXME: astral plane characters break in Notepad++ Win32.INPUT[] inputs = new Win32.INPUT[value.Length]; int iChr = 0; foreach (char chr in value) { inputs[iChr] = new Win32.INPUT(); inputs[iChr].type = Win32.InputType.KEYBOARD; inputs[iChr].U.ki = new Win32.KEYBDINPUT(); inputs[iChr].U.ki.wVk = 0; inputs[iChr].U.ki.wScan = (short)chr; inputs[iChr].U.ki.dwFlags = Win32.KEYEVENTF.UNICODE; inputs[iChr].U.ki.time = 0; iChr++; } // Send keys as soon as Unicodex is done hiding itself EventHandler handler = null; handler = delegate(object sender, EventArgs e) { uint result = Win32.SendInput((uint)value.Length, inputs, Marshal.SizeOf(inputs[0])); if (result == 0) { throw new Win32Exception(); } window.Deactivated -= handler; }; window.Deactivated += handler; window.Hide(); }
/// <summary> /// Send mouse click to relative position in the client window. /// </summary> /// <param name="client">Target client.</param> /// <param name="button">System.Windows.Forms.MouseButtons to click.</param> /// <param name="x">X position of point to click relative to client window's X position.</param> /// <param name="y">Y position of point to click relative to client window's Y position.</param> /// <param name="doubleClick">True for double click, false for single click.</param> /// <returns>True on success.</returns> public static bool SendMouseClick(int client, MouseButtons button, int x, int y, bool doubleClick) { ClientInfo ci; if (ClientInfoCollection.GetClient(client, out ci)) { Win32.INPUT[] inputs = new Win32.INPUT[2]; Win32.MOUSEINPUT mi = new Win32.MOUSEINPUT(); inputs[0].type = Win32.INPUT_MOUSE; inputs[1].type = Win32.INPUT_MOUSE; mi.dx = 0; mi.dy = 0; switch (button) { case MouseButtons.None: return(false); case MouseButtons.Left: mi.dwFlags = Win32.MOUSEEVENTF_LEFTDOWN; inputs[0].mkhi.mi = mi; mi.dwFlags = Win32.MOUSEEVENTF_LEFTUP; inputs[1].mkhi.mi = mi; break; case MouseButtons.Right: mi.dwFlags = Win32.MOUSEEVENTF_RIGHTDOWN; inputs[0].mkhi.mi = mi; mi.dwFlags = Win32.MOUSEEVENTF_RIGHTUP; inputs[1].mkhi.mi = mi; break; case MouseButtons.Middle: mi.dwFlags = Win32.MOUSEEVENTF_MIDDLEDOWN; inputs[0].mkhi.mi = mi; mi.dwFlags = Win32.MOUSEEVENTF_MIDDLEUP; inputs[1].mkhi.mi = mi; break; case MouseButtons.XButton1: mi.mouseData = Win32.XBUTTON1; mi.dwFlags = Win32.MOUSEEVENTF_XDOWN; inputs[0].mkhi.mi = mi; mi.dwFlags = Win32.MOUSEEVENTF_XUP; inputs[1].mkhi.mi = mi; break; case MouseButtons.XButton2: mi.mouseData = Win32.XBUTTON2; mi.dwFlags = Win32.MOUSEEVENTF_XDOWN; inputs[0].mkhi.mi = mi; mi.dwFlags = Win32.MOUSEEVENTF_XUP; inputs[1].mkhi.mi = mi; break; } if (!ci.PrepareWindowForInput()) { ci.DetachFromWindow(); return(false); } Win32.WINDOWPLACEMENT wp = new Win32.WINDOWPLACEMENT(); if (Win32.GetWindowPlacement(ci.WindowHandle, ref wp)) { /*int screenX = wp.rcNormalPosition.X + x; * int screenY = wp.rcNormalPosition.Y + y; * int absX = (screenX * 65536 / Screen.PrimaryScreen.Bounds.Width); * int absY = (screenY * 65536 / Screen.PrimaryScreen.Bounds.Height); * inputs[0].mkhi.mi.dx = absX; * inputs[1].mkhi.mi.dx = absX; * inputs[0].mkhi.mi.dy = absY; * inputs[1].mkhi.mi.dy = absY;*/ // using this method because absolute position is always off by a pixel or 2 if (!Win32.SetCursorPos(x + wp.rcNormalPosition.X, y + wp.rcNormalPosition.Y)) { return(false); } } else { return(false); } uint success = Win32.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0])); if (doubleClick && success == inputs.Length) { success = Win32.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0])); } ci.DetachFromWindow(); return(success == inputs.Length); } return(false); }
public static Win32.INPUT[] CreateInput(MyKey key, MyKey.Action action) { List<Win32.INPUT> inputRange; Win32.INPUT[] input = null; ushort mouseValue; bool swapedButtons = Win32.GetSystemMetrics(Win32.SM_SWAPBUTTON) == 0 ? false : true; switch (action) { case MyKey.Action.KeyDown: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_KEYBOARD; input[0].ki = Win32.CreateKeybdInput(key.Value, 0); break; case MyKey.Action.KeyUp: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_KEYBOARD; input[0].ki = Win32.CreateKeybdInput(key.Value, Win32.KEYEVENTF_KEYUP); break; case MyKey.Action.KeyClick: input = new Win32.INPUT[2]; input[0].type = Win32.INPUT_KEYBOARD; input[0].ki = Win32.CreateKeybdInput(key.Value, 0); input[1].type = Win32.INPUT_KEYBOARD; input[1].ki = Win32.CreateKeybdInput(key.Value, Win32.KEYEVENTF_KEYUP); break; case MyKey.Action.MouseClick: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_MOUSE; mouseValue = key.Value; if (swapedButtons) { if (key.Value == AllKeys.MOUSE_LBUTTON_CLICK.Value) mouseValue = AllKeys.MOUSE_RBUTTON_CLICK.Value; else if (key.Value == AllKeys.MOUSE_RBUTTON_CLICK.Value) mouseValue = AllKeys.MOUSE_LBUTTON_CLICK.Value; } input[0].mi = Win32.CreateMouseInput(0, 0, 0, 0, mouseValue); break; case MyKey.Action.MouseDblClick: inputRange = new List<Win32.INPUT>(); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseClick)); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseClick)); input = inputRange.ToArray(); break; case MyKey.Action.MouseX1Click: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_MOUSE; input[0].mi = Win32.CreateMouseInput(0, 0, Win32.XBUTTON1, 0, key.Value); break; case MyKey.Action.MouseX1DblClick: inputRange = new List<Win32.INPUT>(); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseX1Click)); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseX1Click)); input = inputRange.ToArray(); break; case MyKey.Action.MouseX2Click: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_MOUSE; input[0].mi = Win32.CreateMouseInput(0, 0, Win32.XBUTTON2, 0, key.Value); break; case MyKey.Action.MouseX2DblClick: inputRange = new List<Win32.INPUT>(); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseX2Click)); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseX2Click)); input = inputRange.ToArray(); break; case MyKey.Action.MouseWheelDown: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_MOUSE; input[0].mi = Win32.CreateMouseInput(0, 0, 120, 0, key.Value); break; case MyKey.Action.MouseWheelUp: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_MOUSE; input[0].mi = Win32.CreateMouseInput(0, 0, -120, 0, key.Value); break; } return input; }
static void SwitchKeyboardsToHidNative(IntPtr hDevice) { var numLock = Win32.GetKeyState(Win32.VK_NUMLOCK) != 0; var capsLock = Win32.GetKeyState(Win32.VK_CAPITAL) != 0; Action<short, int> DoSendInput = (wVk, dwFlags) => { var input = new Win32.INPUT { type = Win32.INPUTTYPE_KEYBOARD, U = new Win32.InputUnion { ki = new Win32.KEYBDINPUT { wVk = wVk, dwFlags = dwFlags } } }; Win32.SendInput(1, ref input, Marshal.SizeOf(typeof(Win32.INPUT))); }; if ((numLock) || (capsLock)) { if (numLock) DoSendInput(Win32.VK_NUMLOCK, 0); if (capsLock) DoSendInput(Win32.VK_CAPITAL, 0); Thread.Sleep(50); if (numLock) DoSendInput(Win32.VK_NUMLOCK, 2); if (capsLock) DoSendInput(Win32.VK_CAPITAL, 2); Thread.Sleep(750); } DoSendInput(Win32.VK_NUMLOCK, 0); Thread.Sleep(100); DoSendInput(Win32.VK_NUMLOCK, 0); DoSendInput(Win32.VK_NUMLOCK, 2); DoSendInput(Win32.VK_NUMLOCK, 2); Thread.Sleep(100); DoSendInput(Win32.VK_NUMLOCK, 0); DoSendInput(Win32.VK_NUMLOCK, 2); DoSendInput(Win32.VK_CAPITAL, 0); Thread.Sleep(100); DoSendInput(Win32.VK_CAPITAL, 0); DoSendInput(Win32.VK_CAPITAL, 2); DoSendInput(Win32.VK_CAPITAL, 2); Thread.Sleep(100); DoSendInput(Win32.VK_CAPITAL, 0); DoSendInput(Win32.VK_CAPITAL, 2); DoSendInput(Win32.VK_NUMLOCK, 0); Thread.Sleep(100); DoSendInput(Win32.VK_NUMLOCK, 0); DoSendInput(Win32.VK_NUMLOCK, 2); DoSendInput(Win32.VK_NUMLOCK, 2); if ((!numLock) || (capsLock)) { Thread.Sleep(750); if (!numLock) DoSendInput(Win32.VK_NUMLOCK, 0); if (capsLock) DoSendInput(Win32.VK_CAPITAL, 0); Thread.Sleep(50); if (!numLock) DoSendInput(Win32.VK_NUMLOCK, 2); if (capsLock) DoSendInput(Win32.VK_CAPITAL, 2); } }
/// <summary> /// Handles mouse down input that happens on the magnifier. Only handles events that are either in the magnified state, or was triggered /// by and XButton. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PointingMagnifier_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.XButton1 && e.Button != MouseButtons.XButton2) { if (Magnified && _mouseDown < 1) //only handle while magnified and no buttons are pressed { // before moving the mouse, whose physical button is down, we want to release the // button that was down, but we don't want our mouse up handler to receive this, // so we first block the input from coming through. _synthMouseUp = true; Win32.INPUT i = new Win32.INPUT(); i.type = Win32.INPUTF.MOUSE; i.mi.dx = 0; i.mi.dy = 0; i.mi.mouseData = 0; i.mi.time = 0; i.mi.dwExtraInfo = UIntPtr.Zero; i.mi.dwFlags = MouseButton(e.Button, true); Win32.SendInput(1, ref i, System.Runtime.InteropServices.Marshal.SizeOf(new Win32.INPUT())); _synthMouseUp = false; Cursor.Position = TransposeMouse(); // now move the mouse to the mapped click location. // synthesize a mouse down in the mapped location, and since we want it to go through, unblock input Clickthrough = true; DeMagnify(); _synthMouseDown = true; i.mi.dwFlags = MouseButton(e.Button, false); Win32.SendInput(1, ref i, System.Runtime.InteropServices.Marshal.SizeOf(new Win32.INPUT())); _synthMouseDown = false; } _pressedButton = e.Button; _mouseDown++; _mouseDownPt = Cursor.Position; } else //Mouse down was triggered by an x button and should be passed through to underlying application { _synthMouseUp = true; Win32.INPUT i = new Win32.INPUT(); i.type = Win32.INPUTF.MOUSE; i.mi.dx = 0; i.mi.dy = 0; switch (e.Button) //Button 1 or Button 2 { case System.Windows.Forms.MouseButtons.XButton1: i.mi.mouseData = (uint)Win32.MOUSEEVENTF.XBUTTON1; break; case System.Windows.Forms.MouseButtons.XButton2: i.mi.mouseData = (uint)Win32.MOUSEEVENTF.XBUTTON2; break; default: i.mi.mouseData = 0; break; } i.mi.time = 0; i.mi.dwExtraInfo = UIntPtr.Zero; i.mi.dwFlags = Win32.MOUSEEVENTF.XUP; Win32.SendInput(1, ref i, System.Runtime.InteropServices.Marshal.SizeOf(new Win32.INPUT())); _synthMouseUp = false; // synthesize a mouse down in the mapped location, and since we want it to go through, unblock input Clickthrough = true; //this.Invalidate(); _synthMouseDown = true; i.mi.dwFlags = Win32.MOUSEEVENTF.XDOWN; Win32.SendInput(1, ref i, System.Runtime.InteropServices.Marshal.SizeOf(new Win32.INPUT())); _synthMouseDown = false; } }
/// <summary> /// Captures all mouse movements, even if the mouse is not currently captured by the PM. Used to update the position of /// the PM so that it is centered about the cursor. /// /// Movements are also used to trigger the drag state, when the mouse has moved and a mouse button is pressed. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnRawInputMouseMove(object sender, MouseEventArgs e) { _idleTimeout.Interval = IDLELENGTH; if (isIdle) { _idleTimeout.Enabled = true; if (Idle != null) Idle(this, null); } if (Active) { if (!Magnified) Center = e.Location; if (_dragging && _mouseDown > 0) { _logger.MouseMove(e); } else if (WobbrockLib.Extensions.GeotrigEx.Distance((PointF)_mouseDownPt, (PointF)e.Location) > 10 && _mouseDown > 0 && !_dragging) { _dragging = true; _logger.MouseMove(e); if (!_clickthrough) { //Release capture of mouse _synthMouseUp = true; Win32.INPUT i = new Win32.INPUT(); i.type = Win32.INPUTF.MOUSE; i.mi.dx = 0; i.mi.dy = 0; i.mi.mouseData = 0; i.mi.time = 0; i.mi.dwExtraInfo = UIntPtr.Zero; i.mi.dwFlags = MouseButton(_pressedButton, true); Win32.SendInput(1, ref i, System.Runtime.InteropServices.Marshal.SizeOf(new Win32.INPUT())); _synthMouseUp = false; Cursor.Position = _mouseDownPt; //Move mouse back to original mouse down location Clickthrough = true; // set the form transparent so that the click we synthesize will go through _synthMouseDown = true; i.mi.dwFlags = MouseButton(_pressedButton, false); // synthesize a mouse down in the mapped location, and since we want it to go through Win32.SendInput(1, ref i, System.Runtime.InteropServices.Marshal.SizeOf(new Win32.INPUT())); _synthMouseDown = false; Cursor.Position = e.Location;//Reset cursor position to the current location } } } }
public static void ThreadLoop() { Win32.INPUT inputMouseUpL = new Win32.INPUT { Type = 1 }; inputMouseUpL.U.KeyboardInput = new Win32.KEYBDINPUT(); inputMouseUpL.U.KeyboardInput.wVk = Win32.VirtualKeys.LeftButton; inputMouseUpL.U.KeyboardInput.wScan = 0; inputMouseUpL.U.KeyboardInput.dwFlags = Win32.KEYEVENTF.UNICODE; inputMouseUpL.U.KeyboardInput.dwExtraInfo = UIntPtr.Zero; Win32.INPUT inputMouseDownL = new Win32.INPUT { Type = 1 }; inputMouseDownL.U.KeyboardInput = new Win32.KEYBDINPUT(); inputMouseDownL.U.KeyboardInput.wVk = Win32.VirtualKeys.LeftButton; inputMouseDownL.U.KeyboardInput.wScan = 0; inputMouseDownL.U.KeyboardInput.dwFlags = Win32.KEYEVENTF.KEYUP; inputMouseDownL.U.KeyboardInput.dwExtraInfo = UIntPtr.Zero; Win32.INPUT inputMouseUpR = new Win32.INPUT { Type = 1 }; inputMouseUpL.U.KeyboardInput = new Win32.KEYBDINPUT(); inputMouseUpL.U.KeyboardInput.wVk = Win32.VirtualKeys.RightButton; inputMouseUpL.U.KeyboardInput.wScan = 0; inputMouseUpL.U.KeyboardInput.dwFlags = Win32.KEYEVENTF.UNICODE; inputMouseUpL.U.KeyboardInput.dwExtraInfo = UIntPtr.Zero; Win32.INPUT inputMouseDownR = new Win32.INPUT { Type = 1 }; inputMouseDownL.U.KeyboardInput = new Win32.KEYBDINPUT(); inputMouseDownL.U.KeyboardInput.wVk = Win32.VirtualKeys.RightButton; inputMouseDownL.U.KeyboardInput.wScan = 0; inputMouseDownL.U.KeyboardInput.dwFlags = Win32.KEYEVENTF.KEYUP; inputMouseDownL.U.KeyboardInput.dwExtraInfo = UIntPtr.Zero; Win32.INPUT[] inputsUPL = new Win32.INPUT[] { inputMouseUpL }; Win32.INPUT[] inputsDOWNL = new Win32.INPUT[] { inputMouseDownL }; Win32.INPUT[] inputsUPR = new Win32.INPUT[] { inputMouseUpR }; Win32.INPUT[] inputsDOWNR = new Win32.INPUT[] { inputMouseDownR }; while (true) { /*try * {*/ IntPtr ForegroundWindowHandle = Win32.GetForegroundWindow(); int x = Cursor.Position.X; int y = Cursor.Position.Y; if (ToggleState) { if (cmr_input.GetKeyStateDown(Win32.VirtualKeys.P) && ForegroundWindowHandle == CommunicationData.Overlay.TargetWindowHandle) { while (true) { Win32.mouse_event((uint)Win32.MouseEventFlags.LEFTDOWN, 0, 0, 0, 0); Thread.Sleep(5); Win32.mouse_event((uint)Win32.MouseEventFlags.LEFTUP, 0, 0, 0, 0); Thread.Sleep(5); if (cmr_input.GetKeyStateUp(Win32.VirtualKeys.P)) { break; } ForegroundWindowHandle = Win32.GetForegroundWindow(); if (ForegroundWindowHandle == CommunicationData.Overlay.TargetWindowHandle) { break; } } } else { Thread.Sleep(10); } } //} catch { } } }
public static Win32.INPUT[] CreateInput(MyKey key, MyKey.Action action) { List <Win32.INPUT> inputRange; Win32.INPUT[] input = null; ushort mouseValue; bool swapedButtons = Win32.GetSystemMetrics(Win32.SM_SWAPBUTTON) == 0 ? false : true; switch (action) { case MyKey.Action.KeyDown: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_KEYBOARD; input[0].ki = Win32.CreateKeybdInput(key.Value, 0); break; case MyKey.Action.KeyUp: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_KEYBOARD; input[0].ki = Win32.CreateKeybdInput(key.Value, Win32.KEYEVENTF_KEYUP); break; case MyKey.Action.KeyClick: input = new Win32.INPUT[2]; input[0].type = Win32.INPUT_KEYBOARD; input[0].ki = Win32.CreateKeybdInput(key.Value, 0); input[1].type = Win32.INPUT_KEYBOARD; input[1].ki = Win32.CreateKeybdInput(key.Value, Win32.KEYEVENTF_KEYUP); break; case MyKey.Action.MouseClick: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_MOUSE; mouseValue = key.Value; if (swapedButtons) { if (key.Value == AllKeys.MOUSE_LBUTTON_CLICK.Value) { mouseValue = AllKeys.MOUSE_RBUTTON_CLICK.Value; } else if (key.Value == AllKeys.MOUSE_RBUTTON_CLICK.Value) { mouseValue = AllKeys.MOUSE_LBUTTON_CLICK.Value; } } input[0].mi = Win32.CreateMouseInput(0, 0, 0, 0, mouseValue); break; case MyKey.Action.MouseDblClick: inputRange = new List <Win32.INPUT>(); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseClick)); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseClick)); input = inputRange.ToArray(); break; case MyKey.Action.MouseX1Click: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_MOUSE; input[0].mi = Win32.CreateMouseInput(0, 0, Win32.XBUTTON1, 0, key.Value); break; case MyKey.Action.MouseX1DblClick: inputRange = new List <Win32.INPUT>(); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseX1Click)); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseX1Click)); input = inputRange.ToArray(); break; case MyKey.Action.MouseX2Click: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_MOUSE; input[0].mi = Win32.CreateMouseInput(0, 0, Win32.XBUTTON2, 0, key.Value); break; case MyKey.Action.MouseX2DblClick: inputRange = new List <Win32.INPUT>(); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseX2Click)); inputRange.AddRange(CreateInput(key, MyKey.Action.MouseX2Click)); input = inputRange.ToArray(); break; case MyKey.Action.MouseWheelDown: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_MOUSE; input[0].mi = Win32.CreateMouseInput(0, 0, 120, 0, key.Value); break; case MyKey.Action.MouseWheelUp: input = new Win32.INPUT[1]; input[0].type = Win32.INPUT_MOUSE; input[0].mi = Win32.CreateMouseInput(0, 0, -120, 0, key.Value); break; } return(input); }
/// <summary> /// /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> private IntPtr HookCallback(int nCode, int wParam, IntPtr lParam) { long now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; if (nCode >= 0) { Win32.MSLLHOOKSTRUCT info = (Win32.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(Win32.MSLLHOOKSTRUCT)); bool isDown = false; int nClicks = 1; MouseButtons button = MouseButtons.None; #region setting mouse data Win32.INPUT input = new Win32.INPUT(); input.type = (IntPtr)Win32.INPUTF.MOUSE; input.mi.dwExtraInfo = (IntPtr)0; input.mi.mouseData = 0; input.mi.time = (IntPtr)0; input.mi.dx = (IntPtr)0; input.mi.dy = (IntPtr)0; switch (wParam) // figure out which button was pressed, if any { ////////// down events case (int)Win32.WM.LBUTTONDOWN: input.mi.dwFlags = (IntPtr)Win32.MOUSEEVENTF.LEFTDOWN; button = MouseButtons.Left; isDown = true; nClicks = HandleDoubleClickLogic(button, info.pt.x, info.pt.y, now); break; case (int)Win32.WM.RBUTTONDOWN: input.mi.dwFlags = (IntPtr)Win32.MOUSEEVENTF.RIGHTDOWN; button = MouseButtons.Right; isDown = true; nClicks = HandleDoubleClickLogic(button, info.pt.x, info.pt.y, now); break; case (int)Win32.WM.MBUTTONDOWN: input.mi.dwFlags = (IntPtr)Win32.MOUSEEVENTF.MIDDLEDOWN; button = MouseButtons.Middle; isDown = true; nClicks = HandleDoubleClickLogic(button, info.pt.x, info.pt.y, now); break; case (int)Win32.WM.XBUTTONDOWN: if ((wParam >> 16) == 1) { // high-order word specifies which button was pressed button = MouseButtons.XButton1; input.mi.dwFlags = (IntPtr)Win32.MOUSEEVENTF.XBUTTON1; } else if ((wParam >> 16) == 2) { button = MouseButtons.XButton2; input.mi.dwFlags = (IntPtr)Win32.MOUSEEVENTF.XBUTTON2; } else { Debug.Fail("XButton message received without a button identifier."); } isDown = true; nClicks = HandleDoubleClickLogic(button, info.pt.x, info.pt.y, now); break; ////////// up events case (int)Win32.WM.LBUTTONUP: input.mi.dwFlags = (IntPtr)Win32.MOUSEEVENTF.LEFTUP; button = MouseButtons.Left; nClicks = _state.IssueDoubleClickOnMouseUp ? 2 : 1; break; case (int)Win32.WM.RBUTTONUP: input.mi.dwFlags = (IntPtr)Win32.MOUSEEVENTF.RIGHTUP; button = MouseButtons.Right; nClicks = _state.IssueDoubleClickOnMouseUp ? 2 : 1; break; case (int)Win32.WM.MBUTTONUP: input.mi.dwFlags = (IntPtr)Win32.MOUSEEVENTF.MIDDLEUP; button = MouseButtons.Middle; nClicks = _state.IssueDoubleClickOnMouseUp ? 2 : 1; break; case (int)Win32.WM.XBUTTONUP: if ((wParam >> 16) == 1) { // high-order word specifies which button was pressed button = MouseButtons.XButton1; input.mi.dwFlags = (IntPtr)Win32.MOUSEEVENTF.XUP; } else if ((wParam >> 16) == 2) { button = MouseButtons.XButton2; input.mi.dwFlags = (IntPtr)Win32.MOUSEEVENTF.XUP; } else { Debug.Fail("XButton message received without a button identifier."); } nClicks = _state.IssueDoubleClickOnMouseUp ? 2 : 1; break; } short delta = -1; // wheel if (wParam == (int)Win32.WM.MOUSEWHEEL) { input.mi.dwFlags = (IntPtr)Win32.MOUSEEVENTF.WHEEL; delta = (short)((info.mouseData >> 16) & 0xffff); nClicks = 0; } #endregion MouseEventArgs e = null; switch (_strategy) { case MoveStrategy.Standard: _curr = new Point(info.pt.x, info.pt.y); _prev = _curr; Win32.SetCursorPos(_curr.X, _curr.Y); break; case MoveStrategy.Clipped: // set up the event argument int deltx = info.pt.x - _prev.X; int delty = info.pt.y - _prev.Y; if (deltx < 100 && delty < 100) { int newx = _curr.X + deltx; int newy = _curr.Y + delty; _curr.X += deltx; _curr.Y += delty; #region clip to the screen bounds if (_curr.X < 0) { _curr.X = 0; } else if (_curr.X >= System.Windows.SystemParameters.VirtualScreenWidth) { _curr.X = (int)System.Windows.SystemParameters.VirtualScreenWidth - 1; } if (_curr.Y < 0) { _curr.Y = 0; } else if (_curr.Y >= System.Windows.SystemParameters.VirtualScreenHeight) { _curr.Y = (int)System.Windows.SystemParameters.VirtualScreenHeight - 1; } #endregion } break; case MoveStrategy.Relative: deltx = info.pt.x - _prev.X; delty = info.pt.y - _prev.Y; if (deltx < 100 && delty < 100) { int newx = _curr.X + deltx; int newy = _curr.Y + delty; _curr.X += deltx; _curr.Y += delty; _physicalCursorLocation.X += deltx; _physicalCursorLocation.Y += delty; _prev = _physicalCursorLocation; #region clip to the screen bounds if (_curr.X < 0) { _curr.X = 0; } else if (_curr.X >= System.Windows.SystemParameters.VirtualScreenWidth) { _curr.X = (int)System.Windows.SystemParameters.VirtualScreenWidth - 1; } if (_curr.Y < 0) { _curr.Y = 0; } else if (_curr.Y >= System.Windows.SystemParameters.VirtualScreenHeight) { _curr.Y = (int)System.Windows.SystemParameters.VirtualScreenHeight - 1; } #endregion Win32.SetCursorPos(_physicalCursorLocation.X, _physicalCursorLocation.Y); } break; } e = new MouseEventArgs(button, nClicks, _curr.X, _curr.Y, delta); // fire the appropriate event if (button != MouseButtons.None) { if (isDown) { if (OnPreviewMouseDown != null) { SuppressArgs hp = new SuppressArgs(); OnPreviewMouseDown(e, hp); if (!hp.SuppressMouseEvent) { Win32.SendMouseDown(e); if (OnMouseDown != null) { OnMouseDown(this, e); } } } else { Win32.SendMouseDown(e); if (OnMouseDown != null) { OnMouseDown(this, e); } } } else // button came up { if (Control.MouseButtons != MouseButtons.None) { Win32.SendMouseUp(e); } if (_state.IssueDoubleClickOnMouseUp) { if (OnMouseDoubleClick != null) { OnMouseDoubleClick(this, e); } _state = DoubleClickState.Empty; // unset } else if (_state.IssueClickOnMouseUp) { if (OnMouseClick != null) { OnMouseClick(this, e); } _state.IssueClickOnMouseUp = false; // unset } if (OnMouseUp != null) { OnMouseUp(this, e); } } } else // no buttons -- wheel or mouse movement { if (delta != -1) { Win32.SendMouseWheel(e); //Win32.SendInput(1, ref input, Marshal.SizeOf(new Win32.INPUT())); if (OnMouseWheel != null) { OnMouseWheel(this, e); } } else // mouse movement { if (OnMouseMove != null) { OnMouseMove(this, e); } } } } Win32.CallNextHookEx(_hookID, nCode, wParam, lParam); return((IntPtr)0); }