Acessing C# code in Powershell

允我心安 提交于 2019-12-08 13:25:12

问题


I want to use a system wide hotkey in my powershell GUI application. I found this C# code for registering a hotkey and integrated it in my script. The trouble is, because I can't fully understand the C# code, I don't know how to properly add the parameters for the registerhotkey method.

The goal is: on key press SHIFT+ALT+Z execute the code in $hotkeyExecCode.

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Application]::EnableVisualStyles()

$form1 = New-Object 'System.Windows.Forms.Form'
$form1.ClientSize = '200, 150'


$cCode = @"
 using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Threading;

    /// <summary> This class allows you to manage a hotkey </summary>
    public class GlobalHotkeys : IDisposable
    {
        [DllImport( "user32", SetLastError = true )]
        [return: MarshalAs( UnmanagedType.Bool )]
        public static extern bool RegisterHotKey (IntPtr hwnd, int id, uint fsModifiers, uint vk);
        [DllImport( "user32", SetLastError = true )]
        public static extern int UnregisterHotKey (IntPtr hwnd, int id);
        [DllImport( "kernel32", SetLastError = true )]
        public static extern short GlobalAddAtom (string lpString);
        [DllImport( "kernel32", SetLastError = true )]
        public static extern short GlobalDeleteAtom (short nAtom);

        public const int MOD_ALT = 1;
        public const int MOD_CONTROL = 2;
        public const int MOD_SHIFT = 4;
        public const int MOD_WIN = 8;

        public const int WM_HOTKEY = 0x312;

        public GlobalHotkeys()
        {
            this.Handle = Process.GetCurrentProcess().Handle;
        }

        /// <summary>Handle of the current process</summary>
        public IntPtr Handle;

        /// <summary>The ID for the hotkey</summary>
        public short HotkeyID { get; private set; }

        /// <summary>Register the hotkey</summary>
        public void RegisterGlobalHotKey(int hotkey, int modifiers, IntPtr handle)
        {
            UnregisterGlobalHotKey();
            this.Handle = handle;
            RegisterGlobalHotKey(hotkey, modifiers);
        }

        /// <summary>Register the hotkey</summary>
        public void RegisterGlobalHotKey(int hotkey, int modifiers)
        {
            UnregisterGlobalHotKey();

            try
            {
                // use the GlobalAddAtom API to get a unique ID (as suggested by MSDN)
                string atomName = Thread.CurrentThread.ManagedThreadId.ToString("X8") + this.GetType().FullName;
                HotkeyID = GlobalAddAtom(atomName);
                if (HotkeyID == 0)
                    throw new Exception("Unable to generate unique hotkey ID. Error: " + Marshal.GetLastWin32Error().ToString());

                // register the hotkey, throw if any error
                if (!RegisterHotKey(this.Handle, HotkeyID, (uint)modifiers, (uint)hotkey))
                    throw new Exception("Unable to register hotkey. Error: " + Marshal.GetLastWin32Error().ToString());

            }
            catch (Exception ex)
            {
                // clean up if hotkey registration failed
                Dispose();
                Console.WriteLine(ex);
            }
        }

        /// <summary>Unregister the hotkey</summary>
        public void UnregisterGlobalHotKey()
        {
            if ( this.HotkeyID != 0 )
            {
                UnregisterHotKey(this.Handle, HotkeyID);
                // clean up the atom list
                GlobalDeleteAtom(HotkeyID);
                HotkeyID = 0;
            }
        }

        public void Dispose()
        {
            UnregisterGlobalHotKey();
        }
    }
"@

Add-Type -TypeDefinition $cCode
$hotkeyExecCode = { $form1.WindowState = "normal"}

[GlobalHotkeys]::RegisterHotKey( ???????? )  # <--------- How do i add the parameters here?

$form1.windowstate = "minimized"
$form1.ShowDialog()      

来源:https://stackoverflow.com/questions/23855928/acessing-c-sharp-code-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!