Global Hotkeys and Mouse-Click Forging

Discussion in 'Programming General' started by crackmyknuckles, Jan 1, 2010.

Global Hotkeys and Mouse-Click Forging
  1. Unread #1 - Jan 1, 2010 at 6:16 AM
  2. crackmyknuckles
    Joined:
    Jan 9, 2009
    Posts:
    15
    Referrals:
    0
    Sythe Gold:
    0

    crackmyknuckles Newcomer

    Global Hotkeys and Mouse-Click Forging

    I was Alching in Runescape one day and realized just how annoying it is to have to click the mouse so much. It would be much simpler to just press a button on the keyboard, making sure A. my mouse doesn't move accidentally and B. I wouldn't have to fuss with the thing not working (cheap mice suck ass).

    So after some googleing and modification of other source I have I came up with this. Basically, you press a button you program in to the code (hard code) on your keyboard and your mouse clicks...

    I'm going to step you through how to set up Global Hotkeys and how to Forge Mouse-Clicks.

    First off you need to create a new project, once that's done open the code page of the form and add this using:

    Code:
    using System.Runtime.InteropServices;
    Now just under the class decleration your going to add some Class Level Variables:

    Code:
    private IntPtr KeyID;
    private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
    
    Then go back to the GUI (the form) and access the Load event and add this to it:

    Code:
    KeyID = GlobalAddAtom("ClickKey");
    if (RegisterHotKey(this.Handle, (int)KeyID, 0, (int)Keys.Next) == false)
       {
            MessageBox.Show("Registering the HotKey Failed");
       }
    Ok I'll explain a few things here cause other wise you'll think you broke something. The class level variable and constants is as follows: The KeyID variable holds a unique identifier of the type IntPtr, this is used later to determine whether the key we set as the hotkey was pressed as later we will be capturing and examining every event that goes to the entire OS. the 2 constants and the messages we will be sending when our hotkey is pressed, I don't know much about this but I know it works. Also take note that at this point you will have quite a few red squiggles as there are a few methods called already that haven't been defined, but those are imported from the un-managed OS DLL's so I save those for last.

    Lets continue.

    Next, Back in the code window type the following under the closing brace of the load event:

    Code:
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312)
        {
               int key = m.WParam.ToInt32();
               if (key == (int)KeyID)
               {
                   FakeClick(Cursor.Position);
               }
    
        }
        base.WndProc(ref m);
    }
    What this code does is watches every event and if it matches a key press event (0x0312) it checks again to see if the ID matches our Unique one supplied in the load event.

    Onward we go,

    Now go to the form and in the closed event type:

    Code:
    UnregisterHotKey(this.Handle, (int)KeyID);
    GlobalDeleteAtom(KeyID);
    This removes the hotkey and frees the Unique id so it may be used later.

    Ok only 1 more method and them the un-mannaged methods:

    Back at CLass level under the closed event closing bracket, type this:

    Code:
    public static void FakeClick(Point MouseLocation)
            {
                Cursor.Position = MouseLocation;
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
            }
    This is the heart of the application, this is what clicks the mouse its actually pretty simple. I dont really know what all of it does because i havnt gone through the MSDN about it but it works so hey...

    Anyway now we add the un managed methods, add this to the bottom of the class under the closing brace of the Fake Click method:

    Code:
    [DllImport("user32")]
            internal static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
            [DllImport("user32")]
            internal static extern bool UnregisterHotKey(IntPtr hWnd, int id);
            [DllImport("kernel32")] // GloablAddAtomA
            internal static extern IntPtr GlobalAddAtom(string lpString);
            [DllImport("kernel32")]
            internal static extern IntPtr GlobalDeleteAtom(IntPtr nAtom);
            [DllImport("user32.dll")]
            private static extern void mouse_event(UInt32 dwFlags, UInt32 dX, UInt32 dY, UInt32 dwData, IntPtr dwExtraInfo);
    What these are are the unmanaged methods needed to make this work. nothing really important other than RegisterHotkey method.

    FOr the first paramater it takes the handle of the form, you can do this by typing in this.Handle. the second paramater is the keyID, in this app you made sure the id was unique by using the GlobalAddAtom() method. The third paramater is the modifiers for the hotkey such as ALT+Key or CTRL+Key. ALT = 1, CTRL = 2, SHIFT = 4 and any combination can be achieved by adding the modifiers values together. The fourth and final parameter is the actual key we will be making a hot key, I used Pagedown in this example by using the Keys Enumeration of Keys.Next just remember to cast whatever you use to an integer by placing (int) at the start like (int)Keys.Next.

    Here is the full code...

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace Clicker
    {
        public partial class Form1 : Form
        {
            IntPtr KeyID;
            public const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
            public const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                KeyID = GlobalAddAtom("ClickKey");
                if (RegisterHotKey(this.Handle, (int)KeyID, 0, (int)Keys.Next) == false)
                {
                    MessageBox.Show("Registering The Hotkey failed");
                }
            }
    
            #region UnManagedMethods
            [DllImport("user32")]
            internal static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
            [DllImport("user32")]
            internal static extern bool UnregisterHotKey(IntPtr hWnd, int id);
            [DllImport("kernel32")] // GloablAddAtomA
            internal static extern IntPtr GlobalAddAtom(string lpString);
            [DllImport("kernel32")]
            internal static extern IntPtr GlobalDeleteAtom(IntPtr nAtom);
            [DllImport("user32.dll")]
            private static extern void mouse_event(UInt32 dwFlags, UInt32 dX, UInt32 dY, UInt32 dwData, IntPtr dwExtraInfo);
            #endregion
    
            #region HotKey Handler
            protected override void WndProc(ref Message m)
            {
                if (m.Msg == 0x0312)
                {
                    int key = m.WParam.ToInt32();
                    if (key == (int)KeyID)
                    {
                        FakeClick(Cursor.Position);
    
                        MessageBox.Show("Clicked: " + Cursor.Position.ToString());
                    }
    
                }
                base.WndProc(ref m);
            }
    
            public static void FakeClick(Point MouseLocation)
            {
                Cursor.Position = MouseLocation;
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
            }
    
            #endregion
    
            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                UnregisterHotKey(this.Handle, (int)KeyID);
                GlobalDeleteAtom(KeyID);
            }
        }
    }
    
    Please ignore any spelling mistakes as i wrote this at 6 am. Also i tested it a bit and it seems that there is no difference between the click messages sent by this and the messages sent by your mouse. however dont hold me to that use it at your own risk...
     
  3. Unread #2 - Jan 1, 2010 at 6:35 AM
  4. jake__
    Joined:
    Jun 27, 2008
    Posts:
    484
    Referrals:
    0
    Sythe Gold:
    0

    jake__ Forum Addict

    Global Hotkeys and Mouse-Click Forging

    Check out my auto-alching guide if it ends up not working. Even if you don't want to auto, you can substitute the autoclicker for pressing 5 while watching TV or something ;D
     
< I/O streams, need help! | Opacity changer >

Users viewing this thread
1 guest


 
 
Adblock breaks this site