예제 #1
0
        private void loadAMacroToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filePath = string.Empty;
            using(OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "Macro File|*.bmr";
                if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    return;
                filePath = ofd.FileName;
            }
            try
            {
                IMacroEvent[] loadedEvents = null;
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                using (BinaryReader br = new BinaryReader(fs))
                {
                    loadedEvents = new IMacroEvent[br.ReadInt32()];
                    int eventIndex = 0;
                    while(fs.Position != fs.Length)
                    {
                        MacroEventType mType = (MacroEventType)br.ReadByte();
                        if(mType == MacroEventType.Delay)
                        {
                            loadedEvents[eventIndex] = new MacroEvent_Delay(br.ReadInt32());
                        }

                        if(mType == MacroEventType.Keyboard)
                        {
                            eKeyboardEvent kbEvent = (eKeyboardEvent)br.ReadInt32();
                            KeyboardEvent eventData = new KeyboardEvent();
                            eventData.vKeyCode = br.ReadUInt32();
                            eventData.scanCode = br.ReadUInt32();
                            eventData.Flags = br.ReadUInt32();
                            eventData.Time = 0;
                            eventData.dwExtraInfo = WinApi.GetMessageExtraInfo();
                            loadedEvents[eventIndex] = new MacroEvent_Keyboard(kbEvent, eventData);
                        }

                        if(mType == MacroEventType.Mouse)
                        {
                            eMouseButton mButton = (eMouseButton)br.ReadInt32();
                            MouseEvent mEvent = new MouseEvent();
                            mEvent.mouseData = br.ReadUInt32();
                            mEvent.Location = new mPoint();
                            mEvent.Location.X = br.ReadInt32();
                            mEvent.Location.Y = br.ReadInt32();
                            mEvent.Flags = br.ReadUInt32();
                            loadedEvents[eventIndex] = new MacroEvent_Mouse(mEvent, mButton);
                        }
                        eventIndex++;
                    }
                }
                ClearCurrent();
                foreach (IMacroEvent lEvent in loadedEvents)
                    AddEvent(lEvent);
                MessageBox.Show("Loaded successfully");
            }
            catch
            {
                MessageBox.Show("Failed to load macro.");
            }
        }
예제 #2
0
        private IntPtr KeyboardEventCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            eKeyboardEvent eKB = (eKeyboardEvent)wParam;
            if(!(nCode < 0) && (eKB == eKeyboardEvent.KeyDown || eKB == eKeyboardEvent.KeyUp))
            {
                lock (this)
                {
                    KeyboardEvent kbEvent = (KeyboardEvent)Marshal.PtrToStructure(lParam, typeof(KeyboardEvent));
                    DateTime cTime = DateTime.Now;
                    int delay = (cTime - delayBegin).Milliseconds;

                    MacroEvent_Delay dEvent = new MacroEvent_Delay(delay);
                    MacroEvent_Keyboard kEvent = new MacroEvent_Keyboard(eKB, kbEvent);

                    if (PopulateInternalEventList)
                    {
                        EventList.Add(dEvent);
                        EventList.Add(kEvent);
                    }

                    if (OnEvent != null)
                    {
                        OnEvent(dEvent);
                        OnEvent(kEvent);
                    }
                    delayBegin = cTime;
                }
            }
            return WinApi.CallNextHookEx(KeyboardHook, nCode, wParam, lParam);
        }