Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainForm.cs:
- 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;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using System.Timers;
- using System.Threading;
- using System.Windows.Input;
- namespace Makro
- {
- public partial class MainForm : Form
- {
- private SortedSet<Keys> keysPressed = new SortedSet<Keys>();
- struct INPUT
- {
- public UInt32 type;
- public ushort wVk;
- public ushort wScan;
- public UInt32 dwFlags;
- public UInt32 time;
- public UIntPtr dwExtraInfo;
- public UInt32 uMsg;
- public ushort wParamL;
- public ushort wParamH;
- }
- enum SendInputFlags
- {
- KEYEVENTF_EXTENDEDKEY = 0x0001,
- KEYEVENTF_KEYUP = 0x0002,
- KEYEVENTF_UNICODE = 0x0004,
- KEYEVENTF_SCANCODE = 0x0008,
- }
- [DllImport("user32.dll")]
- static extern UInt32 SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, Int32 cbSize);
- [DllImport("user32.dll")]
- static extern UInt32 MapVirtualKey(UInt32 uCode, UInt32 uMapType);
- /*static Task SimulateKeyHold(VirtualKeyCode key, int holdDurationMs, int repeatDelayMs, int repeatRateMs, CancellationToken token)
- {
- var tcs = new TaskCompletionSource<object>();
- var ctr = new CancellationTokenRegistration();
- var startCount = Environment.TickCount;
- System.Threading.Timer timer = null;
- timer = new System.Threading.Timer(s =>
- {
- lock (timer)
- {
- if (Environment.TickCount - startCount <= holdDurationMs)
- InputSimulator.SimulateKeyDown(key);
- else if (startCount != -1)
- {
- startCount = -1;
- timer.Dispose();
- ctr.Dispose();
- InputSimulator.SimulateKeyUp(key);
- tcs.TrySetResult(null);
- }
- }
- });
- timer.Change(repeatDelayMs, repeatRateMs);
- if (token.CanBeCanceled)
- ctr = token.Register(() =>
- {
- timer.Dispose();
- tcs.TrySetCanceled();
- });
- return tcs.Task;
- }*/
- public MainForm()
- {
- InitializeComponent();
- WindowState = FormWindowState.Minimized;
- ShowInTaskbar = false;
- Visible = false;
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- Hide();
- Makro.InterceptKeys.OnKeyDown += InterceptKeys_OnKeyDown;
- Makro.InterceptKeys.OnKeyUp += InterceptKeys_OnKeyUp;
- Makro.InterceptKeys.Start();
- }
- private void InterceptKeys_OnKeyDown(object sender, KeyEventArgs e)
- {
- keysPressed.Add(e.KeyCode);
- //Control + Shift
- if (keysPressed.Contains(Keys.LControlKey) && keysPressed.Contains(Keys.LShiftKey))
- {
- if (keysPressed.Contains(Keys.Down))
- {
- INPUT[] InputDataCtrlShift = new INPUT[2];
- InputDataCtrlShift[0].type = 1; //INPUT_KEYBOARD
- InputDataCtrlShift[0].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
- InputDataCtrlShift[0].wVk = (ushort)Keys.ControlKey;
- InputDataCtrlShift[1].type = 1; //INPUT_KEYBOARD
- InputDataCtrlShift[1].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
- InputDataCtrlShift[1].wVk = (ushort)Keys.ShiftKey;
- SendInput(2, InputDataCtrlShift, Marshal.SizeOf(InputDataCtrlShift[0]));
- for (int i = 0; i < 5; ++i)
- {
- INPUT[] InputData = new INPUT[2];
- InputData[0].type = 1; //INPUT_KEYBOARD
- InputData[0].wVk = (ushort)Keys.Down;
- InputData[1].type = 1; //INPUT_KEYBOARD
- InputData[1].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
- InputData[1].wVk = (ushort)Keys.Down;
- SendInput(2, InputData, Marshal.SizeOf(InputData[0]));
- }
- InputDataCtrlShift[0].dwFlags = 0;
- InputDataCtrlShift[1].dwFlags = 0;
- SendInput(2, InputDataCtrlShift, Marshal.SizeOf(InputDataCtrlShift[0]));
- }
- else if (keysPressed.Contains(Keys.Up))
- {
- INPUT[] InputDataCtrlShift = new INPUT[2];
- InputDataCtrlShift[0].type = 1; //INPUT_KEYBOARD
- InputDataCtrlShift[0].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
- InputDataCtrlShift[0].wVk = (ushort)Keys.ControlKey;
- InputDataCtrlShift[1].type = 1; //INPUT_KEYBOARD
- InputDataCtrlShift[1].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
- InputDataCtrlShift[1].wVk = (ushort)Keys.ShiftKey;
- SendInput(2, InputDataCtrlShift, Marshal.SizeOf(InputDataCtrlShift[0]));
- for (int i = 0; i < 5; ++i)
- {
- INPUT[] InputData = new INPUT[2];
- InputData[0].type = 1; //INPUT_KEYBOARD
- InputData[0].wVk = (ushort)Keys.Up;
- InputData[1].type = 1; //INPUT_KEYBOARD
- InputData[1].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
- InputData[1].wVk = (ushort)Keys.Up;
- SendInput(2, InputData, Marshal.SizeOf(InputData[0]));
- }
- InputDataCtrlShift[0].dwFlags = 0;
- InputDataCtrlShift[1].dwFlags = 0;
- SendInput(2, InputDataCtrlShift, Marshal.SizeOf(InputDataCtrlShift[0]));
- }
- }
- }
- private void InterceptKeys_OnKeyUp(object sender, KeyEventArgs e)
- {
- keysPressed.Remove(e.KeyCode);
- }
- private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Right)
- {
- Close();
- }
- }
- private void MainForm_Leave(object sender, EventArgs e)
- {
- notifyIcon.Visible = false;
- }
- }
- }
- InterceptKeys.cs (class):
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace Makro
- {
- class InterceptKeys
- {
- private const int WH_KEYBOARD_LL = 13;
- private const int WM_KEYDOWN = 0x0100;
- private const int WM_SYSKEYDOWN = 0x0104;
- private const int WM_KEYUP = 0x0101;
- private const int WM_SYSKEYUP = 0x0105;
- private static LowLevelKeyboardProc _proc = HookCallback;
- private static IntPtr _hookID = IntPtr.Zero;
- public static void Start()
- {
- _hookID = SetHook(_proc);
- }
- public static void Stop()
- {
- UnhookWindowsHookEx(_hookID);
- }
- public static event KeyEventHandler OnKeyDown;
- public static event KeyEventHandler OnKeyUp;
- private static IntPtr SetHook(LowLevelKeyboardProc proc)
- {
- using (Process curProcess = Process.GetCurrentProcess())
- using (ProcessModule curModule = curProcess.MainModule)
- {
- return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
- }
- }
- private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
- private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
- {
- if (nCode >= 0)
- {
- if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
- {
- var vkCode = (Keys)Marshal.ReadInt32(lParam);
- OnKeyDown(null, new KeyEventArgs(vkCode));
- }
- else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
- {
- var vkCode = (Keys)Marshal.ReadInt32(lParam);
- OnKeyUp(null, new KeyEventArgs(vkCode));
- }
- }
- return CallNextHookEx(_hookID, nCode, wParam, lParam);
- }
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr SetWindowsHookEx(int idHook,
- LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool UnhookWindowsHookEx(IntPtr hhk);
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
- IntPtr wParam, IntPtr lParam);
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr GetModuleHandle(string lpModuleName);
- }
- }
- App.config:
- <?xml version="1.0" encoding="utf-8"?>
- <configuration>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
- </startup>
- <appSettings>
- <add key="SendKeys" value="SendInput" />
- <add key="ClientSettingsProvider.ServiceUri" value="" />
- </appSettings>
- <system.web>
- <membership defaultProvider="ClientAuthenticationMembershipProvider">
- <providers>
- <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
- </providers>
- </membership>
- <roleManager defaultProvider="ClientRoleProvider" enabled="true">
- <providers>
- <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
- </providers>
- </roleManager>
- </system.web>
- </configuration>
- MainForm.Designer.cs:
- namespace Makro
- {
- partial class MainForm
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
- this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
- this.SuspendLayout();
- //
- // notifyIcon
- //
- this.notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon.Icon")));
- this.notifyIcon.Text = "notifyIcon";
- this.notifyIcon.Visible = true;
- this.notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon_MouseClick);
- //
- // MainForm
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(184, 0);
- this.Name = "MainForm";
- this.ShowInTaskbar = false;
- this.Text = "Makro";
- this.Load += new System.EventHandler(this.MainForm_Load);
- this.Leave += new System.EventHandler(this.MainForm_Leave);
- this.ResumeLayout(false);
- }
- #endregion
- private System.Windows.Forms.NotifyIcon notifyIcon;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement