Advertisement
Guest User

Untitled

a guest
Jul 6th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace clipboardtest
  6. {
  7.     class Program
  8.     {
  9.         [DllImport("User32.dll")]
  10.         public static extern Int32 SetForegroundWindow(IntPtr hWnd);
  11.  
  12.         [DllImport("user32.dll")]
  13.         public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             IntPtr WindowToFind = FindWindow(null, "New folder");
  18.  
  19.             SetForegroundWindow(WindowToFind);
  20.  
  21.             Keyboard.SimulateKeyStroke('v', ctrl: true);
  22.         }
  23.     }
  24.  
  25.     static class Keyboard
  26.     {
  27.         public static void SimulateKeyStroke(char key, bool ctrl = false, bool alt = false, bool shift = false)
  28.         {
  29.             List<ushort> keys = new List<ushort>();
  30.  
  31.             if (ctrl)
  32.                 keys.Add(VK_CONTROL);
  33.  
  34.             if (alt)
  35.                 keys.Add(VK_MENU);
  36.  
  37.             if (shift)
  38.                 keys.Add(VK_SHIFT);
  39.  
  40.             keys.Add(char.ToUpper(key));
  41.  
  42.             INPUT input = new INPUT();
  43.             input.type = INPUT_KEYBOARD;
  44.             int inputSize = Marshal.SizeOf(input);
  45.  
  46.             for (int i = 0; i < keys.Count; ++i)
  47.             {
  48.                 input.mkhi.ki.wVk = keys[i];
  49.  
  50.                 bool isKeyDown = (GetAsyncKeyState(keys[i]) & 0x10000) != 0;
  51.  
  52.                 if (!isKeyDown)
  53.                     SendInput(1, ref input, inputSize);
  54.             }
  55.  
  56.             input.mkhi.ki.dwFlags = KEYEVENTF_KEYUP;
  57.             for (int i = keys.Count - 1; i >= 0; --i)
  58.             {
  59.                 input.mkhi.ki.wVk = keys[i];
  60.                 SendInput(1, ref input, inputSize);
  61.             }
  62.         }
  63.  
  64.         [DllImport("user32.dll")]
  65.         static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
  66.  
  67.         [DllImport("user32.dll")]
  68.         static extern short GetAsyncKeyState(ushort vKey);
  69.  
  70.         struct MOUSEINPUT
  71.         {
  72.             public int dx;
  73.             public int dy;
  74.             public uint mouseData;
  75.             public uint dwFlags;
  76.             public uint time;
  77.             public IntPtr dwExtraInfo;
  78.         }
  79.  
  80.         struct KEYBDINPUT
  81.         {
  82.             public ushort wVk;
  83.             public ushort wScan;
  84.             public uint dwFlags;
  85.             public uint time;
  86.             public IntPtr dwExtraInfo;
  87.         }
  88.  
  89.         struct HARDWAREINPUT
  90.         {
  91.             public int uMsg;
  92.             public short wParamL;
  93.             public short wParamH;
  94.         }
  95.  
  96.         [StructLayout(LayoutKind.Explicit)]
  97.         struct MOUSEKEYBDHARDWAREINPUT
  98.         {
  99.             [FieldOffset(0)]
  100.             public MOUSEINPUT mi;
  101.  
  102.             [FieldOffset(0)]
  103.             public KEYBDINPUT ki;
  104.  
  105.             [FieldOffset(0)]
  106.             public HARDWAREINPUT hi;
  107.         }
  108.  
  109.         struct INPUT
  110.         {
  111.             public int type;
  112.             public MOUSEKEYBDHARDWAREINPUT mkhi;
  113.         }
  114.  
  115.         const int INPUT_KEYBOARD = 1;
  116.         const uint KEYEVENTF_KEYUP = 0x0002;
  117.  
  118.         const ushort VK_SHIFT = 0x10;
  119.         const ushort VK_CONTROL = 0x11;
  120.         const ushort VK_MENU = 0x12;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement