// // GetKeyDialog(string message) // Asks the user to press a key and returns the key that the user pressed // private KEYCOMBO GetKeyDialog(string message) { //Construct the key dialog Form keyPrompt = new Form() { FormBorderStyle = FormBorderStyle.FixedSingle, MinimizeBox = false, MaximizeBox = false }; keyPrompt.StartPosition = FormStartPosition.CenterParent; keyPrompt.Width = 250; keyPrompt.Height = 100; keyPrompt.Text = "Clicktastic"; keyPrompt.KeyPreview = true; keyPrompt.Icon = Properties.Resources.clicktastic; keyPrompt.BackColor = Color.Black; Label lblKey = new Label() { Width = 250, Height = 65, ImageAlign = ContentAlignment.MiddleCenter, TextAlign = ContentAlignment.MiddleCenter, Text = message, ForeColor = Color.White }; lblKey.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold); keyPrompt.Controls.Add(lblKey); System.Timers.Timer timer = new System.Timers.Timer(750); int textState = 1; timer.AutoReset = true; timer.Enabled = true; timer.Elapsed += (sender, e) => { try { this.Invoke(new MethodInvoker(() => { switch (textState) { //add dots to the end of the message as time passes case 0: lblKey.Text = message; break; case 1: lblKey.Text = message + "."; break; case 2: lblKey.Text = message + ".."; break; case 3: lblKey.Text = message + "..."; break; } })); textState = (textState + 1) % 4; } catch { } }; KEYCOMBO key = new KEYCOMBO(); Keys lastKey = Keys.None; key.valid = false; //assume invalid unless otherwise stated string strKey = null; keyPrompt.PreviewKeyDown += (sender, e) => { timer.Stop(); //Determine the key pressed strKey = keyStringConverter.KeyToString(e.KeyCode); //Determine key modifiers if (e.Alt && e.KeyCode != Keys.Menu) strKey = "Alt + " + strKey; if (e.Shift && e.KeyCode != Keys.ShiftKey) strKey = "Shift + " + strKey; if (e.Control && e.KeyCode != Keys.ControlKey) strKey = "Ctrl + " + strKey; lblKey.Text = strKey; lastKey = e.KeyCode; }; keyPrompt.KeyDown += (sender, e) => { if (e.Modifiers.Equals(Keys.Alt)) { e.Handled = true; //don't open the menu with alt } }; keyPrompt.KeyUp += (sender, e) => { keyPrompt.Close(); }; lblKey.MouseClick += (sender, e) => { timer.Stop(); if (e.Button == MouseButtons.Left) strKey = "LeftClick"; else if (e.Button == MouseButtons.Right) strKey = "RightClick"; else if (e.Button == MouseButtons.Middle) strKey = "MiddleClick"; else return; //button not recognized string strLastKey = lblKey.Text.Split(' ').Last(); if (strLastKey == "Ctrl" || strLastKey == "Shift" || strLastKey == "Alt") strKey = lblKey.Text + " + " + strKey; lblKey.Text = strKey; lastKey = Keys.None; keyPrompt.Close(); }; keyPrompt.MouseWheel += (sender, e) => { timer.Stop(); if (e.Delta < 0) //negative is down strKey = "MouseWheelDown"; else //positive is up strKey = "MouseWheelUp"; string strLastKey = lblKey.Text.Split(' ').Last(); if (strLastKey == "Ctrl" || strLastKey == "Shift" || strLastKey == "Alt") strKey = lblKey.Text + " + " + strKey; lblKey.Text = strKey; lastKey = Keys.None; keyPrompt.Close(); }; keyPrompt.ShowDialog(); //All done with the dialog keyPrompt.Dispose(); lblKey.Dispose(); key = ParseKEYCOMBO(strKey, lastKey); return key; }
// // PlayKeyCombo(KEYCOMBO key) // Simulates a key press or click from a KEYCOMBO structure // private void PlayKeyCombo(KEYCOMBO key) { if (!key.valid) return; //key combo is invalid, so don't try running it if (profileData.hold) { if (key.isKeyboard) //keyboard key { return; //keyboard keys are not supported in hold mode } else //is mouse { if (key.mouseButton == MOUSEEVENTF_LEFTDOWN) //left mouse click { for (int i = 0; i < profileData.turbo; i++) { mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); } } else if (key.mouseButton == MOUSEEVENTF_RIGHTDOWN) //right mouse click { for (int i = 0; i < profileData.turbo; i++) { mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); } } else if (key.mouseButton == MOUSEEVENTF_MIDDLEDOWN) //middle mouse click { for (int i = 0; i < profileData.turbo; i++) { mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0); } } else //scroll { return; //mouse scroll is not supported in hold mode } } } else //toggle { if (key.isKeyboard) //keyboard key { try { SendKeys.SendWait(profileData.AutoclickKey.cmd); } catch { } } else //is mouse { if (key.mouseButton == MOUSEEVENTF_LEFTDOWN) //left mouse click { for (int i = 0; i < profileData.turbo; i++) { mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } } else if (key.mouseButton == MOUSEEVENTF_RIGHTDOWN) //right mouse click { for (int i = 0; i < profileData.turbo; i++) { mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); } } else if (key.mouseButton == MOUSEEVENTF_MIDDLEDOWN) //middle mouse click { for (int i = 0; i < profileData.turbo; i++) { mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0); } } else //scroll { for (int i = 0; i < profileData.turbo; i++) { mouse_event(MOUSEEVENTF_WHEEL, 0, 0, key.wheel, 0); } } } } }
// // isAutoclickKeySettingsValid(KEYCOMBO key) // Checks if the activation hotkey settings are valid. It throws errors and returns false if not // private Boolean isAutoclickKeySettingsValid(KEYCOMBO key) { if (!key.valid) return false; else if (key.isKeyboard && !isKeyAcceptable(key.key)) { MessageBox.Show("That key is not supported!", "Clicktastic", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } else if (!key.isKeyboard && (key.mouseButton != MOUSEEVENTF_LEFTDOWN && key.mouseButton != MOUSEEVENTF_RIGHTDOWN && key.mouseButton != MOUSEEVENTF_MIDDLEDOWN && key.wheel == 0)) { //unknown combination of settings MessageBox.Show("That button is not supported!", "Clicktastic", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } else if (!key.isKeyboard && key.modifierKeys != Keys.None) { MessageBox.Show("Keyboard combos are not supported with the mouse!", "Clicktastic", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } else if (key.isKeyboard) { if (profileData.hold) //hold is on { DialogResult result = DialogResult.Yes; result = MessageBox.Show("Keyboard keys are not supported in hold mode!\nWould you like to switch back to toggle mode?", "Clicktastic", MessageBoxButtons.YesNo, MessageBoxIcon.Error); if (result == DialogResult.Yes) { ddbActivationMode.SelectedIndex = 0; } else return false; } } else if (profileData.hold && key.wheel != 0) { DialogResult result = DialogResult.Yes; result = MessageBox.Show("Mouse wheel is not supported in hold mode!\nWould you like to switch back to toggle mode?", "Clicktastic", MessageBoxButtons.YesNo, MessageBoxIcon.Error); if (result == DialogResult.Yes) { ddbActivationMode.SelectedIndex = 0; } else return false; } return true; }
// // ParseKEYCOMBO(string strKey, Keys lastKeyCode) // Creates a KEYCOMBO structure out of a string and a keycode // private KEYCOMBO ParseKEYCOMBO(string strKey, Keys lastKeyCode) { KEYCOMBO key = new KEYCOMBO(); if (strKey == null) { key.valid = false; return key; //key is invalid, so give up } key.keyString = strKey; Boolean ctrl = false; Boolean shift = false; Boolean alt = false; key.modifierKeys = Keys.None; key.isKeyboard = false; key.mouseButton = 0; key.wheel = 0; key.key = Keys.None; string[] buttons = strKey.Split(' '); string previous = null; string lastKey = buttons.Last(); foreach (string button in buttons) { if (button == "(~)" || (button == "+" && (button == previous || lastKey != button))) { previous = button; continue; } if (button != lastKey) { if (button == "Ctrl") { ctrl = true; previous = button; continue; } else if (button == "Shift") { shift = true; previous = button; continue; } else if (button == "Alt") { alt = true; previous = button; continue; } } if (button == "LeftClick") key.mouseButton = MOUSEEVENTF_LEFTDOWN; else if (button == "RightClick") key.mouseButton = MOUSEEVENTF_RIGHTDOWN; else if (button == "MiddleClick") key.mouseButton = MOUSEEVENTF_MIDDLEDOWN; else if (button == "MouseWheelDown") key.wheel = -120; else if (button == "MouseWheelUp") key.wheel = 120; else { key.isKeyboard = true; if (button == "Ctrl") ctrl = true; else if (button == "Shift") shift = true; else if (button == "Alt") alt = true; if (!isKeyAcceptable(lastKeyCode)) { key.key = Keys.None; //allow only Ctrl, Shift, or Alt by themselves } else key.key = lastKeyCode; //store the key code } previous = button; } //Add modifier keys if (ctrl) key.modifierKeys = key.modifierKeys | Keys.Control; if (shift) key.modifierKeys = key.modifierKeys | Keys.Shift; if (alt) key.modifierKeys = key.modifierKeys | Keys.Alt; key.cmd = keyStringConverter.KeyToCmd(key.key, key.modifierKeys, profileData.pressEnter, profileData.turbo); if (key.isKeyboard && key.cmd == null) key.valid = false; //key command could not be constructed, so key is invalid else key.valid = true; return key; }
// // isActivationSettingsValid(KEYCOMBO key) // Checks if the activation hotkey settings are valid. It throws errors and returns false if not // private Boolean isActivationSettingsValid(KEYCOMBO key) { if (!key.valid) return false; else if (key.isKeyboard && key.key == Keys.None && key.modifierKeys == Keys.None) { MessageBox.Show("That key is not supported!", "Clicktastic", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } else if (!key.isKeyboard && (key.mouseButton != MOUSEEVENTF_LEFTDOWN && key.mouseButton != MOUSEEVENTF_RIGHTDOWN && key.mouseButton != MOUSEEVENTF_MIDDLEDOWN && key.wheel == 0)) { //unknown combination of settings MessageBox.Show("That button is not supported!", "Clicktastic", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } else if (!key.isKeyboard) { MessageBox.Show("Mouse buttons cannot be used as activator hotkeys!", "Clicktastic", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } else return true; }