Example #1
0
        private void btnKeyHook_Click(object sender, EventArgs e)
        {
            //We check if globalKeyHook is instantiated or not.
            if (globalKeyHook == null)
            {
                //If the globalKeyhook isn't created, we instantiate it and subscribe to the available events.
                globalKeyHook               = new GlobalKeyHook();
                globalKeyHook.OnKeyDown    += GlobalKeyHook_OnKeyDown;
                globalKeyHook.OnKeyPressed += GlobalKeyHook_OnKeyPressed;
                globalKeyHook.OnKeyUp      += GlobalKeyHook_OnKeyUp;

                //We also need to change the text of the button itself to let the user know that he or she can remove the key hook by clicking it.
                btnKeyHook.Text = REMOVE_KEY_HOOK;

                //Enable the disable left mouse button and mouse movement checkboxes as we can now detect key press for the A key.
                checkNoMouseMove.Enabled = true;
                checkNoLeftMouse.Enabled = true;

                //Also make the status label text which tells us how to disble tricks via keyboard, visible by changing the font color.
                statusADisableTricks.ForeColor = Color.FromKnownColor(KnownColor.ControlText);
            }
            else
            {
                //If the globablKeyHook is already created, we dispose the instance.
                globalKeyHook.Dispose();
                globalKeyHook = null; //Probably not needed but just to be sure.

                //Change the text of the button itself to its defaults.
                btnKeyHook.Text = INSTALL_KEY_HOOK;

                //Uncheck and disable the disable left mouse button and mouse movement checkboxes as we do not have anyways of unticking it.
                checkNoLeftMouse.Checked = false;
                checkNoMouseMove.Checked = false;
                checkNoMouseMove.Enabled = false;
                checkNoLeftMouse.Enabled = false;

                // Also make the status label text which tells us how to disble tricks via keyboard, not visible by changing the font color.
                statusADisableTricks.ForeColor = Color.FromKnownColor(KnownColor.Control);
            }
        }