Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. C# Keybind for Autoclicker

C# Keybind for Autoclicker

Scheduled Pinned Locked Moved C#
csharplinqgraphicshelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aaudiio
    wrote on last edited by
    #1

    Hey. I'm looking for help with my autoclicker. I got it working with buttons, but I need keybinds instead.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace auto_click
    {

    public partial class Form1 : Form
    {
    
        
        \[System.Runtime.InteropServices.DllImport("user32.dll")\]
        public static extern void mouse\_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    
        public const int MOUSEEVENTF\_LEFTDOWN = 0x02;
        public const int MOUSEEVENTF\_LEFTUP = 0x04;
        public const int MOUSEEVENTF\_RIGHTDOWN = 0x08;
        public const int MOUSEEVENTF\_RIGHTUP = 0x10;
    
    
    
        
        public void MouseClick()
        {
            mouse\_event(MOUSEEVENTF\_LEFTDOWN, 0, 0, 0, 0);
            mouse\_event(MOUSEEVENTF\_LEFTUP, 0, 0, 0, 0);
        }
        public Form1()
        {
            InitializeComponent();
        }
    
    
        private void timer1\_Tick(object sender, EventArgs e)
        {
            MouseClick();
        }
    
        private void Form1\_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "F") ;
            timer1.Enabled = true;
        }
    }
    

    }

    Any idea why it doesn't work?

    REEEEEEEEEEEEH

    Z O 2 Replies Last reply
    0
    • A Aaudiio

      Hey. I'm looking for help with my autoclicker. I got it working with buttons, but I need keybinds instead.

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;

      namespace auto_click
      {

      public partial class Form1 : Form
      {
      
          
          \[System.Runtime.InteropServices.DllImport("user32.dll")\]
          public static extern void mouse\_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
      
          public const int MOUSEEVENTF\_LEFTDOWN = 0x02;
          public const int MOUSEEVENTF\_LEFTUP = 0x04;
          public const int MOUSEEVENTF\_RIGHTDOWN = 0x08;
          public const int MOUSEEVENTF\_RIGHTUP = 0x10;
      
      
      
          
          public void MouseClick()
          {
              mouse\_event(MOUSEEVENTF\_LEFTDOWN, 0, 0, 0, 0);
              mouse\_event(MOUSEEVENTF\_LEFTUP, 0, 0, 0, 0);
          }
          public Form1()
          {
              InitializeComponent();
          }
      
      
          private void timer1\_Tick(object sender, EventArgs e)
          {
              MouseClick();
          }
      
          private void Form1\_KeyDown(object sender, KeyEventArgs e)
          {
              if (e.KeyCode.ToString() == "F") ;
              timer1.Enabled = true;
          }
      }
      

      }

      Any idea why it doesn't work?

      REEEEEEEEEEEEH

      Z Offline
      Z Offline
      Zain Ul Abidin
      wrote on last edited by
      #2

      first confirm whether your timer is starting or not some times timer itself does not starts and people start messing with the code inside timer tick event handler

      1 Reply Last reply
      0
      • A Aaudiio

        Hey. I'm looking for help with my autoclicker. I got it working with buttons, but I need keybinds instead.

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows.Forms;

        namespace auto_click
        {

        public partial class Form1 : Form
        {
        
            
            \[System.Runtime.InteropServices.DllImport("user32.dll")\]
            public static extern void mouse\_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        
            public const int MOUSEEVENTF\_LEFTDOWN = 0x02;
            public const int MOUSEEVENTF\_LEFTUP = 0x04;
            public const int MOUSEEVENTF\_RIGHTDOWN = 0x08;
            public const int MOUSEEVENTF\_RIGHTUP = 0x10;
        
        
        
            
            public void MouseClick()
            {
                mouse\_event(MOUSEEVENTF\_LEFTDOWN, 0, 0, 0, 0);
                mouse\_event(MOUSEEVENTF\_LEFTUP, 0, 0, 0, 0);
            }
            public Form1()
            {
                InitializeComponent();
            }
        
        
            private void timer1\_Tick(object sender, EventArgs e)
            {
                MouseClick();
            }
        
            private void Form1\_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode.ToString() == "F") ;
                timer1.Enabled = true;
            }
        }
        

        }

        Any idea why it doesn't work?

        REEEEEEEEEEEEH

        O Offline
        O Offline
        OmegaExtern
        wrote on last edited by
        #3

        First off all, you are dealing with deprecated method (mouse_event). Second, I think you will need KeyPreview enabled/set to true for your form (look on Form1 properties). Third, "if (e.KeyCode.ToString() == "F") ;" wtf? That "if" branch statement does nothing, why? You assigned it to empty (;) and by not using "{" and "}" it basically mean "if" is limited to single line.. code style.. Fourth, your if statement comparison should be made from "e.KeyCode.ToString() == "F" to "e.KeyCode == Keys.F". Fifth, are you sure your events (timer1_Tick/Form1_KeyDown) are assigned, that could be a main cause, "this code does nothing", as I don't see anything in your constructor, "InitializeComponent();", and you didn't posted code of your (*.Designer.cs) Form1.Designer.cs file in this case. Sixth, it is a lot of bad practice in here so I will jump to seventh. Seventh, you can't just copy/paste into C# always double-check everything such as Win32 native API you deal with, it maybe have wrong signature from my experience there are a lot of examples also 90% of all Windows native APIs from pinvoke.net are NOT correctly defined, so sad... I have fixed your code, and it works fine for me, all you need to do is keep up with my friendly warnings. If you are not that smart, well, here is my smart suggestion for you, why not do: "timer1.Enabled = Keys.F;" in Form1_KeyDown event (Keys.F will be 1/true when hold down; otherwise 0/false). And yeah, do not forget mouse_event() is deprecated method and is no longer officially supported (so I should not be even giving you away these nice warnings). Switch to SendInput function with structures, it is not that hard. Best regards, OmegaExtern.

        A 1 Reply Last reply
        0
        • O OmegaExtern

          First off all, you are dealing with deprecated method (mouse_event). Second, I think you will need KeyPreview enabled/set to true for your form (look on Form1 properties). Third, "if (e.KeyCode.ToString() == "F") ;" wtf? That "if" branch statement does nothing, why? You assigned it to empty (;) and by not using "{" and "}" it basically mean "if" is limited to single line.. code style.. Fourth, your if statement comparison should be made from "e.KeyCode.ToString() == "F" to "e.KeyCode == Keys.F". Fifth, are you sure your events (timer1_Tick/Form1_KeyDown) are assigned, that could be a main cause, "this code does nothing", as I don't see anything in your constructor, "InitializeComponent();", and you didn't posted code of your (*.Designer.cs) Form1.Designer.cs file in this case. Sixth, it is a lot of bad practice in here so I will jump to seventh. Seventh, you can't just copy/paste into C# always double-check everything such as Win32 native API you deal with, it maybe have wrong signature from my experience there are a lot of examples also 90% of all Windows native APIs from pinvoke.net are NOT correctly defined, so sad... I have fixed your code, and it works fine for me, all you need to do is keep up with my friendly warnings. If you are not that smart, well, here is my smart suggestion for you, why not do: "timer1.Enabled = Keys.F;" in Form1_KeyDown event (Keys.F will be 1/true when hold down; otherwise 0/false). And yeah, do not forget mouse_event() is deprecated method and is no longer officially supported (so I should not be even giving you away these nice warnings). Switch to SendInput function with structures, it is not that hard. Best regards, OmegaExtern.

          A Offline
          A Offline
          Aaudiio
          wrote on last edited by
          #4

          Thanks a lot. As you can tell, I know almost nothing. I'm just trying to pick up on the basics by trying to make things.

          O 1 Reply Last reply
          0
          • A Aaudiio

            Thanks a lot. As you can tell, I know almost nothing. I'm just trying to pick up on the basics by trying to make things.

            O Offline
            O Offline
            OmegaExtern
            wrote on last edited by
            #5

            Re-Edited my post, refresh your page. I glad I can help you :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups