private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     //Used to override the Close control box to minimize the form instead
     if (!RealClose)
     {
         e.Cancel         = true;
         HideMinimized    = true;
         this.WindowState = FormWindowState.Minimized;
     }
     else
     {
         //Check for currently Toggled Keys, if any, KeyUp them before changing profiles
         if (Bags.CheckForToggle())
         {
             DebugLog.Instance.writeLog("Found Keys currently pressed, Upping those keys", true);
             Bags.UpAllKeys();
         }
         DebugLog.Instance.closeFile();
     }
 }
        private void LoadButtonsfromProfile(string ProfileName)
        {
            //Check for currently Toggled Keys, if any, KeyUp them before changing profiles
            if (Bags.CheckForToggle())
            {
                DebugLog.Instance.writeLog("Found Keys currently pressed, Upping those keys", true);
                Bags.UpAllKeys();
            }

            //Load the Keymap from the .pgm file for this profile
            if (System.IO.File.Exists(Globals.ProfileSavePath + ProfileName + ".pgm"))
            {
                System.IO.FileStream fs = new System.IO.FileStream(Globals.ProfileSavePath + ProfileName + ".pgm", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read);
                LoadFromStream(fs);
                mFileName = fs.Name;
                fs.Close();
                DebugLog.Instance.writeLog("Profile Keyset Loaded from profile: " + ProfileName, true);
            }
            else
            {
                //File doesn't exist, If application Active, Error to user, if not error to log file
                FormWindowState CurrentState = WindowState;
                if (CurrentState != FormWindowState.Minimized)
                {
                    //Form isn't minimized, popup message
                    if (ProfileName != DefGlobalProf)
                    {
                        MessageBox.Show("Error loading Key Mappings for profile " + ProfileName + ". Loading Defaults.", "", MessageBoxButtons.OK);
                    }
                    SaveButtonstoProfile(ProfileName);
                }
                else
                {
                    //Form is minimized, log error
                    DebugLog.Instance.writeLog("Error loading Key Mappings for profile " + ProfileName + ". Loading Defaults.", true);
                }
            }
        }
        public void KeyUp(KeyMap CurrentKey, string ExtraData = "")
        {
            //A special key has been released
            //Determine the type of Special Key being pressed
            //Added for Issue #19
            switch (CurrentKey.Type)
            {
            case 4:
            {
                //4 = Special Key (Mouse, Media, etc)
                switch (CurrentKey.Action)
                {
                case 0:
                {
                    //Left Mouse Button
                    //mouse_event(0x04, 0, 0, 0, 0);
                    INPUT input_up = new INPUT();
                    input_up.type         = INPUT_MOUSE;
                    input_up.mi.dx        = 0;
                    input_up.mi.dy        = 0;
                    input_up.mi.mouseData = 0;
                    input_up.mi.dwFlags   = (int)MOUSEEVENTF_LEFTUP;

                    INPUT[] input = { input_up };

                    SendInput(1, input, Marshal.SizeOf(input_up));
                    break;
                }

                case 1:
                {
                    //Right Mouse Button
                    INPUT input_up = new INPUT();
                    input_up.type         = INPUT_MOUSE;
                    input_up.mi.dx        = 0;
                    input_up.mi.dy        = 0;
                    input_up.mi.mouseData = 0;
                    input_up.mi.dwFlags   = (int)MOUSEEVENTF_RIGHTUP;

                    INPUT[] input = { input_up };

                    SendInput(1, input, Marshal.SizeOf(input_up));
                    break;
                }

                case 2:
                {
                    //Middle Mouse Button
                    INPUT input_up = new INPUT();
                    input_up.type         = INPUT_MOUSE;
                    input_up.mi.dx        = 0;
                    input_up.mi.dy        = 0;
                    input_up.mi.mouseData = 0;
                    input_up.mi.dwFlags   = (int)MOUSEEVENTF_MIDDLEUP;

                    INPUT[] input = { input_up };

                    SendInput(1, input, Marshal.SizeOf(input_up));
                    break;
                }

                default:
                {
                    //Used for any Special Key that can be sent just with the SpecialValue to the Keyboard Input
                    INPUT input_up = new INPUT();
                    input_up.type           = INPUT_KEYBOARD;
                    input_up.ki.wVk         = _SpecialKeys[CurrentKey.Action].SpecialValue;
                    input_up.ki.wScan       = 0;
                    input_up.ki.dwExtraInfo = IntPtr.Zero;
                    input_up.ki.dwFlags     = KEYEVENTF_KEYUP;

                    INPUT[] input = { input_up };

                    SendInput(1, input, Marshal.SizeOf(input_up));
                    break;
                }
                }
                break;
            }

            case 5:
            {
                //5 = Toggle Key
                //Added for Issue #19

                switch (Bags.getValue(CurrentKey.Action))
                {
                case 1:
                {
                    //One Key-up has already been ignored, take this one and release the Key
                    INPUT input_down = new INPUT();
                    input_down.type           = INPUT_KEYBOARD;
                    input_down.ki.wVk         = CurrentKey.Action;
                    input_down.ki.wScan       = 0;
                    input_down.ki.dwExtraInfo = IntPtr.Zero;
                    input_down.ki.dwFlags     = KEYEVENTF_KEYUP;

                    INPUT[] input = { input_down };

                    SendInput(1, input, Marshal.SizeOf(input_down));
                    //Remove Key being "pressed" from Dictionary so it isn't released again later
                    Bags.RemoveToggle(CurrentKey.Action);
                    DebugLog.Instance.writeLog("         Key Upped: " + CurrentKey.Action);
                    break;
                }

                case 2:
                {
                    //Key just pressed ignore this key-up, change State to 1
                    Bags.DecValue(CurrentKey.Action);
                    DebugLog.Instance.writeLog("         First Key Up ignored: " + CurrentKey.Action);
                    break;
                }

                default:
                {
                    //Should never hit here.
                    DebugLog.Instance.writeLog("         Unknown State for key: " + CurrentKey.Action);
                    break;
                }
                }
                break;
            }

            default:
            {
                break;
            }
            }
        }
        //[System.Runtime.InteropServices.DllImport("user32.dll")]
        //public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);


        public void KeyDown(KeyMap CurrentKey, string ExtraData = "")
        {
            //A special key has been pressed
            //Determine the type of Special Key being pressed
            //Added for Issue #19
            switch (CurrentKey.Type)
            {
            case 4:
            {
                //4 = Special Key (Mouse, Media, etc)
                switch (CurrentKey.Action)
                {
                case 0:
                {
                    //Left Mouse Button
                    //Old Code - Mostly Worked
                    //mouse_event(0x02, 0, 0, 0, 0);
                    INPUT input_down = new INPUT();
                    input_down.type         = INPUT_MOUSE;
                    input_down.mi.dx        = 0;
                    input_down.mi.dy        = 0;
                    input_down.mi.mouseData = 0;
                    input_down.mi.dwFlags   = (int)MOUSEEVENTF_LEFTDOWN;

                    INPUT[] input = { input_down };

                    SendInput(1, input, Marshal.SizeOf(input_down));
                    break;
                }

                case 1:
                {
                    //Right Mouse Button
                    INPUT input_down = new INPUT();
                    input_down.type         = INPUT_MOUSE;
                    input_down.mi.dx        = 0;
                    input_down.mi.dy        = 0;
                    input_down.mi.mouseData = 0;
                    input_down.mi.dwFlags   = (int)MOUSEEVENTF_RIGHTDOWN;

                    INPUT[] input = { input_down };

                    SendInput(1, input, Marshal.SizeOf(input_down));
                    break;
                }

                case 2:
                {
                    //Middle Mouse Button
                    INPUT input_down = new INPUT();
                    input_down.type         = INPUT_MOUSE;
                    input_down.mi.dx        = 0;
                    input_down.mi.dy        = 0;
                    input_down.mi.mouseData = 0;
                    input_down.mi.dwFlags   = (int)MOUSEEVENTF_MIDDLEDOWN;

                    INPUT[] input = { input_down };

                    SendInput(1, input, Marshal.SizeOf(input_down));
                    break;
                }

                case 3:
                {
                    //Mouse Vert Scroll
                    INPUT input_down = new INPUT();
                    input_down.type  = INPUT_MOUSE;
                    input_down.mi.dx = 0;
                    input_down.mi.dy = 0;

                    //Check to see if scrolling Up or down
                    if (CurrentKey.CustomData == "1")
                    {
                        input_down.mi.mouseData = 100;
                    }
                    else
                    {
                        input_down.mi.mouseData = -100;
                    }

                    input_down.mi.dwFlags = (int)MOUSEEVENTF_VWHEEL;

                    INPUT[] input = { input_down };

                    SendInput(1, input, Marshal.SizeOf(input_down));
                    break;
                }

                case 4:
                {
                    //Mouse Horz Scroll
                    INPUT input_down = new INPUT();
                    input_down.type  = INPUT_MOUSE;
                    input_down.mi.dx = 0;
                    input_down.mi.dy = 0;

                    //Check to see if scrolling Up or down
                    if (CurrentKey.CustomData == "1")
                    {
                        input_down.mi.mouseData = 100;
                    }
                    else
                    {
                        input_down.mi.mouseData = -100;
                    }

                    input_down.mi.dwFlags = (int)MOUSEEVENTF_HWHEEL;

                    INPUT[] input = { input_down };

                    SendInput(1, input, Marshal.SizeOf(input_down));
                    break;
                }

                //case 5:
                //    {
                //        //Media Play/Pause
                //        INPUT input_down = new INPUT();
                //        input_down.ki.wVk = (ushort)MEDIA_PLAY_PAUSE;
                //        input_down.ki.wScan = 0;
                //        input_down.ki.dwExtraInfo = IntPtr.Zero;
                //        input_down.ki.dwFlags = 0;

                //        INPUT[] input = { input_down };

                //        SendInput(1, input, Marshal.SizeOf(input_down));
                //        break;
                //    }
                default:
                {
                    //Used for any Special Key that can be sent just with the SpecialValue to the Keyboard Input
                    INPUT input_down = new INPUT();
                    input_down.type           = INPUT_KEYBOARD;
                    input_down.ki.wVk         = _SpecialKeys[CurrentKey.Action].SpecialValue;
                    input_down.ki.wScan       = 0;
                    input_down.ki.dwExtraInfo = IntPtr.Zero;
                    input_down.ki.dwFlags     = 0;

                    INPUT[] input = { input_down };

                    SendInput(1, input, Marshal.SizeOf(input_down));

                    break;
                }
                }
                break;
            }

            case 5:
            {
                //5 = Toggle Key
                //Added for Issue #19
                DebugLog.Instance.writeLog("Attempting to Toggle key: " + CurrentKey.Action, true);
                //Check if Key is "0", if so press key (Ignore key in all other states)
                if (Bags.getValue(CurrentKey.Action) == 0)
                {
                    INPUT input_down = new INPUT();
                    input_down.type           = INPUT_KEYBOARD;
                    input_down.ki.wVk         = CurrentKey.Action;
                    input_down.ki.wScan       = 0;
                    input_down.ki.dwExtraInfo = IntPtr.Zero;
                    input_down.ki.dwFlags     = 0;

                    INPUT[] input = { input_down };

                    SendInput(1, input, Marshal.SizeOf(input_down));

                    //Set Flag to "ON"
                    //Add Key being "pressed" to Dictionary for later removal
                    Bags.AddToggle(CurrentKey.Action, 2);
                    DebugLog.Instance.writeLog("         Key Toggled: " + CurrentKey.Action);
                }
                else
                {
                    DebugLog.Instance.writeLog("         Key NOT Toggled: " + CurrentKey.Action);
                }
                break;
            }

            default:
            {
                break;
            }
            }
        }