Advertisement
Guest User

Makro (Up/Down 5 with Ctrl+Shift+Up/Down)

a guest
Jan 13th, 2017
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.20 KB | None | 0 0
  1. MainForm.cs:
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Runtime.InteropServices;
  12. using System.Diagnostics;
  13. using System.Timers;
  14. using System.Threading;
  15. using System.Windows.Input;
  16.  
  17. namespace Makro
  18. {
  19.     public partial class MainForm : Form
  20.     {
  21.         private SortedSet<Keys> keysPressed = new SortedSet<Keys>();
  22.  
  23.         struct INPUT
  24.         {
  25.             public UInt32 type;
  26.             public ushort wVk;
  27.             public ushort wScan;
  28.             public UInt32 dwFlags;
  29.             public UInt32 time;
  30.             public UIntPtr dwExtraInfo;
  31.             public UInt32 uMsg;
  32.             public ushort wParamL;
  33.             public ushort wParamH;
  34.         }
  35.  
  36.         enum SendInputFlags
  37.         {
  38.             KEYEVENTF_EXTENDEDKEY = 0x0001,
  39.             KEYEVENTF_KEYUP = 0x0002,
  40.             KEYEVENTF_UNICODE = 0x0004,
  41.             KEYEVENTF_SCANCODE = 0x0008,
  42.         }
  43.  
  44.         [DllImport("user32.dll")]
  45.         static extern UInt32 SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, Int32 cbSize);
  46.  
  47.         [DllImport("user32.dll")]
  48.         static extern UInt32 MapVirtualKey(UInt32 uCode, UInt32 uMapType);
  49.  
  50.         /*static Task SimulateKeyHold(VirtualKeyCode key, int holdDurationMs, int repeatDelayMs, int repeatRateMs, CancellationToken token)
  51.         {
  52.             var tcs = new TaskCompletionSource<object>();
  53.             var ctr = new CancellationTokenRegistration();
  54.             var startCount = Environment.TickCount;
  55.             System.Threading.Timer timer = null;
  56.             timer = new System.Threading.Timer(s =>
  57.             {
  58.                 lock (timer)
  59.                 {
  60.                     if (Environment.TickCount - startCount <= holdDurationMs)
  61.                         InputSimulator.SimulateKeyDown(key);
  62.                     else if (startCount != -1)
  63.                     {
  64.                         startCount = -1;
  65.                         timer.Dispose();
  66.                         ctr.Dispose();
  67.                         InputSimulator.SimulateKeyUp(key);
  68.  
  69.                         tcs.TrySetResult(null);
  70.                     }
  71.                 }
  72.             });
  73.             timer.Change(repeatDelayMs, repeatRateMs);
  74.  
  75.             if (token.CanBeCanceled)
  76.                 ctr = token.Register(() =>
  77.                 {
  78.                     timer.Dispose();
  79.                     tcs.TrySetCanceled();
  80.                 });
  81.             return tcs.Task;
  82.         }*/
  83.  
  84.  
  85.         public MainForm()
  86.         {
  87.             InitializeComponent();
  88.  
  89.             WindowState = FormWindowState.Minimized;
  90.             ShowInTaskbar = false;
  91.             Visible = false;
  92.         }
  93.  
  94.         private void MainForm_Load(object sender, EventArgs e)
  95.         {
  96.             Hide();
  97.  
  98.             Makro.InterceptKeys.OnKeyDown += InterceptKeys_OnKeyDown;
  99.             Makro.InterceptKeys.OnKeyUp += InterceptKeys_OnKeyUp;
  100.             Makro.InterceptKeys.Start();
  101.         }
  102.  
  103.         private void InterceptKeys_OnKeyDown(object sender, KeyEventArgs e)
  104.         {
  105.             keysPressed.Add(e.KeyCode);
  106.  
  107.             //Control + Shift
  108.             if (keysPressed.Contains(Keys.LControlKey) && keysPressed.Contains(Keys.LShiftKey))
  109.             {
  110.                 if (keysPressed.Contains(Keys.Down))
  111.                 {
  112.                     INPUT[] InputDataCtrlShift = new INPUT[2];
  113.                     InputDataCtrlShift[0].type = 1; //INPUT_KEYBOARD
  114.                     InputDataCtrlShift[0].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
  115.                     InputDataCtrlShift[0].wVk = (ushort)Keys.ControlKey;
  116.                     InputDataCtrlShift[1].type = 1; //INPUT_KEYBOARD
  117.                     InputDataCtrlShift[1].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
  118.                     InputDataCtrlShift[1].wVk = (ushort)Keys.ShiftKey;
  119.                     SendInput(2, InputDataCtrlShift, Marshal.SizeOf(InputDataCtrlShift[0]));
  120.  
  121.  
  122.                     for (int i = 0; i < 5; ++i)
  123.                     {
  124.                         INPUT[] InputData = new INPUT[2];
  125.  
  126.                         InputData[0].type = 1; //INPUT_KEYBOARD
  127.                         InputData[0].wVk = (ushort)Keys.Down;
  128.                         InputData[1].type = 1; //INPUT_KEYBOARD
  129.                         InputData[1].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
  130.                         InputData[1].wVk = (ushort)Keys.Down;
  131.                         SendInput(2, InputData, Marshal.SizeOf(InputData[0]));
  132.                     }
  133.  
  134.  
  135.                     InputDataCtrlShift[0].dwFlags = 0;
  136.                     InputDataCtrlShift[1].dwFlags = 0;
  137.                     SendInput(2, InputDataCtrlShift, Marshal.SizeOf(InputDataCtrlShift[0]));
  138.                 }
  139.                 else if (keysPressed.Contains(Keys.Up))
  140.                 {
  141.                     INPUT[] InputDataCtrlShift = new INPUT[2];
  142.                     InputDataCtrlShift[0].type = 1; //INPUT_KEYBOARD
  143.                     InputDataCtrlShift[0].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
  144.                     InputDataCtrlShift[0].wVk = (ushort)Keys.ControlKey;
  145.                     InputDataCtrlShift[1].type = 1; //INPUT_KEYBOARD
  146.                     InputDataCtrlShift[1].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
  147.                     InputDataCtrlShift[1].wVk = (ushort)Keys.ShiftKey;
  148.                     SendInput(2, InputDataCtrlShift, Marshal.SizeOf(InputDataCtrlShift[0]));
  149.  
  150.  
  151.                     for (int i = 0; i < 5; ++i)
  152.                     {
  153.                         INPUT[] InputData = new INPUT[2];
  154.  
  155.                         InputData[0].type = 1; //INPUT_KEYBOARD
  156.                         InputData[0].wVk = (ushort)Keys.Up;
  157.                         InputData[1].type = 1; //INPUT_KEYBOARD
  158.                         InputData[1].dwFlags = (uint)(SendInputFlags.KEYEVENTF_KEYUP);
  159.                         InputData[1].wVk = (ushort)Keys.Up;
  160.                         SendInput(2, InputData, Marshal.SizeOf(InputData[0]));
  161.                     }
  162.  
  163.                    
  164.                     InputDataCtrlShift[0].dwFlags = 0;
  165.                     InputDataCtrlShift[1].dwFlags = 0;
  166.                     SendInput(2, InputDataCtrlShift, Marshal.SizeOf(InputDataCtrlShift[0]));
  167.                 }
  168.             }
  169.         }
  170.  
  171.         private void InterceptKeys_OnKeyUp(object sender, KeyEventArgs e)
  172.         {
  173.             keysPressed.Remove(e.KeyCode);
  174.         }
  175.  
  176.         private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
  177.         {
  178.             if (e.Button == MouseButtons.Right)
  179.             {
  180.                 Close();
  181.             }
  182.         }
  183.        
  184.         private void MainForm_Leave(object sender, EventArgs e)
  185.         {
  186.             notifyIcon.Visible = false;
  187.         }
  188.     }
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195. InterceptKeys.cs (class):
  196. using System;
  197. using System.Collections.Generic;
  198. using System.Linq;
  199. using System.Text;
  200. using System.Threading.Tasks;
  201. using System.Diagnostics;
  202. using System.Runtime.InteropServices;
  203. using System.Windows.Forms;
  204.  
  205. namespace Makro
  206. {
  207.     class InterceptKeys
  208.     {
  209.         private const int WH_KEYBOARD_LL = 13;
  210.         private const int WM_KEYDOWN = 0x0100;
  211.         private const int WM_SYSKEYDOWN = 0x0104;
  212.         private const int WM_KEYUP = 0x0101;
  213.         private const int WM_SYSKEYUP = 0x0105;
  214.         private static LowLevelKeyboardProc _proc = HookCallback;
  215.         private static IntPtr _hookID = IntPtr.Zero;
  216.  
  217.         public static void Start()
  218.         {
  219.             _hookID = SetHook(_proc);
  220.         }
  221.  
  222.         public static void Stop()
  223.         {
  224.             UnhookWindowsHookEx(_hookID);
  225.         }
  226.  
  227.         public static event KeyEventHandler OnKeyDown;
  228.         public static event KeyEventHandler OnKeyUp;
  229.  
  230.         private static IntPtr SetHook(LowLevelKeyboardProc proc)
  231.         {
  232.             using (Process curProcess = Process.GetCurrentProcess())
  233.             using (ProcessModule curModule = curProcess.MainModule)
  234.             {
  235.                 return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
  236.             }
  237.         }
  238.  
  239.         private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
  240.  
  241.         private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  242.         {
  243.             if (nCode >= 0)
  244.             {
  245.                 if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
  246.                 {
  247.                     var vkCode = (Keys)Marshal.ReadInt32(lParam);
  248.                     OnKeyDown(null, new KeyEventArgs(vkCode));
  249.                 }
  250.                 else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
  251.                 {
  252.                     var vkCode = (Keys)Marshal.ReadInt32(lParam);
  253.                     OnKeyUp(null, new KeyEventArgs(vkCode));
  254.                 }
  255.             }
  256.  
  257.             return CallNextHookEx(_hookID, nCode, wParam, lParam);
  258.         }
  259.  
  260.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  261.         private static extern IntPtr SetWindowsHookEx(int idHook,
  262.             LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  263.  
  264.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  265.         [return: MarshalAs(UnmanagedType.Bool)]
  266.         private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  267.  
  268.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  269.         private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
  270.             IntPtr wParam, IntPtr lParam);
  271.  
  272.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  273.         private static extern IntPtr GetModuleHandle(string lpModuleName);
  274.     }
  275. }
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283. App.config:
  284. <?xml version="1.0" encoding="utf-8"?>
  285. <configuration>
  286.   <startup>
  287.     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  288.   </startup>
  289.   <appSettings>
  290.     <add key="SendKeys" value="SendInput" />
  291.     <add key="ClientSettingsProvider.ServiceUri" value="" />
  292.   </appSettings>
  293.   <system.web>
  294.     <membership defaultProvider="ClientAuthenticationMembershipProvider">
  295.       <providers>
  296.         <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
  297.       </providers>
  298.     </membership>
  299.     <roleManager defaultProvider="ClientRoleProvider" enabled="true">
  300.       <providers>
  301.         <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
  302.       </providers>
  303.     </roleManager>
  304.   </system.web>
  305. </configuration>
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314. MainForm.Designer.cs:
  315. namespace Makro
  316. {
  317.     partial class MainForm
  318.     {
  319.         /// <summary>
  320.         /// Required designer variable.
  321.         /// </summary>
  322.         private System.ComponentModel.IContainer components = null;
  323.  
  324.         /// <summary>
  325.         /// Clean up any resources being used.
  326.         /// </summary>
  327.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  328.         protected override void Dispose(bool disposing)
  329.         {
  330.             if (disposing && (components != null))
  331.             {
  332.                 components.Dispose();
  333.             }
  334.             base.Dispose(disposing);
  335.         }
  336.  
  337.         #region Windows Form Designer generated code
  338.  
  339.         /// <summary>
  340.         /// Required method for Designer support - do not modify
  341.         /// the contents of this method with the code editor.
  342.         /// </summary>
  343.         private void InitializeComponent()
  344.         {
  345.             this.components = new System.ComponentModel.Container();
  346.             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
  347.             this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
  348.             this.SuspendLayout();
  349.             //
  350.             // notifyIcon
  351.             //
  352.             this.notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon.Icon")));
  353.             this.notifyIcon.Text = "notifyIcon";
  354.             this.notifyIcon.Visible = true;
  355.             this.notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon_MouseClick);
  356.             //
  357.             // MainForm
  358.             //
  359.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  360.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  361.             this.ClientSize = new System.Drawing.Size(184, 0);
  362.             this.Name = "MainForm";
  363.             this.ShowInTaskbar = false;
  364.             this.Text = "Makro";
  365.             this.Load += new System.EventHandler(this.MainForm_Load);
  366.             this.Leave += new System.EventHandler(this.MainForm_Leave);
  367.             this.ResumeLayout(false);
  368.  
  369.         }
  370.  
  371.         #endregion
  372.  
  373.         private System.Windows.Forms.NotifyIcon notifyIcon;
  374.     }
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement