private void PlaySync() { try { manualResetEvent.Reset(); MouseInput.GetMaxSize(); uint period = Math.Max(periodMin, Math.Min(periodMax, (uint)GetWaitTime((int)minWaitTime)));; if (NativeMethods.timeBeginPeriod(period) != 0) { throw new Exception("timeBeginPeriod failed."); } for (int i = 0; i < times && State == SessionState.Playing; i++) { for (int j = 0; j < packets.Count && State == SessionState.Playing; j++) { Packet packet = packets[j]; WaitPacket wait = packet as WaitPacket; if (wait != null) { sendInput.Flush(); int waitTime = GetWaitTime(wait.Milliseconds); if (waitTime > 0) { manualResetEvent.WaitOne(waitTime); } continue; } InputPacket input = packet as InputPacket; if (input != null) { sendInput.Queue(input.Input); continue; } } } if (State == SessionState.Playing) { sendInput.Flush(); } if (NativeMethods.timeEndPeriod(period) != 0) { throw new Exception("timeEndPeriod failed."); } } catch (Exception ex) { MainForm.ShowError(ex); } State = SessionState.Idle; }
private static void MergeKeyChars(PacketList packets) { List <KeyCharPacket> keyCharPackets = new List <KeyCharPacket>(); StringBuilder sb = new StringBuilder(); int curPos = 0; int lastPos; int postWaitTime = 0; for (lastPos = 0; lastPos < packets.Count; lastPos++) { KeyCharPacket p = packets[lastPos] as KeyCharPacket; if (p == null) { continue; } keyCharPackets.Add(p); for (curPos = lastPos + 1; curPos < packets.Count; curPos++) { WaitPacket w = packets[curPos] as WaitPacket; if (w != null) { postWaitTime += w.Milliseconds; continue; } KeyCharPacket k = packets[curPos] as KeyCharPacket; if (k == null) { break; } keyCharPackets.Add(k); } if (keyCharPackets.Count > 1) { sb.Length = 0; for (int j = 0; j < keyCharPackets.Count; j++) { sb.Append(((KeyCharInput)keyCharPackets[j].Input).Character); } packets.RemoveRange(lastPos, curPos - lastPos); packets.Insert(lastPos, new SendTextInput(sb.ToString()).ToPacket()); if (postWaitTime > 0) { packets.Insert(lastPos + 1, new WaitPacket(postWaitTime)); } } keyCharPackets.Clear(); postWaitTime = 0; } if (keyCharPackets.Count > 1) { sb.Length = 0; for (int j = 0; j < keyCharPackets.Count; j++) { sb.Append(((KeyCharInput)keyCharPackets[j].Input).Character); } packets.RemoveRange(lastPos, curPos - lastPos); packets.Insert(lastPos, new SendTextInput(sb.ToString()).ToPacket()); if (postWaitTime > 0) { packets.Insert(lastPos + 1, new WaitPacket(postWaitTime)); } } }