Advertisement
Guest User

Untitled

a guest
Apr 24th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5. using System.Windows;
  6. using System.Windows.Forms;
  7. using System.Windows.Input;
  8.  
  9.  
  10. namespace test
  11. {
  12.     public partial class MainWindow : Window
  13.     {
  14.         public MainWindow()
  15.         {
  16.             InitializeComponent();
  17.             ConsoleWindow.ShowConsoleWindow();
  18.             TestTextBox.GotFocus += Test;
  19.             NHotkey.Wpf.HotkeyManager.Current.AddOrReplace("Test", Key.G, ModifierKeys.Alt, Test);
  20.         }
  21.  
  22.         private static void Test(object sender, EventArgs e)
  23.         {
  24.             Console.WriteLine("test");
  25.             Thread.Sleep(150);
  26.             ModifiedKeyStroke(Key.A, ModifierKeys.Control);
  27.         }
  28.  
  29.         internal static class NativeMethods
  30.         {
  31.             [Flags]
  32.             public enum KeyboardFlags : uint
  33.             {
  34.                 None = 0,
  35.  
  36.                 /// <summary>
  37.                 /// KEYEVENTF_EXTENDEDKEY = 0x0001 (If specified, the scan code was preceded by a prefix byte that has the value 0xE0 (224).)
  38.                 /// </summary>
  39.                 ExtendedKey = 1,
  40.  
  41.                 /// <summary>
  42.                 /// KEYEVENTF_KEYUP = 0x0002 (If specified, the key is being released. If not specified, the key is being pressed.)
  43.                 /// </summary>
  44.                 KeyUp = 2,
  45.  
  46.                 /// <summary>
  47.                 /// KEYEVENTF_UNICODE = 0x0004 (If specified, wScan identifies the key and wVk is ignored.)
  48.                 /// </summary>
  49.                 Unicode = 4,
  50.  
  51.                 /// <summary>
  52.                 /// KEYEVENTF_SCANCODE = 0x0008 (Windows 2000/XP: If specified, the system synthesizes a VK_PACKET keystroke. The wVk parameter must be zero. This flag can only be combined with the KEYEVENTF_KEYUP flag. For more information, see the Remarks section.)
  53.                 /// </summary>
  54.                 ScanCode = 8,
  55.             }
  56.  
  57.             [Flags]
  58.             public enum MouseFlags : uint
  59.             {
  60.                 /// <summary>
  61.                 /// Specifies that movement occurred.
  62.                 /// </summary>
  63.                 Move = 0x0001,
  64.  
  65.                 /// <summary>
  66.                 /// Specifies that the left button was pressed.
  67.                 /// </summary>
  68.                 LeftDown = 0x0002,
  69.  
  70.                 /// <summary>
  71.                 /// Specifies that the left button was released.
  72.                 /// </summary>
  73.                 LeftUp = 0x0004,
  74.  
  75.                 /// <summary>
  76.                 /// Specifies that the right button was pressed.
  77.                 /// </summary>
  78.                 RightDown = 0x0008,
  79.  
  80.                 /// <summary>
  81.                 /// Specifies that the right button was released.
  82.                 /// </summary>
  83.                 RightUp = 0x0010,
  84.  
  85.                 /// <summary>
  86.                 /// Specifies that the middle button was pressed.
  87.                 /// </summary>
  88.                 MiddleDown = 0x0020,
  89.  
  90.                 /// <summary>
  91.                 /// Specifies that the middle button was released.
  92.                 /// </summary>
  93.                 MiddleUp = 0x0040,
  94.  
  95.                 /// <summary>
  96.                 /// Windows 2000/XP: Specifies that an X button was pressed.
  97.                 /// </summary>
  98.                 XDown = 0x0080,
  99.  
  100.                 /// <summary>
  101.                 /// Windows 2000/XP: Specifies that an X button was released.
  102.                 /// </summary>
  103.                 XUp = 0x0100,
  104.  
  105.                 /// <summary>
  106.                 /// Windows NT/2000/XP: Specifies that the wheel was moved, if the mouse has a wheel. The amount of movement is specified in mouseData.
  107.                 /// </summary>
  108.                 VerticalWheel = 0x0800,
  109.  
  110.                 /// <summary>
  111.                 /// Specifies that the wheel was moved horizontally, if the mouse has a wheel. The amount of movement is specified in mouseData. Windows 2000/XP:  Not supported.
  112.                 /// </summary>
  113.                 HorizontalWheel = 0x1000,
  114.  
  115.                 /// <summary>
  116.                 /// Windows 2000/XP: Maps coordinates to the entire desktop. Must be used with MOUSEEVENTF_ABSOLUTE.
  117.                 /// </summary>
  118.                 VirtualDesk = 0x4000,
  119.  
  120.                 /// <summary>
  121.                 /// Specifies that the dx and dy members contain normalized absolute coordinates. If the flag is not set, dxand dy contain relative data (the change in position since the last reported position). This flag can be set, or not set, regardless of what kind of mouse or other pointing device, if any, is connected to the system. For further information about relative mouse motion, see the following Remarks section.
  122.                 /// </summary>
  123.                 Absolute = 0x8000,
  124.             }
  125.  
  126.             [StructLayout(LayoutKind.Sequential)]
  127.             public struct KEYBDINPUT
  128.             {
  129.                 public ushort virtualKey;
  130.                 public ushort scanCode;
  131.                 public KeyboardFlags flags;
  132.                 public uint timeStamp;
  133.                 public IntPtr extraInfo;
  134.             }
  135.  
  136.             [StructLayout(LayoutKind.Sequential)]
  137.             public struct MOUSEINPUT
  138.             {
  139.                 public int deltaX;
  140.                 public int deltaY;
  141.                 public uint mouseData;
  142.                 public MouseFlags flags;
  143.                 public uint time;
  144.                 public IntPtr extraInfo;
  145.             }
  146.  
  147.             [StructLayout(LayoutKind.Sequential)]
  148.             public struct HARDWAREINPUT
  149.             {
  150.                 public uint message;
  151.                 public ushort wParamL;
  152.                 public ushort wParamH;
  153.             }
  154.  
  155.             [StructLayout(LayoutKind.Explicit)]
  156.             public struct InputUnion
  157.             {
  158.                 [FieldOffset(0)]
  159.                 public MOUSEINPUT mouse;
  160.                 [FieldOffset(0)]
  161.                 public KEYBDINPUT keyboard;
  162.                 [FieldOffset(0)]
  163.                 public HARDWAREINPUT hardware;
  164.             }
  165.             public enum InputType : int
  166.             {
  167.                 Mouse = 0,
  168.                 Keyboard = 1,
  169.                 Hardware = 2
  170.             }
  171.             public struct INPUT
  172.             {
  173.                 public InputType type;
  174.                 public InputUnion union;
  175.             }
  176.  
  177.             public static int SizeOfInput { get; } = Marshal.SizeOf(typeof(INPUT));
  178.  
  179.             [DllImport("user32.dll")]
  180.             public static extern uint SendInput(int nInputs, INPUT[] pInputs, int cbSize);
  181.         }
  182.  
  183.         public static void ModifiedKeyStroke(Key key, ModifierKeys modifiers = ModifierKeys.None)
  184.         {
  185.             static NativeMethods.INPUT BuildINPUT(Key k, NativeMethods.KeyboardFlags flags) => new NativeMethods.INPUT
  186.             {
  187.                 type = NativeMethods.InputType.Keyboard,
  188.                 union = new NativeMethods.InputUnion { keyboard = new NativeMethods.KEYBDINPUT { virtualKey = (ushort)KeyInterop.VirtualKeyFromKey(k), scanCode = 0, flags = flags, timeStamp = 0, extraInfo = IntPtr.Zero } }
  189.             };
  190.             List<Key> keys = new List<Key>();
  191.             if (modifiers.HasFlag(ModifierKeys.Control)) keys.Add(Key.LeftCtrl);
  192.             if (modifiers.HasFlag(ModifierKeys.Alt)) keys.Add(Key.LeftAlt);
  193.             if (modifiers.HasFlag(ModifierKeys.Shift)) keys.Add(Key.LeftShift);
  194.             if (modifiers.HasFlag(ModifierKeys.Windows)) keys.Add(Key.LWin);
  195.             keys.Add(key);
  196.             NativeMethods.INPUT[] inputs = new NativeMethods.INPUT[keys.Count * 2];
  197.             for (int i = 0; i < keys.Count; i++)
  198.             {
  199.                 inputs[i] = BuildINPUT(keys[i], NativeMethods.KeyboardFlags.None);
  200.                 inputs[^(i + 1)] = BuildINPUT(keys[i], NativeMethods.KeyboardFlags.KeyUp);
  201.             }
  202.             _ = NativeMethods.SendInput(inputs.Length, inputs, NativeMethods.SizeOfInput);
  203.         }
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement