예제 #1
0
 void gkh_KeyUp(object sender, KeyEventArgs e)
 {
     if (!Form1.running)
     {
         if (Form1.recording)
         {
             PressedKeyEvent keyInfo = getLastOccurance(Form1.writingChars, (byte)e.KeyValue);
             if (keyInfo != null)
             {
                 keyInfo.duration = Form1.sw.ElapsedMilliseconds - keyInfo.startTime;
             }
         }
         else//To start the macro
         {
             // check if we need to handle the print screen in taking image
             if (e.KeyCode == Keys.PrintScreen && TextDialog.isInScreenshot && !TextDialog.takingScreenshot)
             {
                 TextDialog.takePic();
             }
             else
             {
                 string currentProgram = GetFocusedProcessName();
                 if (!currentProgram.Equals("RecordNplay"))
                 {
                     if (isCombination(0) && Form1.writingChars != null)
                     {
                         new Thread(() =>
                         {
                             form1.runMacro(Form1.writingChars, form1.currentLoop.Text, form1.currentWait.Text);
                         }).Start();
                     }
                     else if (isCombination(1) && Form1.slot1 != null)
                     {
                         new Thread(() =>
                         {
                             form1.runMacro(Form1.slot1, form1.macro1Loop.Text, form1.macro1Wait.Text);
                         }).Start();
                     }
                     else if (isCombination(2) && Form1.slot2 != null)
                     {
                         new Thread(() =>
                         {
                             form1.runMacro(Form1.slot2, form1.macro2Loop.Text, form1.randomMeanTextbox.Text);
                         }).Start();
                     }
                     else if (isCombination(3) && Form1.slot3 != null)
                     {
                         new Thread(() =>
                         {
                             form1.runMacro(Form1.slot3, form1.macro3Loop.Text, form1.macro3Wait.Text);
                         }).Start();
                     }
                 }
             }
         }
     }
     else
     {
         if ((byte)e.KeyValue == 27)           //it means we need to stop the macro
         {
             if (KeysClicker.clickEscape <= 0) // checking if the computer is pressing or human
             {
                 releaseAllKeys();
                 Form1.running = false;
             }
             KeysClicker.clickEscape--; //true when pc is clicking so we turn it to false right after so we know a human is pressing after
         }
     }
     keysHolding.Remove(e.KeyCode);
     //Console.WriteLine("Up\t" + e.KeyCode.ToString());
     //e.Handled = true;
 }
예제 #2
0
        public override void activate()
        {
            while (Form1.running) // check if still need to wait (user didn't press Escape to stop the macro)
            {
                Bitmap   imgAsBitmap     = TextDialog.ByteStringToBitmap(img);
                var      rc              = Screen.PrimaryScreen.Bounds;
                int      calScreenWidth  = rc.Width;
                int      calScreenHeight = rc.Height;
                Bitmap   bm              = new Bitmap(calScreenWidth, calScreenHeight);
                Graphics g          = Graphics.FromImage(bm);
                Mat      button_img = imgAsBitmap.ToMat();
                //get a scrennshot of the pc
                g.CopyFromScreen(0, 0, 0, 0, bm.Size);

                Mat    game_img = bm.ToMat();
                Mat    result   = new Mat();
                double minVal   = 0;
                double maxVal   = 0;
                Point  maxLoc   = new Point();
                Point  minLoc   = new Point();
                //search for matches and then take the highest
                CvInvoke.MatchTemplate(game_img, button_img, result, TemplateMatchingType.CcoeffNormed);
                CvInvoke.MinMaxLoc(result, ref minVal, ref maxVal, ref minLoc, ref maxLoc);

                if (maxVal > threshold)
                {
                    // before we exit we check if we need to click it
                    if (click)
                    {
                        switch (clickType)
                        {
                        case "Left Click":
                            //when we found, we click on the image (middle of the image)
                            MouseClicker.pressLeftMouse(maxLoc.X + x, maxLoc.Y + y);
                            MouseClicker.leaveLeftMouse(maxLoc.X + x, maxLoc.Y + y);
                            break;

                        case "Double Click":
                            //when we found, we click on the image (middle of the image)
                            MouseClicker.pressLeftMouse(maxLoc.X + x, maxLoc.Y + y);
                            MouseClicker.leaveLeftMouse(maxLoc.X + x, maxLoc.Y + y);
                            Thread.Sleep(100);
                            MouseClicker.pressLeftMouse(maxLoc.X + x, maxLoc.Y + y);
                            MouseClicker.leaveLeftMouse(maxLoc.X + x, maxLoc.Y + y);
                            break;

                        case "Right Click":
                            MouseClicker.pressRightMouse(maxLoc.X + x, maxLoc.Y + y);
                            MouseClicker.leaveRighttMouse(maxLoc.X + x, maxLoc.Y + y);
                            break;

                        case "Middle Click":
                            MouseClicker.pressMiddleMouse(maxLoc.X + x, maxLoc.Y + y);
                            MouseClicker.leaveMiddletMouse(maxLoc.X + x, maxLoc.Y + y);
                            break;

                        default:
                            break;
                        }
                    }
                    break;
                }
                game_img.Dispose();
                button_img.Dispose();
                result.Dispose();
                bm.Dispose();
                g.Dispose();
                new ManualResetEvent(false).WaitOne(1); // do sleep for 1 millisecond to not waste CPU (busy waiting)
            }
        }