Showing posts with label Mouse. Show all posts
Showing posts with label Mouse. Show all posts

Saturday, December 11, 2010

C# Mouse

Have you ever wanted to manipulate the mouse cursor programmatically using C#?  Well, look no further, because this small Mouse class puts the mouse actions into a nice, small class you can use in your programs.

If you are still wondering what you can do with this simple class, here are a few examples:
  • Moving the mouse around so the user has no idea what's going on.
  • Making a macro (or "bot") for an online game.
  • Repeatedly clicking the same thing over and over again.

Many of the programs I will post will be based on this Mouse class!


Have a look at the code here (although you can download it at bottom of this post):


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;


static class Mouse
{
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x02;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x04;
    private const UInt32 MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const UInt32 MOUSEEVENTF_RIGHTUP = 0x10;
    private const UInt32 MOUSEEVENTF_MIDDLEDOWN = 0x20;
    private const UInt32 MOUSEEVENTF_MIDDLEUP = 0x40;


    [DllImport("user32.dll")]
    private static extern void mouse_event(
        UInt32 dwFlags, // motion and click options
        UInt32 dx, // horizontal position or change
        UInt32 dy, // vertical position or change
        UInt32 dwData, // wheel movement
        IntPtr dwExtraInfo // application-defined information
    );


public static void Move(Point p)
{
Cursor.Position = p;
}


    public static void Move(int x, int y)
    {
        Mouse.Move(new Point(x, y));
    }


public static void Click()
{
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
}


public static void Click(Point p)
{
Mouse.Move(p);
Mouse.Click();
}


    public static void Click(int x, int y)
    {
        Mouse.Click(new Point(x, y));
    }


    public static void RightClick()
    {
        mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, new System.IntPtr());
        mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, new System.IntPtr());
    }


    public static void RightClick(Point p)
    {
        Mouse.Move(p);
        Mouse.RightClick();
    }


    public static void RightClick(int x, int y)
    {
        Mouse.RightClick(new Point(x, y));
    }


    public static void MiddleClick()
    {
        mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, new System.IntPtr());
        mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, new System.IntPtr());
    }


    public static void MiddleClick(Point p)
    {
        Mouse.Move(p);
        Mouse.MiddleClick();
    }


    public static void MiddleClick(int x, int y)
    {
        Mouse.MiddleClick(new Point(x, y));
    }
}



Most of the methods are self-explanatory (a.k.a. they do pretty much what the method names would suggest).  However, I give some examples anyway:

For each of these lines, assume that somePoint is some Point already defined, such as:
Point somePoint = new Point(13, 37);

Mouse.Move(somePoint);    // Moves the cursor to somePoint
Mouse.Move(123, 456);    // Moves the cursor to the pixel on your screen at (123, 456)
Mouse.Click();    // Left-click at current cursor position
Mouse.Click(somePoint);    // Left-click at somePoint
Mouse.Click(135, 246);    // Left-click at the pixel on your screen at (135, 246)
Mouse.RightClick();    // Right-click at current cursor position
Mouse.RightClick(somePoint);    // Right-click at somePoint
Mouse.RightClick(147, 258);    // Right-click at the pixel on your screen at (147, 258)
Mouse.MiddleClick();    // Middle-click at current cursor position
Mouse.MiddleClick(somePoint);    // Middle-click at somePoint
Mouse.MiddleClick(159, 260);    // Middle-click at the pixel on your screen at (159, 260)

After reading that, you're probably thinking, Why did I bother reading that?  It was so freakin' obvious!  Or maybe you were thinking, Why did that guy waste his time posting those examples?  Does he think I'm stupid or something?!


Anyway, you can download the C# class here (Mouse.cs).

Note: If you don't remember how to add this class to your C# project, see C# InputBox.

Saturday, December 4, 2010

Choosing a Mouse

The mouse is one of the main ways that people interact with a computer.  So, they should probably pick a mouse that they will be happy with, especially if they are heavy computer users, such as gamers.

First of all, ever wondered why it is called a mouse?  Look here, but you probably could already guess that the two buttons look like ears and the wire looks like a tail.

There are 4 main considerations in choosing the right mouse:

  1. Laser vs. Optical
    • The laser has better DPI than an optical mouse, meaning that it is much more sensitive to small motions.  Even though this sounds like a great thing, I think that both of these types are more sensitive than they actually need to be, resulting in me always turning down the sensitivity.
    • The laser can be used on any surface.  This is kind of important.  Optical mice cannot work on certain surfaces, like glossy ones, but laser mice can basically work on any surface, meaning you don't actually need a mousepad to use your mouse.
    • The optical mouse has a red light.  I don't really know if this is a plus or minus for the optical mouse.  Some people think the red light is cool, but the invisible laser can look "cleaner."
    • Laser mice cost more, but not by much anymore.  Even though laser mice are a tad more expensive than your standard optical mouse, I would say that it's worth the extra few dollars.
  2. Wired vs. Wireless
    • Wired mice do not require batteries!  It's really annoying to have to change out your batteries in your mouse as it's dying.  When your cursor begins to stop following your mouse, you have to stick in another battery.  If you have cheap batteries and an inefficient mouse, you could be switching out batteries a lot.
    • Wired mouse have wires, duh!  The wires can look sort of messy on your desk or get tangled up with other wires, but I recommend that you tie up your long wires with twisty-ties so that they are just long enough let your mouse be where you want it.  Also, put the wires behind your monitor/tower/laptop so that it doesn't get in the way and it looks a bit neater.
    • Wireless mice are more expensive, but not by much.  I think that wired mice are better, mostly because they don't have to get their batteries changed.  Also, some wireless receivers are so crappy that a long-wired mouse can actually reach farther!
  3. Extra buttons
    • Some people need those extra buttons on their mouse that they can configure, especially for gaming, such as a "reload" or "next weapon" key for first-person shooters.  I personally think that these keys aren't necessary, but if you're a hardcore gamer, maybe you should invest in a hardcore mouse so that you are on top of the competition.
  4. Durability
    • Some mice are really cheap, but break so frequently.  The good thing is that these cheap ones are easily replaced.  If you want to keep on buying cheap mice, then durability is not a problem, but if you have an expensive one, you'd want to make sure it lasts.
The verdict (my choice): Laser, wired, no extra buttons, cheap/not-very-durable.

Price range: $10-15.