// --| Alert and do things according to user settings
        // --| I created this function to avoid code duplication since i am going to use it twice in keyboard press event hook and mouse movement
        public void AlertAndProtect()
        {
            // "Play a sound on detection" is checked so we play our alarm sound file on loop until he clicks "Okay." button on the other warning form
            if (chkBox_Alert.Checked)
            {
                SoundPlayer CustomSound = new SoundPlayer(Properties.Resources.alarm);
                CustomSound.PlayLooping();
            }

            // --| "lock windows on movement detection" is checked so we lock windows
            if (chkBox_Lock.Checked)
            {
                LockWorkStation();
            }

            // --| We stop all our timers and scanners since detection has been made
            ScannerTimer.Stop();
            ScannerTimer.Enabled = false;

            IdleChekTimer.Stop();
            IdleChekTimer.Enabled = false;

            // --| Display our warning form
            Form3 WarningForm = new Form3();

            WarningForm.Show();
        }
        // --| User clicked the "Stop Scanner" button
        // --| Now if the scanner is not running at all we throw an error message
        // --| If the scanner has been working but process was stopped by the button, we throw an info message
        // --| We also stop all scanners
        private void button1_Click_1(object sender, EventArgs e)
        {
            // --| If the "Auto-Start if system is idle" is checked, first we stop that
            if (chkBox_Idle.Checked == true)
            {
                chkBox_Idle.Checked = false;

                IdleChekTimer.Stop();
                IdleChekTimer.Enabled = false;

                isScannerDetected = false;

                // --| Disable radar.GIF animation
                pictureBox2_Scanner.Enabled = false;

                MessageBox.Show("Scanner Stopped", "Scanner has ben stopped.", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }

            // --| If there is no scanner running, we throw an information
            if (timer2.Enabled == false)
            {
                MessageBox.Show("Scanner not running", "Scanner is not running!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                return;
            }

            // --| We stop the scanners
            ScannerTimer.Stop();
            ScannerTimer.Enabled = false;

            timer2.Stop();
            timer2.Enabled = false;

            isScannerDetected = false;

            // --| Disable radar.GIF animation
            pictureBox2_Scanner.Enabled = false;

            MessageBox.Show("Scanner Stopped", "Scanner has ben stopped.", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        // --| User unchecked device "Keyboard"
        // --| If the Scanner or Auto-start on IDLE scanner is active, we disable them since we cannot run a scanner without having what to detect"
        private void chkBox_Keyboard_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBox_Keyboard.Enabled == false || chckBox_Mouse.Checked == false)
            {
                if (ScannerTimer.Enabled == true || timer2.Enabled == true)
                {
                    // --| We stop all our timers and scanners since detection has been made
                    ScannerTimer.Stop();
                    ScannerTimer.Enabled = false;

                    timer2.Stop();
                    timer2.Enabled = false;

                    // --| Disable radar.GIF animation
                    pictureBox2_Scanner.Enabled = false;

                    MessageBox.Show("Scanner stopped, please select at least one device!", "Scanner Stopped", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (IdleChekTimer.Enabled == true)
                {
                    // --| Uncheck our "Auto-Start if system is idle" checkbox
                    chkBox_Idle.Checked = false;

                    // --| Disable radar.GIF animation
                    pictureBox2_Scanner.Enabled = false;

                    IdleChekTimer.Stop();
                    IdleChekTimer.Enabled = false;

                    MessageBox.Show("Scanner stopped, please select at least one device!", "Scanner Stopped", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }
        }
        private void chkBox_Idle_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBox_Idle.Checked == true)
            {
                // --| Check if the scanner is running or in process to start and throw a warning message to the user, while automatically unchecking the checkbox since we
                // --| don't want two scanning processes to start at the same time
                if (ScannerTimer.Enabled == true || timer2.Enabled == true)
                {
                    chkBox_Idle.Checked = false;
                    MessageBox.Show("You need to turn off the Scanner first in order to use this setting!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    // --| Enable radar.GIF animation
                    pictureBox2_Scanner.Enabled = true;

                    return;
                }

                // --| If there is no device checked, throw an error
                if (chckBox_Mouse.Checked == false && chkBox_Keyboard.Checked == false)
                {
                    // --| Uncheck our "Auto-Start if system is idle" checkbox
                    chkBox_Idle.Checked = false;

                    MessageBox.Show("Try to select at least one device!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    // --| Disable radar.GIF animation
                    pictureBox2_Scanner.Enabled = false;

                    return;
                }

                // --| If scanner is not running or not in process to start then we start it
                else
                {
                    // --| Get value from "Idle Control" Dropdown list
                    IdleIntervalDropdownListCheck();

                    // --| Enable radar.GIF animation
                    pictureBox2_Scanner.Enabled = true;

                    // --| Start searching if system is IDLE
                    IdleChekTimer.Start();
                    IdleChekTimer.Enabled = true;

                    // --| Since we don't want two timers to run at a time, when "Auto-Start if system is idle" is checked
                    // --| we disable "Start Scanner" button and we disable the "Scanner Start Delay" dropdown list
                    btn_StartScanner.Enabled        = false;
                    comboBox_StartSelection.Enabled = false;
                }
            }

            // --| If checkbox is unchecked then we enable "Start Scanner" button and "Scanner Start Delay" dropdown list again
            // --| Also we disable the timer where it checks for system being idle or not
            if (chkBox_Idle.Checked == false)
            {
                btn_StartScanner.Enabled        = true;
                comboBox_StartSelection.Enabled = true;

                // --| Disable radar.GIF animation
                pictureBox2_Scanner.Enabled = false;

                IdleChekTimer.Stop();
                IdleChekTimer.Enabled = false;
            }
        }
        // --| When program starts, we disable every scanning timer and set the default value for combobox Scanner Start Delay to 4 (1 Minute)
        // --| And set the default value for combobox IDLE Control to 1 (5 minutes)
        // --| We also disable the notification icon, since our program is maximised and we do some more settings
        private void Form1_Load(object sender, EventArgs e)
        {
            // --| If program was reopened and user made settings on the program, we load saved settings
            // --| Load saved values for checkbox setting made by the user
            chkBox_Startup.Checked  = Properties.Settings.Default.RunonStartup_Setting;
            chkBox_StartMin.Checked = Properties.Settings.Default.Startminimized_Setting;
            chkBox_Alert.Checked    = Properties.Settings.Default.Playsound_Setting;
            chkBox_Lock.Checked     = Properties.Settings.Default.Lockmachine_Setting;
            chkBox_Idle.Checked     = Properties.Settings.Default.AutoStartonIdle_Setting;
            chkBox_Keyboard.Checked = Properties.Settings.Default.DeviceKeyboard_Settings;
            chckBox_Mouse.Checked   = Properties.Settings.Default.DeviceMouse_Settings;

            // --| Load dropdown lists settings made by the user
            comboBox_StartSelection.SelectedIndex = Properties.Settings.Default.StartDelay_Setting;
            comboBox_IdleTime.SelectedIndex       = Properties.Settings.Default.IdleControl_Settings;

            // --| Disable radar.GIF animation
            pictureBox2_Scanner.Enabled = false;

            // --| If checkbox "Auto-Start if system is idle is checked" we start our idle check timer
            if (chkBox_Idle.Checked == true)
            {
                IdleChekTimer.Start();
                IdleChekTimer.Enabled = true;

                // --| Enable radar.GIF animation
                pictureBox2_Scanner.Enabled = true;

                // --| We also disable "Start Scanner" button and "Scanner Delay" dropdown list
                comboBox_StartSelection.Enabled = false;
                btn_StartScanner.Enabled        = false;
            }

            // --| Reset our scanners
            ScannerTimer.Enabled  = false;
            timer2.Enabled        = false;
            IdleChekTimer.Enabled = false;

            isScannerDetected   = false;
            notifyIcon1.Visible = false;


            // --| We start our program minimised to system tray
            if (chkBox_StartMin.Checked == true)
            {
                WindowState = FormWindowState.Minimized;

                if (FormWindowState.Minimized == WindowState)
                {
                    Hide();

                    notifyIcon1.Visible = true;
                    this.ShowInTaskbar  = false;
                }
            }

            // --| If the program is already opened, we disable multiple instances of the same program to be opened in the same time
            // --| Only one instance can be opened
            // --| We find our program process and if it is already running, we throw an information error and exit application


            // --| But what if user renames the exe file in something else? No problem, we get the current executable name and we dont allow it to run on
            // --| Multiple instances :p
            // --| Also it does work if you change the executable name
            string ApplicationCurrentName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;

            Process[] pResult = Process.GetProcessesByName(ApplicationCurrentName);

            if (pResult.Length > 1)
            {
                MessageBox.Show("There is already a instance running!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                System.Environment.Exit(0);
            }
        }
        // --| Event for key press from Global Mouse and Keyboard hook
        public void HookKeyDown(object sender, KeyEventArgs e)
        {
            // --| If user presses End KEY while one of the scanners are running, in this way we detect that he knows what key to press
            // --| and we know is the computer admin/owner so we stop the scanners
            if (e.KeyCode == Keys.End)
            {
                if (ScannerTimer.Enabled == true || timer2.Enabled == true)
                {
                    // --| We stop all our timers and scanners since user pressed F2 key so we know he is the owned or the computer
                    ScannerTimer.Stop();
                    ScannerTimer.Enabled = false;

                    timer2.Stop();
                    timer2.Enabled = false;

                    // --| Play a feedback soun so owner knows that program has detected his End shortcut and disabled scanners
                    // --| SOUND TAKEN FOR FREE FROM http://soundjax.com/alarm-1.html
                    SoundPlayer StoppedSound = new SoundPlayer(Properties.Resources.stopped);
                    StoppedSound.Play();
                }

                else if (IdleChekTimer.Enabled == true || chkBox_Idle.Checked == true)
                {
                    // --| Disable idle scanner
                    IdleChekTimer.Stop();
                    IdleChekTimer.Enabled = false;

                    // --| Uncheck "Auto-Start if system is idle" checkbox
                    chkBox_Idle.Checked = false;

                    // --| Re-enable our start scanner button and dropdown list
                    btn_StartScanner.Enabled        = true;
                    comboBox_StartSelection.Enabled = true;

                    // --| Play a feedback soun so owner knows that program has detected his End shortcut and disabled scanners
                    // --| SOUND TAKEN FOR FREE FROM http://soundjax.com/alarm-1.html
                    SoundPlayer StoppedSound = new SoundPlayer(Properties.Resources.stopped);
                    StoppedSound.Play();
                }

                // --| Disable radar.GIF animation
                pictureBox2_Scanner.Enabled = false;

                // --| Disable value for scanner detection
                isScannerDetected = false;

                return;
            }

            // --| If "Keyboard" checkbox is checked then our scanner will detect keypress
            if (chkBox_Keyboard.Checked == true)
            {
                if (ScannerTimer.Enabled == true && timer2.Enabled == false)
                {
                    // --| Our scanner has detected something so we turn it to true value
                    isScannerDetected = true;

                    // --| Do what user selected in settings
                    AlertAndProtect();
                }
            }
        }