private void SendMouse(mpKey mpKey) { if (mpKey.IsMouseMove) { WindowsAPI.SetCursorPos(mpKey.MouseMove_X, mpKey.MouseMove_Y); } else { if (mpKey.MouseClick_Right) { WindowsAPI.MouseAction(WindowsAPI.ME.RIGHTDOWN | WindowsAPI.ME.RIGHTUP, mpKey.MouseMove_X * 65536 / PrimaryScreenWidth, mpKey.MouseMove_Y * 65536 / PrimaryScreenHeight, 0, 0); } else { WindowsAPI.MouseAction(WindowsAPI.ME.LEFTDOWN | WindowsAPI.ME.LEFTUP, mpKey.MouseMove_X * 65536 / PrimaryScreenWidth, mpKey.MouseMove_Y * 65536 / PrimaryScreenHeight, 0, 0); } } }
// Format: // {K,VK_1} -> VK // {K,VK_1,DU} -> VK, DownUp // {K,VK_1,DU,1s} -> VK, DownUp, KeyPressInterval // {K,VK_1,DU,1s,100ms} -> VK, DownUp, KeyPressInterval, KeyDownUpInterval // {MM,X,Y} -> Move mouse to X,Y // {MC,R} -> Mouse right click // {MC,L} -> Mouse left click // {D,1000ms} -> Delay 1000ms private mpKey[] TranslateKeyArray(string KeyArray) { string[] Keys = GetKeys(KeyArray); mpKey[] TranslateKeyArray = new mpKey[Keys.Length]; for (int i = 0; i < Keys.Length; i++) { TranslateKeyArray[i] = new mpKey(); string[] KeyArg = Keys[i].Split(new char[] { ',' }); if (KeyArg[0] == "K") { TranslateKeyArray[i].Type = mpKeyType.K; TranslateKeyArray[i].Key = GetVKInt(KeyArg[1]); if (KeyArg.Length == 2) { TranslateKeyArray[i].KeyPressOption = KeyPressOption.KeyDownAndUp; } if (KeyArg.Length >= 3 && KeyArg.Length <= 4) { switch (KeyArg[2]) { case "D": TranslateKeyArray[i].KeyPressOption = KeyPressOption.KeyDown; break; case "DU": TranslateKeyArray[i].KeyPressOption = KeyPressOption.KeyDownAndUp; break; default: throw new Exception(string.Format("Invalid Key Array: {{{0}}}", Keys[i])); } } if (KeyArg.Length >= 4 && KeyArg.Length <= 5) { TranslateKeyArray[i].KeyPressInterval = new mpTime(KeyArg[3]); } if (KeyArg.Length == 5) { TranslateKeyArray[i].KeyDownUpInterval = new mpTime(KeyArg[4]); } if (KeyArg.Length > 5) { throw new Exception(string.Format("Invalid Key Array: {{{0}}}", Keys[i])); } } else if (KeyArg[0] == "MM") { TranslateKeyArray[i].Type = mpKeyType.MM; if (KeyArg.Length == 3) { TranslateKeyArray[i].IsMouseMove = true; TranslateKeyArray[i].MouseMove_X = int.Parse(KeyArg[1]); TranslateKeyArray[i].MouseMove_Y = int.Parse(KeyArg[2]); } else { throw new Exception(string.Format("Invalid Mouse Move Array: {{{0}}}", Keys[i])); } } else if (KeyArg[0] == "MC") { TranslateKeyArray[i].Type = mpKeyType.MC; if (KeyArg.Length == 2) { if (KeyArg[1] == "R") { TranslateKeyArray[i].MouseClick_Right = true; } } else { throw new Exception(string.Format("Invalid Mouse Click Array: {{{0}}}", Keys[i])); } } else if (KeyArg[0] == "D") { TranslateKeyArray[i].Type = mpKeyType.D; if (KeyArg.Length == 2) { TranslateKeyArray[i].DelayInterval = new mpTime(KeyArg[1]); } else { throw new Exception(string.Format("Invalid Delay Array: {{{0}}}", Keys[i])); } } } return TranslateKeyArray; }
// Known issue: // If using Hotkey, Ctrl, Alt, Shift + ? will block app response posted message. // Win + ? works fine. private void SendKey(IntPtr hWnd, mpKey mpKey) { WindowsAPI.PostMessageA(hWnd, WindowsAPI.WM.WM_KEYDOWN, mpKey.Key, 0); if (mpKey.KeyPressOption == KeyPressOption.KeyDownAndUp) { mpTimer.Sleep(mpKey.KeyDownUpInterval); WindowsAPI.PostMessageA(hWnd, WindowsAPI.WM.WM_KEYUP, mpKey.Key, 0); } mpTimer.Sleep(mpKey.KeyPressInterval); }