public static IntPtr WindowsKeyHookCallback(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { const int WM_HOTKEY = 0x0312; switch (msg) { case WM_HOTKEY: switch (wParam.ToInt32()) { case KeyHookConstants.HOTKEY_ID: { if (View.IsVisible) { View.Hide(); break; } else { (View.DataContext as QuickTypeViewModel).LastFocusedWindow = WinApiProxy.GetForegroundWindow(); View.Show(); WinApiProxy.SetForegroundWindow(View.Handle); break; } } } break; } return(IntPtr.Zero); }
public static void WindowsWindowCreatedHook(IntPtr handle) { var windowName = WinApiProxy.GetWindowText(handle); if (string.IsNullOrEmpty(windowName)) { return; } Debug.WriteLine($"Window Created = {windowName}"); var names = QuickTypeCommandManager.Instance.AutoKillWindowNames(); if (names.Any(x => x.Item1 == windowName)) { var command = names.First(x => x.Item1 == windowName); var keys = command.Item2; if (!string.IsNullOrEmpty(keys)) { WinApiProxy.SetForegroundWindow(handle); SendKeys.SendWait(keys); } else { WinApiProxy.CloseWindow(handle); } } }
public static List <QueryResult> KeyboardShortcutsProvider(IntPtr window, List <SingleCommand> cmd) { return(cmd.Select(x => new QueryResult() { Data = () => { WinApiProxy.SetForegroundWindow(window); SendKeys.SendWait(x.Keys); }, Name = x.Name }).OrderBy(x => x.Name).ToList()); }
public static List <QueryResult> ProcessShortcutsCreator(List <SingleCommand> commands, string filter) { var cmd = commands; if (!string.IsNullOrEmpty(filter)) { cmd = cmd.Where(x => x.WindowContext == filter).ToList(); } return(cmd.Select(x => new QueryResult() { Data = () => { try { var expanded = (Environment.ExpandEnvironmentVariables(x.Parameter)); if (!string.IsNullOrEmpty(x.Additional)) { var args = QuickTypeEvnVariables.Resolve(x.Additional, QuickTypeCommandManager.Instance.WindowFocus, x.VisualStudioContext); var process = Process.Start($"{expanded}", $"{args}"); if (process != null) { WinApiProxy.SetForegroundWindow(process.Handle); } } else { var process = Process.Start($"{expanded}"); if (process != null) { WinApiProxy.SetForegroundWindow(process.Handle); } } } catch (Exception ex) { MessageBox.Show($"{ex.Message}\n{ex?.InnerException?.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }, Name = x.Name }).OrderBy(x => x.Name).ToList()); }
public static void SubscribeToWindowEvents() { if (windowEventHook == IntPtr.Zero) { windowEventHook = WinApiProxy.SetWinEventHook( EVENT_SYSTEM_FOREGROUND, // eventMin EVENT_SYSTEM_FOREGROUND, // eventMax IntPtr.Zero, // hmodWinEventProc _proc, // lpfnWinEventProc 0, // idProcess 0, // idThread WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS); if (windowEventHook == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } }
public List <QueryResult> GetContextCommands() { WinApiProxy.GetWindowThreadProcessId(WindowFocus, out var procId); var text = Process.GetProcesses().FirstOrDefault(x => x.Id == procId)?.ProcessName; #if DEBUG if (!string.IsNullOrEmpty(text)) { Debug.WriteLine($"Process = {text}"); } #endif if (string.IsNullOrEmpty(text)) { if (WinApiProxy.IsExplorer(WindowFocus)) { text = AppConstants.EXPLORER_WINDOW; } else { var focusedWin = WinApiProxy.GetParent(WindowFocus); if (focusedWin != IntPtr.Zero) { WindowFocus = focusedWin; } text = Process.GetProcesses().Where(x => x.MainWindowHandle == WindowFocus).Select(x => x.ProcessName).FirstOrDefault(); } if (string.IsNullOrEmpty(text)) { return(new List <QueryResult>()); } } var cache = CommandFactory.CommandCache.RegisteredCommands.Where(x => x.Type != CommandType.Compound).ToList(); var vsCommands = cache.Where(x => !string.IsNullOrEmpty(x.VisualStudioContext)).ToList(); var commands = cache.Where(x => x.WindowContext.StartsWith(text)).ToList(); var otherctxCommands = CommandFactory.ProcessShortcutsCreator(vsCommands, text); var queryResult = CommandFactory.KeyboardShortcutsProvider(WindowFocus, commands); queryResult = queryResult.Where(x => otherctxCommands.All(y => y.Name != x.Name)).ToList(); queryResult.AddRange(otherctxCommands); queryResult = queryResult.OrderBy(x => x.Name).ToList(); ProcessCompound(CommandFactory.CommandCache.RegisteredCommands.Where(x => x.Type == CommandType.Compound).ToList(), queryResult); return(queryResult); }
/// <summary> /// Get a COM handle to DTE of a running VS instance /// </summary> /// <param name="vsVersion">Version of Visual Studio (15.0 for 2017)</param> /// <param name="processId">Process ID of the running version</param> /// <returns></returns> public static DTE GetDTE(string vsVersion, int processId) { //var vsRunningInstance = BoundInstances.FirstOrDefault(x => x.ProcessId == processId && x.Version == vsVersion); //if (vsRunningInstance != null) //{ // return vsRunningInstance.VsInstance; //} string progId = $"!VisualStudio.DTE.{vsVersion}:{processId.ToString()}"; object runningObject = null; IBindCtx bindCtx = null; IRunningObjectTable rot = null; IEnumMoniker enumMonikers = null; try { Marshal.ThrowExceptionForHR(WinApiProxy.CreateBindCtx(reserved: 0, ppbc: out bindCtx)); bindCtx.GetRunningObjectTable(out rot); rot.EnumRunning(out enumMonikers); IMoniker[] moniker = new IMoniker[1]; IntPtr numberFetched = IntPtr.Zero; while (enumMonikers.Next(1, moniker, numberFetched) == 0) { IMoniker runningObjectMoniker = moniker[0]; string name = null; try { runningObjectMoniker?.GetDisplayName(bindCtx, null, out name); Debug.WriteLine($"Name = {name}"); } catch (UnauthorizedAccessException) { // Do nothing, there is something in the ROT that we do not have access to. } if (!string.IsNullOrEmpty(name) && string.Equals(name, progId, StringComparison.Ordinal)) { Marshal.ThrowExceptionForHR(rot.GetObject(runningObjectMoniker, out runningObject)); break; } } } finally { if (enumMonikers != null) { Marshal.ReleaseComObject(enumMonikers); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (bindCtx != null) { Marshal.ReleaseComObject(bindCtx); } } return((DTE)runningObject); }