public static bool SendMessage(IntPtr hWnd, VKey vKey, bool checkKeyboardState, int delay = 100) { if (checkKeyboardState) { CheckKeyShiftState(); } //Send KEY_DOWN if (SendMessage(hWnd, (int)Message.KEY_DOWN, (uint)vKey.Vk, GetLParam(1, vKey.Vk, 0, 0, 0, 0))) { return(false); } Thread.Sleep(delay); //Send VM_CHAR if (SendMessage(hWnd, (int)Message.VM_CHAR, (uint)vKey.Vk, GetLParam(1, vKey.Vk, 0, 0, 0, 0))) { return(false); } Thread.Sleep(delay); //Send KEY_UP if (SendMessage(hWnd, (int)Message.KEY_UP, (uint)vKey.Vk, GetLParam(1, vKey.Vk, 0, 0, 1, 1))) { return(false); } Thread.Sleep(delay); return(true); }
/// <summary>Constructor if you already have a whole VKey. Good for making a dereferenced copy.</summary> /// <param name="vKey">The already built VKey.</param> public VKey(VKey vKey) { buttonCounter = 0; Vk = vKey.Vk; ShiftKey = vKey.ShiftKey; ShiftType = vKey.ShiftType; }
public static bool GetKeyState(VKey vKey) { if ((GetKeyState((int)vKey.Vk) & 0xF0) == 1) { return(true); } return(false); }
public static bool ForegroundKeyPress(IntPtr hWnd, VKey vKey, int delay = 100) { var temp = true; temp &= ForegroundKeyDown(hWnd, vKey); Thread.Sleep(delay); temp &= ForegroundKeyUp(hWnd, vKey); Thread.Sleep(delay); return(temp); }
public static bool ForegroundKeyUp(IntPtr hWnd, VKey vKey) { if (GetForegroundWindow() != hWnd) { if (!SetForegroundWindow(hWnd)) { return(false); } } return(ForegroundKeyUp(vKey)); }
public static bool ForegroundKeyUp(VKey vKey) { uint intReturn; INPUT structInput; structInput = new INPUT(); structInput.type = INPUT_KEYBOARD; // VKey down shift, ctrl, and/or alt structInput.keyboardInput.wScan = 0; structInput.keyboardInput.time = 0; structInput.keyboardInput.dwFlags = 0; // VKey down the actual VKey-code structInput.keyboardInput.wVk = (ushort)vKey.Vk; // VKey up the actual VKey-code structInput.keyboardInput.dwFlags = KEYEVENTF_KEYUP; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(typeof(INPUT))); return(true); }
public static bool PostMessage(IntPtr hWnd, VKey vKey, int delay = 100) { //Send KEY_DOWN if (PostMessage(hWnd, (int)Message.KEY_DOWN, (uint)vKey.Vk, GetLParam(1, vKey.Vk, 0, 0, 0, 0))) { return(false); } Thread.Sleep(delay); //Send VM_CHAR if (PostMessage(hWnd, (int)Message.VM_CHAR, (uint)vKey.Vk, GetLParam(1, vKey.Vk, 0, 0, 0, 0))) { return(false); } Thread.Sleep(delay); if (PostMessage(hWnd, (int)Message.KEY_UP, (uint)vKey.Vk, GetLParam(1, vKey.Vk, 0, 0, 0, 0))) { return(false); } Thread.Sleep(delay); return(true); }
public static bool ForegroundKeyDown(VKey vKey) { var structInput = new INPUT { type = INPUT_KEYBOARD, keyboardInput = { wScan = 0, time = 0, dwFlags = 0, wVk = (ushort)vKey.Vk } }; // VKey down shift, ctrl, and/or alt // VKey down the actual VKey-code SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); // VKey up shift, ctrl, and/or alt //keybd_event((int)VKey.VK, GetScanCode(VKey.VK) + 0x80, KEYEVENTF_NONE, 0); //keybd_event((int)VKey.VK, GetScanCode(VKey.VK) + 0x80, KEYEVENTF_KEYUP, 0); return(true); }
public static void BackgroundMouseClick(IntPtr hWnd, VKey vKey, int x, int y, int delay = 100) { switch (vKey.Vk) { case VKeys.KEY_MBUTTON: PostMessage(hWnd, (int)Message.MBUTTONDOWN, (uint)vKey.Vk, GetLParam(x, y)); Thread.Sleep(delay); PostMessage(hWnd, (int)Message.MBUTTONUP, (uint)vKey.Vk, GetLParam(x, y)); break; case VKeys.KEY_LBUTTON: PostMessage(hWnd, (int)Message.LBUTTONDOWN, (uint)vKey.Vk, GetLParam(x, y)); Thread.Sleep(delay); PostMessage(hWnd, (int)Message.LBUTTONUP, (uint)vKey.Vk, GetLParam(x, y)); break; case VKeys.KEY_RBUTTON: PostMessage(hWnd, (int)Message.RBUTTONDOWN, (uint)vKey.Vk, GetLParam(x, y)); Thread.Sleep(delay); PostMessage(hWnd, (int)Message.RBUTTONUP, (uint)vKey.Vk, GetLParam(x, y)); break; } }
public static bool PostMessageAll(IntPtr hWnd, VKey vKey, bool alt, bool ctrl, bool shift, int delay = 100) { CheckKeyShiftState(); uint intReturn; INPUT structInput; structInput = new INPUT(); structInput.type = INPUT_KEYBOARD; // VKey down shift, ctrl, and/or alt structInput.keyboardInput.wScan = 0; structInput.keyboardInput.time = 0; structInput.keyboardInput.dwFlags = 0; if (alt) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_MENU; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); } if (ctrl) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_CONTROL; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); } if (shift) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_SHIFT; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); if (vKey.ShiftKey != VKeys.NULL) { //Send KEY_DOWN if (PostMessage(hWnd, (int)Message.KEY_DOWN, (uint)vKey.Vk, GetLParam(1, vKey.ShiftKey, 0, 0, 0, 0))) { return(false); } Thread.Sleep(delay); } } PostMessage(hWnd, vKey); structInput.keyboardInput.dwFlags = KEYEVENTF_KEYUP; if (shift && vKey.ShiftKey == VKeys.NULL) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_SHIFT; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); } if (ctrl) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_CONTROL; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); } if (alt) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_MENU; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); } return(true); }
public static bool ForegroundKeyPressAll(IntPtr hWnd, VKey vKey, bool alt, bool ctrl, bool shift, int delay = 100) { if (GetForegroundWindow() != hWnd) { if (!SetForegroundWindow(hWnd)) { return(false); } } uint intReturn; INPUT structInput; structInput = new INPUT(); structInput.type = INPUT_KEYBOARD; // VKey down shift, ctrl, and/or alt structInput.keyboardInput.wScan = 0; structInput.keyboardInput.time = 0; structInput.keyboardInput.dwFlags = 0; if (alt) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_MENU; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); } if (ctrl) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_CONTROL; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); } if (shift) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_SHIFT; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); if (vKey.ShiftKey != VKeys.NULL) { structInput.keyboardInput.wVk = (ushort)vKey.ShiftKey; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); } } // VKey up the actual VKey-code ForegroundKeyPress(hWnd, vKey); structInput.keyboardInput.dwFlags = KEYEVENTF_KEYUP; if (shift && vKey.ShiftKey == VKeys.NULL) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_SHIFT; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); } if (ctrl) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_CONTROL; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); } if (alt) { structInput.keyboardInput.wVk = (ushort)VKeys.KEY_MENU; intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); Thread.Sleep(delay); } return(true); }