Guest User

Untitled

a guest
May 6th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Diagnostics;
  6. using System.Threading;
  7.  
  8. namespace StarCraft_II_Macros_Utility
  9. {
  10.     public partial class MainForm : Form
  11.     {
  12.         const string processName = "SC2_x64";
  13.         //const string processName = "notepad++";
  14.        
  15.         Color colorEnabled = Color.Green;
  16.         Color colorDisabled = Color.Red;
  17.        
  18.         int LMBdelay = 500; // мс
  19.         int МMBdelay = 200; // мс
  20.         Keys lastKeyPressed = Keys.None;
  21.        
  22.         string statusFoundText;
  23.         string statusNotFoundText;
  24.        
  25.         bool LMBpressed = false;
  26.         bool MMBpressed = false;
  27.         bool RMBpressed = false;
  28.         bool shiftPressed = false;
  29.         bool controlPressed = false;
  30.         bool altPressed = false;
  31.         bool spacePressed = false;
  32.        
  33.         bool freeCameraModeEnabled = false;
  34.         bool freeCameraMode = false;
  35.         //bool alternativeSelectionMode = false;
  36.         bool basePosition = false;
  37.         bool isEnabled = false;
  38.         bool isUnitGroupKey = false;
  39.         bool isLocGroupKey = false;
  40.         bool isChatMsgKey = false;
  41.         bool isMultickickKeyState = false;
  42.        
  43.        
  44.         int LMBstate = 0;
  45.         int chatMsgKeyState = 0;
  46.         int multiclickKeyState = 0;
  47.        
  48.         HotkeysForm hotkeysForm = null;
  49.        
  50.         //int m_LMBdelay = 0;
  51.         //int m_MMBdelay = 0;
  52.        
  53.         public MainForm()
  54.         {
  55.             InitializeComponent();
  56.            
  57.             LMBdelay /= mainTimer.Interval;
  58.             МMBdelay /= mainTimer.Interval;
  59.            
  60.             statusFoundText = "Процесс \"" + processName + "\" найден.";
  61.             statusNotFoundText = "Процесс \"" + processName + "\" не найден.";
  62.            
  63.             statusTextBox.Text = statusNotFoundText;
  64.             statusTextBox.ForeColor = colorDisabled;
  65.             statusTextBox.BackColor = statusTextBox.BackColor;
  66.             lastKeyPressedLabel.Text = lastKeyPressed.ToString();
  67.             new ToolTip().SetToolTip(cameraModeButton, "Режим управления камерой. Для подробной информации смотри меню \"Хоткеи\"");
  68.             new ToolTip().SetToolTip(lastKeyPressedLabel, "Последняя нажатая клавиша. Для подробной информации смотри меню \"Хоткеи\"");
  69.             ChangeEnabledState();
  70.             ChangeCameraMode();
  71.         }
  72.        
  73.         void HotkeysItemClick(object sender, EventArgs e)
  74.     {
  75.           if (hotkeysForm == null)
  76.           {
  77.             hotkeysForm = new HotkeysForm();
  78.         hotkeysForm.FormClosed += HotkeysFormClosed;
  79.         hotkeysForm.Show();
  80.           }
  81.           else
  82.           {
  83.             hotkeysForm.Focus();
  84.           }
  85.     }
  86.         void HotkeysFormClosed(object sender, EventArgs e)
  87.     {
  88.           hotkeysForm = null;
  89.         }
  90.    
  91.     void CameraModeButtonClick(object sender, EventArgs e)
  92.     {
  93.       freeCameraModeEnabled = !freeCameraModeEnabled;
  94.       ChangeCameraMode();
  95.     }
  96.        
  97.     void ChangeCameraMode()
  98.     {
  99.       if (freeCameraModeEnabled)
  100.       {
  101.         cameraModeButton.BackColor = Color.Yellow;
  102.       }
  103.       else
  104.       {
  105.         cameraModeButton.BackColor = Color.White;
  106.       }
  107.     }
  108.    
  109.     void EnableButtonClick(object sender, EventArgs e)
  110.     {
  111.       ChangeEnabledState();
  112.     }
  113.    
  114.         void ChangeEnabledState()
  115.         {
  116.             isEnabled = !isEnabled;
  117.            
  118.             if (isEnabled)
  119.             {
  120.                 enableButton.Text = "Запущено (\"*\")";
  121.                 enableButton.ForeColor = colorEnabled;
  122.             }
  123.             else
  124.             {
  125.                 enableButton.Text = "Остановлено (\"*\")";
  126.                 enableButton.ForeColor = colorDisabled;
  127.             }
  128.         }
  129.        
  130.         void MainTimerTick(object sender, EventArgs e)
  131.         {
  132.             if (KeyEvents.UpdateKeyStatus(Keys.Multiply))
  133.             {
  134.                 ChangeEnabledState();
  135.             }
  136.            
  137.             if (!isEnabled) return;
  138.            
  139.             Process[] processes = Process.GetProcessesByName(processName);
  140.            
  141.             if (processes == null || processes.Length <= 0)
  142.             {
  143.                 if (statusTextBox.Text != statusFoundText)
  144.                 {
  145.                     statusTextBox.Text = statusNotFoundText;
  146.                     statusTextBox.ForeColor = colorDisabled;
  147.                 }
  148.                
  149.                 return;
  150.             }
  151.            
  152.             Process starCraftII = processes[0];
  153.             if (statusTextBox.Text != statusFoundText)
  154.             {
  155.                 statusTextBox.Text = statusFoundText;
  156.                 statusTextBox.ForeColor = colorEnabled;
  157.             }
  158.            
  159.             if (starCraftII.Responding)
  160.             //if (starCraftII.Responding && !Utilities.IsMinimized(starCraftII))
  161.             {
  162.               KeysStatus();
  163.                 MainLogic();
  164.             }
  165.         }
  166.        
  167.         void KeysStatus()
  168.         {
  169.           if (MouseEvents.GetKeyStatus(MouseButtons.Left)) LMBpressed = true;
  170.       else LMBpressed = false;
  171.      
  172.       if (MouseEvents.GetKeyStatus(MouseButtons.Middle)) MMBpressed = true;
  173.       else MMBpressed = false;
  174.      
  175.       if (MouseEvents.GetKeyStatus(MouseButtons.Right)) RMBpressed = true;
  176.       else RMBpressed = false;
  177.      
  178.      
  179.       if (KeyEvents.UpdateKeyStatus(Keys.ShiftKey)) shiftPressed = true;
  180.       else shiftPressed = false;
  181.      
  182.       if (KeyEvents.UpdateKeyStatus(Keys.ControlKey)) controlPressed = true;
  183.       else controlPressed = false;
  184.      
  185.       if (KeyEvents.UpdateKeyStatus(Keys.Alt)) altPressed = true;
  186.       else altPressed = false;
  187.      
  188.       if (KeyEvents.UpdateKeyStatus(Keys.Space)) spacePressed = true;
  189.       else spacePressed = false;
  190.         }
  191.        
  192.         void MainLogic()
  193.         {
  194.             int key = -1;
  195.          
  196.             if (KeyEvents.UpdateKeyStatus(Keys.Divide))
  197.         {
  198.             freeCameraModeEnabled = !freeCameraModeEnabled;
  199.             ChangeCameraMode();
  200.         }
  201.            
  202.             if (spacePressed)
  203.             {
  204.                 if (freeCameraModeEnabled)
  205.                 {
  206.                     if (freeCameraMode)
  207.                     {
  208.                         MouseEvents.Up(MouseButtons.Middle);
  209.                         Thread.Sleep(50);
  210.                         //MouseEvents.SetCursorPostion(Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/2);
  211.                         freeCameraMode = false;
  212.                     }
  213.                 }
  214.                 else
  215.                 {
  216.                     if (!freeCameraMode)
  217.                     {
  218.                         MouseEvents.Down(MouseButtons.Middle);
  219.                         freeCameraMode = true;
  220.                     }
  221.                 }
  222.             }
  223.             else
  224.             {
  225.                 if (freeCameraModeEnabled)
  226.                 {
  227.                     if (!freeCameraMode)
  228.                     {
  229.                         MouseEvents.Down(MouseButtons.Middle);
  230.                         freeCameraMode = true;
  231.                     }
  232.                 }
  233.                 else
  234.                 {
  235.                     if (freeCameraMode)
  236.                     {
  237.                         MouseEvents.Up(MouseButtons.Middle);
  238.                         Thread.Sleep(50);
  239.                         //MouseEvents.SetCursorPostion(Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/2);
  240.                         freeCameraMode = false;
  241.                     }
  242.                 }
  243.             }
  244.            
  245.             key = NumbericKeyPressed();
  246.             if (key >= 0) isUnitGroupKey = true;
  247.            
  248.             if (key < 0)
  249.             {
  250.               key = MapHotkeyKeyPressed();
  251.               if (key >= 0) isLocGroupKey = true;
  252.             }
  253.            
  254.             if (LMBpressed)
  255.             {
  256.               if (key < 0)
  257.               {
  258.                 if (LMBstate == 0) LMBstate = 1;
  259.               }
  260.               else
  261.               {
  262.                 if (LMBstate == 1) LMBstate = 2;
  263.               }
  264.             }
  265.             else LMBstate = 0;
  266.            
  267.             //Console.WriteLine("LMBstate = " + LMBstate);
  268.             //Console.WriteLine("key = " + key);
  269.            
  270.           if (LMBstate == 2)
  271.       {
  272.             LMBstate = 3;
  273.            
  274. //          if (shiftPressed)
  275. //          {
  276. //            MouseEvents.Up(MouseButtons.Left);
  277. //          MouseEvents.Click(MouseButtons.Left);
  278. //          MouseEvents.Click(MouseButtons.Left);
  279. //          return;
  280. //          }
  281.            
  282.                 if (isUnitGroupKey && key >= 0)
  283.             {
  284.                   isUnitGroupKey = false;
  285.                  
  286.                   if (key == 0 || key > 5)
  287.                   {
  288.                     MouseEvents.Click(MouseButtons.Left);
  289.             MouseEvents.Click(MouseButtons.Left);
  290.                   }
  291.                   else
  292.                   {
  293.                     Point pos = new Point(MouseEvents.CursorPostionX, MouseEvents.CursorPostionY);
  294.                     MouseEvents.SetCursorPostion(0, 0);
  295.                     MouseEvents.Click(MouseButtons.Left);
  296.                     MouseEvents.SetCursorPostion(pos.X, pos.Y);
  297.                   }
  298.            
  299.             if (!controlPressed && !shiftPressed && !altPressed) KeyEvents.Down(Keys.ControlKey);
  300.            
  301.             switch (key)
  302.                     {
  303.                         case 0:
  304.                             KeyEvents.Press(Keys.D0);
  305.                             break;
  306.                         case 1:
  307.                             KeyEvents.Press(Keys.D1);
  308.                             break;
  309.                         case 2:
  310.                             KeyEvents.Press(Keys.D2);
  311.                             break;
  312.                         case 3:
  313.                             KeyEvents.Press(Keys.D3);
  314.                             break;
  315.                         case 4:
  316.                             KeyEvents.Press(Keys.D4);
  317.                             break;
  318.                         case 5:
  319.                             KeyEvents.Press(Keys.D5);
  320.                             break;
  321.                         case 6:
  322.                             KeyEvents.Press(Keys.D6);
  323.                             break;
  324.                         case 7:
  325.                             KeyEvents.Press(Keys.D7);
  326.                             break;
  327.                         case 8:
  328.                             KeyEvents.Press(Keys.D8);
  329.                             break;
  330.                         case 9:
  331.                             KeyEvents.Press(Keys.D9);
  332.                             break;
  333.                     }
  334.            
  335.             if (!controlPressed && !shiftPressed && !altPressed) KeyEvents.Up(Keys.ControlKey);
  336.             return;
  337.             }
  338.            
  339.                 if (isLocGroupKey && key >= 0)
  340.         {
  341.                   isLocGroupKey = false;
  342.                  
  343.               if (!controlPressed) KeyEvents.Down(Keys.ControlKey);
  344.              
  345.               switch (key)
  346.                     {
  347.                 case 5:
  348.                             KeyEvents.Press(Keys.F5);
  349.                             break;
  350.                         case 6:
  351.                             KeyEvents.Press(Keys.F6);
  352.                             break;
  353.                         case 7:
  354.                             KeyEvents.Press(Keys.F7);
  355.                             break;
  356.                         case 8:
  357.               KeyEvents.Press(Keys.F8);
  358.               break;
  359.               }
  360.              
  361.               if (!controlPressed) KeyEvents.Up(Keys.ControlKey);
  362.               return;
  363.             }
  364.                
  365.                 return;
  366.           }
  367.          
  368.           if (KeyEvents.UpdateKeyStatus(Keys.A))
  369.       {
  370.                 KeyEvents.Press(Keys.T);
  371.         MouseEvents.Click(MouseButtons.Left);
  372.         return;
  373.       }
  374.          
  375.           if (KeyEvents.UpdateKeyStatus(Keys.CapsLock))
  376.       {
  377.         basePosition = !basePosition;
  378.        
  379.         if (basePosition)
  380.         {
  381.           if (!controlPressed) KeyEvents.Down(Keys.ControlKey);
  382.           if (!shiftPressed) KeyEvents.Down(Keys.ShiftKey);
  383.           KeyEvents.Press(Keys.F8);
  384.           if (!shiftPressed) KeyEvents.Up(Keys.ShiftKey);
  385.           if (!controlPressed) KeyEvents.Up(Keys.ControlKey);
  386.          
  387.           KeyEvents.Press(Keys.Back);
  388.         }
  389.         else
  390.         {
  391.           if (!shiftPressed) KeyEvents.Down(Keys.ShiftKey);
  392.           KeyEvents.Press(Keys.F8);
  393.           if (!shiftPressed) KeyEvents.Up(Keys.ShiftKey);
  394.         }
  395.       }
  396.      
  397.       if (!controlPressed && !shiftPressed && !altPressed)
  398.       {
  399.         key = NumPadKeyPressed();
  400.        
  401.         if (KeyEvents.UpdateKeyStatus(Keys.Decimal))
  402.         {
  403.           if (key >= 0) isChatMsgKey = true;
  404.          
  405.           if (key < 0)
  406.           {
  407.             if (chatMsgKeyState == 0) chatMsgKeyState = 1;
  408.           }
  409.           else
  410.           {
  411.             if (chatMsgKeyState == 1) chatMsgKeyState = 2;
  412.           }
  413.         }
  414.         if (key < 0 && chatMsgKeyState == 3) chatMsgKeyState = 0;
  415.        
  416.         if (KeyEvents.UpdateKeyStatus(Keys.NumPad0))
  417.         {
  418.           if (key >= 0) isMultickickKeyState = true;
  419.          
  420.           if (key < 0)
  421.           {
  422.             if (multiclickKeyState == 0) multiclickKeyState = 1;
  423.           }
  424.           else
  425.           {
  426.             if (multiclickKeyState == 1) multiclickKeyState = 2;
  427.           }
  428.         }
  429.         if (key < 0 && multiclickKeyState == 3) multiclickKeyState = 0;
  430.        
  431.         if (chatMsgKeyState == 2)
  432.         {
  433.           chatMsgKeyState = 3;
  434.          
  435.           if (isChatMsgKey && key >= 0)
  436.           {
  437.             isChatMsgKey = false;
  438.            
  439.             switch (key)
  440.             {
  441.               case 1:
  442.                 WriteToChat("glhf");
  443.                 break;
  444.               case 2:
  445.                 WriteToChat("yes");
  446.                 break;
  447.               case 3:
  448.                 WriteToChat("no");
  449.                 break;
  450.               case 4:
  451.                 WriteToAllyChat("go!");
  452.                 break;
  453.               case 5:
  454.                 WriteToAllyChat("help!");
  455.                 break;
  456.               case 6:
  457.                 WriteToChat("ok");
  458.                 break;
  459.               case 7:
  460.                 WriteToChat("sorry");
  461.                 break;
  462.               case 8:
  463.                 WriteToChat("?");
  464.                 break;
  465.               case 9:
  466.                 WriteToChat("gg");
  467.                 break;
  468.             }
  469.             return;
  470.           }
  471.         }
  472.        
  473.         if (multiclickKeyState == 2)
  474.         {
  475.           multiclickKeyState = 3;
  476.          
  477.           if (isMultickickKeyState && key >= 0)
  478.           {
  479.             isMultickickKeyState = false;
  480.            
  481.             if (lastKeyPressed != Keys.None)
  482.             {
  483.               if (KeyEvents.UpdateKeyStatus(Keys.NumPad1)) KeyEvents.Press(lastKeyPressed, 1);
  484.               else if (KeyEvents.UpdateKeyStatus(Keys.NumPad2)) KeyEvents.Press(lastKeyPressed, 2);
  485.               else if (KeyEvents.UpdateKeyStatus(Keys.NumPad3)) KeyEvents.Press(lastKeyPressed, 3);
  486.               else if (KeyEvents.UpdateKeyStatus(Keys.NumPad4)) KeyEvents.Press(lastKeyPressed, 4);
  487.               else if (KeyEvents.UpdateKeyStatus(Keys.NumPad5)) KeyEvents.Press(lastKeyPressed, 5);
  488.               else if (KeyEvents.UpdateKeyStatus(Keys.NumPad6)) KeyEvents.Press(lastKeyPressed, 6);
  489.               else if (KeyEvents.UpdateKeyStatus(Keys.NumPad7)) KeyEvents.Press(lastKeyPressed, 7);
  490.               else if (KeyEvents.UpdateKeyStatus(Keys.NumPad8)) KeyEvents.Press(lastKeyPressed, 8);
  491.               else if (KeyEvents.UpdateKeyStatus(Keys.NumPad9)) KeyEvents.Press(lastKeyPressed, 9);
  492.             }
  493.            
  494.             return;
  495.           }
  496.         }
  497.        
  498.       }
  499.      
  500.       if (KeyEvents.UpdateKeyStatus(Keys.Enter) && Control.IsKeyLocked(Keys.CapsLock))
  501.       {
  502.         KeyEvents.Press(Keys.CapsLock);
  503.       }
  504.      
  505.       if (KeyEvents.UpdateKeyStatus(Keys.Add))
  506.       {
  507.         KeyEvents.Press(lastKeyPressed);
  508.         return;
  509.       }
  510.      
  511.       string keyStr = KeyPressed();
  512.       if (keyStr != null)
  513.       {
  514.         if (keyStr != lastKeyPressedLabel.Text) SetLastPressedKey(keyStr);
  515.         MouseEvents.Click(MouseButtons.Left);
  516.       }
  517. //      if (KeyEvents.UpdateKeyStatus(Keys.Subtract))
  518. //      {
  519. //        KeyEvents.Down(Keys.ControlKey);
  520. //        KeyEvents.Press(lastKeyPressed);
  521. //        KeyEvents.Up(Keys.ControlKey);
  522. //        return;
  523. //      }
  524.         }
  525.        
  526.         void WriteToChat(string str)
  527.         {
  528.           KeyEvents.Press(Keys.Enter);
  529.       KeyEvents.FromString(str);
  530.       KeyEvents.Press(Keys.Enter);
  531.       Thread.Sleep(100);
  532.         }
  533.        
  534.         void WriteToAllyChat(string str)
  535.     {
  536.           KeyEvents.Down(Keys.ControlKey);
  537.       KeyEvents.Press(Keys.Enter);
  538.       KeyEvents.Up(Keys.ControlKey);
  539.      
  540.       KeyEvents.FromString(str);
  541.       KeyEvents.Press(Keys.Enter);
  542.       Thread.Sleep(100);
  543.     }
  544.        
  545.         string KeyPressed()
  546.         {
  547.           if (KeyEvents.UpdateKeyStatus(Keys.Q)) return "Q";
  548.           if (KeyEvents.UpdateKeyStatus(Keys.W)) return "W";
  549.           if (KeyEvents.UpdateKeyStatus(Keys.E)) return "E";
  550.           if (KeyEvents.UpdateKeyStatus(Keys.R)) return "R";
  551.           if (KeyEvents.UpdateKeyStatus(Keys.T)) return "T";
  552.          
  553.           if (KeyEvents.UpdateKeyStatus(Keys.A)) return "A";
  554.       if (KeyEvents.UpdateKeyStatus(Keys.S)) return "S";
  555.       if (KeyEvents.UpdateKeyStatus(Keys.D)) return "D";
  556.       if (KeyEvents.UpdateKeyStatus(Keys.F)) return "F";
  557.       if (KeyEvents.UpdateKeyStatus(Keys.G)) return "G";
  558.      
  559.       if (KeyEvents.UpdateKeyStatus(Keys.Z)) return "Z";
  560.       if (KeyEvents.UpdateKeyStatus(Keys.X)) return "X";
  561.       if (KeyEvents.UpdateKeyStatus(Keys.C)) return "C";
  562.       if (KeyEvents.UpdateKeyStatus(Keys.V)) return "V";
  563.       if (KeyEvents.UpdateKeyStatus(Keys.B)) return "B";
  564.          
  565.           return null;
  566.         }
  567.        
  568.         int NumbericKeyPressed()
  569.         {
  570.             if (KeyEvents.UpdateKeyStatus(Keys.D0)) return 0;
  571.             if (KeyEvents.UpdateKeyStatus(Keys.D1)) return 1;
  572.             if (KeyEvents.UpdateKeyStatus(Keys.D2)) return 2;
  573.             if (KeyEvents.UpdateKeyStatus(Keys.D3)) return 3;
  574.             if (KeyEvents.UpdateKeyStatus(Keys.D4)) return 4;
  575.             if (KeyEvents.UpdateKeyStatus(Keys.D5)) return 5;
  576.             if (KeyEvents.UpdateKeyStatus(Keys.D6)) return 6;
  577.             if (KeyEvents.UpdateKeyStatus(Keys.D7)) return 7;
  578.             if (KeyEvents.UpdateKeyStatus(Keys.D8)) return 8;
  579.             if (KeyEvents.UpdateKeyStatus(Keys.D9)) return 9;
  580.            
  581.             return -1;
  582.         }
  583.        
  584.         int MapHotkeyKeyPressed()
  585.         {
  586.             if (KeyEvents.UpdateKeyStatus(Keys.F5)) return 5;
  587.             if (KeyEvents.UpdateKeyStatus(Keys.F6)) return 6;
  588.             if (KeyEvents.UpdateKeyStatus(Keys.F7)) return 7;
  589.             if (KeyEvents.UpdateKeyStatus(Keys.F8)) return 8;
  590.            
  591.             return -1;
  592.         }
  593.        
  594.         int NumPadKeyPressed()
  595.         {
  596.             if (KeyEvents.UpdateKeyStatus(Keys.NumPad1)) return 1;
  597.             if (KeyEvents.UpdateKeyStatus(Keys.NumPad2)) return 2;
  598.             if (KeyEvents.UpdateKeyStatus(Keys.NumPad3)) return 3;
  599.             if (KeyEvents.UpdateKeyStatus(Keys.NumPad4)) return 4;
  600.             if (KeyEvents.UpdateKeyStatus(Keys.NumPad5)) return 5;
  601.             if (KeyEvents.UpdateKeyStatus(Keys.NumPad6)) return 6;
  602.             if (KeyEvents.UpdateKeyStatus(Keys.NumPad7)) return 7;
  603.             if (KeyEvents.UpdateKeyStatus(Keys.NumPad8)) return 8;
  604.             if (KeyEvents.UpdateKeyStatus(Keys.NumPad9)) return 9;
  605.            
  606.             return -1;
  607.         }
  608.        
  609.         void SetLastPressedKey(string keyStr)
  610.         {
  611.           Keys key;
  612.       if (Enum.TryParse(keyStr, out key))
  613.       {
  614.         lastKeyPressed = key;
  615.         lastKeyPressedLabel.Text = key.ToString();
  616.       }
  617.         }
  618.        
  619.         bool Delay(int delay, int maxDelay)
  620.         {
  621.             if (delay >= maxDelay)
  622.             {
  623.                 return true;
  624.             }
  625.            
  626.             return false;
  627.         }
  628.    
  629.     }
  630. }
Advertisement
Add Comment
Please, Sign In to add comment