Skip to content

Instantly share code, notes, and snippets.

@yalayabeeb
Last active December 28, 2015 06:48
Show Gist options
  • Save yalayabeeb/ac30b4ced3001dfa93cc to your computer and use it in GitHub Desktop.
Save yalayabeeb/ac30b4ced3001dfa93cc to your computer and use it in GitHub Desktop.
Changes the position of the cursor according to the speed that is set and the key that was pressed. (Mouse Keys)
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class Keyboard
{
#region WinAPI
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
#endregion
#region Delegates
private delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);
public delegate void KeyboardEventHandler(Keys keyPressed);
#endregion
#region Events
public static event KeyboardEventHandler OnKeyPress;
#endregion
private static IntPtr _hook = IntPtr.Zero;
public static void Hook()
{
if (_hook != IntPtr.Zero)
throw new Exception("It is already hooked!");
_hook = SetWindowsHookEx(13, new HookProc(HookProcCallBack), IntPtr.Zero, 0);
}
public static void UnHook()
{
if (UnhookWindowsHookEx(_hook))
_hook = IntPtr.Zero;
else
throw new Exception("Unable to be unhooked");
}
#region CallBacks
private static IntPtr HookProcCallBack(int code, IntPtr wParam, IntPtr lParam)
{
Keys keyPressed = (Keys)Marshal.ReadInt32(lParam);
OnKeyPress(keyPressed);
return CallNextHookEx(_hook, code, wParam, lParam);
}
#endregion
}
using System.Drawing;
using System.Windows.Forms;
public class MouseKeys
{
#region Properties
public int Speed { get; set; }
#endregion
public MouseKeys()
{
Speed = 5;
}
public MouseKeys(int speed)
{
Speed = speed;
}
public void HandleKey(Keys key)
{
Point mousePos = Cursor.Position;
switch (key)
{
case Keys.Up:
mousePos.Y -= Speed;
Cursor.Position = mousePos;
break;
case Keys.Down:
mousePos.Y += Speed;
Cursor.Position = mousePos;
break;
case Keys.Right:
mousePos.X += Speed;
Cursor.Position = mousePos;
break;
case Keys.Left:
mousePos.X -= Speed;
Cursor.Position = mousePos;
break;
}
}
}
using System;
using System.Windows.Forms;
class Program_Example
{
static MouseKeys _mouseKeys = new MouseKeys();
static void Main(string[] args)
{
Console.WriteLine("Set the speed of the mouse. If you want to use the default speed, type \"default\"");
string command = Console.ReadLine();
int speed = 0;
if (command != "default")
{
bool result = int.TryParse(command, out speed);
if (!result)
{
Console.WriteLine("Invalid command!");
return;
}
else
{
_mouseKeys.Speed = speed;
}
}
Keyboard.OnKeyPress += Keyboard_OnKeyPress;
Keyboard.Hook();
Console.WriteLine("Keyboard hooked!");
Application.Run();
}
private static void Keyboard_OnKeyPress(Keys keyPressed)
{
_mouseKeys.HandleKey(keyPressed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment