Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Runtime.InteropServices;
  8. using System.Diagnostics;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11.  
  12. //namespace System.Windows.Forms;
  13.  
  14. public class Test {
  15.     delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
  16.     [DllImport("user32.dll")]
  17.     static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
  18.     [DllImport("user32.dll")]
  19.     static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
  20.     [DllImport("user32.dll")]
  21.     public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  22.     [DllImport("user32.dll")]
  23.     public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  24.     [DllImport("user32.dll")]
  25.     public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  26.     [DllImport("user32.DLL")]
  27.     public static extern bool SetForegroundWindow(IntPtr hWnd);
  28.     [DllImport("user32.dll")]
  29.     static extern IntPtr GetForegroundWindow();
  30.  
  31.  
  32.     const uint WM_KEYDOWN = 0x100;
  33.     const uint WM_KEYUP = 0x0101;
  34.     static IntPtr m_hWndEdit = IntPtr.Zero;
  35.  
  36.     public static void SendKeystroke(ushort k)
  37.     {
  38.        
  39.  
  40.         IntPtr hWnd = FindWindow("UnityWndClass", null);
  41.         //IntPtr hWndEdit = IntPtr.Zero;
  42.  
  43.         //EnumChildWindows(hWnd, EnumChildWindowsCallback, hWndEdit);
  44.  
  45.         if (hWnd != null)
  46.         {          
  47.            
  48.             //IntPtr curhWnd = GetForegroundWindow ();
  49.             SetForegroundWindow (hWnd);
  50.             SendMessage(hWnd, WM_KEYDOWN, ((IntPtr)k), (IntPtr)0);
  51.             SendMessage(hWnd, WM_KEYUP, ((IntPtr)k), (IntPtr)0);
  52.             //SetForegroundWindow (curhWnd);
  53.  
  54.         }
  55.     }
  56.  
  57.     static bool EnumChildWindowsCallback(IntPtr hWnd, IntPtr lParam)
  58.     {
  59.         // Search for notepads edit window - if we find it "false" is returned (means stop enumerating windows)
  60.  
  61.         StringBuilder sb = new StringBuilder();
  62.         GetClassName(hWnd, sb, 256);
  63.  
  64.         if (!sb.ToString().Contains("Edit"))
  65.         {
  66.             return true;
  67.         }
  68.  
  69.         m_hWndEdit = hWnd; // Store the handle to notepads edit window (this is the window we want to send the messages to)
  70.         return false;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement