Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using System.Windows.Forms;
- using System.Threading;
- //namespace System.Windows.Forms;
- public class Test {
- delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
- [DllImport("user32.dll")]
- static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
- [DllImport("user32.dll")]
- static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
- [DllImport("user32.dll")]
- public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("user32.dll")]
- public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
- [DllImport("user32.dll")]
- public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
- [DllImport("user32.DLL")]
- public static extern bool SetForegroundWindow(IntPtr hWnd);
- [DllImport("user32.dll")]
- static extern IntPtr GetForegroundWindow();
- const uint WM_KEYDOWN = 0x100;
- const uint WM_KEYUP = 0x0101;
- static IntPtr m_hWndEdit = IntPtr.Zero;
- public static void SendKeystroke(ushort k)
- {
- IntPtr hWnd = FindWindow("UnityWndClass", null);
- //IntPtr hWndEdit = IntPtr.Zero;
- //EnumChildWindows(hWnd, EnumChildWindowsCallback, hWndEdit);
- if (hWnd != null)
- {
- //IntPtr curhWnd = GetForegroundWindow ();
- SetForegroundWindow (hWnd);
- SendMessage(hWnd, WM_KEYDOWN, ((IntPtr)k), (IntPtr)0);
- SendMessage(hWnd, WM_KEYUP, ((IntPtr)k), (IntPtr)0);
- //SetForegroundWindow (curhWnd);
- }
- }
- static bool EnumChildWindowsCallback(IntPtr hWnd, IntPtr lParam)
- {
- // Search for notepads edit window - if we find it "false" is returned (means stop enumerating windows)
- StringBuilder sb = new StringBuilder();
- GetClassName(hWnd, sb, 256);
- if (!sb.ToString().Contains("Edit"))
- {
- return true;
- }
- m_hWndEdit = hWnd; // Store the handle to notepads edit window (this is the window we want to send the messages to)
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement