Powershell Global Hotkeys(现有Block)

时间:2016-05-13 12:14:43

标签: powershell user32

我正在尝试编写一个简单的热键应用程序,它会写出我们经常使用的文本来节省我的团队时间。我有它的工作,但我很难找到一个在Microsoft Office中没有做过的关键组合。因此我希望修改脚本以阻止这些热键并使用我自己的热键。问题是我几乎不理解我正在使用的代码。这是代码片,我希望有人比我更幸运。

Side Note,不能使用已经为我做得很好的许多应用程序中的一个。长话故事。

奖金问题 - 任何人都可以更快地将一堆文本粘贴到前台应用程序中。 Sendkey比我想要的慢。我有一个版本,我将文本放入剪贴板并粘贴,但团队需要他们的剪贴板不会改变。

[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null
while ($true) {
    try
    {
        $ImportDll = [User32]
    }
    catch
    {
        $DynAssembly = New-Object System.Reflection.AssemblyName('Win32Lib')
        $AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
        $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('Win32Lib', $False)
        $TypeBuilder = $ModuleBuilder.DefineType('User32', 'Public, Class')

        $DllImportConstructor = [Runtime.InteropServices.DllImportAttribute].GetConstructor(@([String]))
        $FieldArray = [Reflection.FieldInfo[]] @(
            [Runtime.InteropServices.DllImportAttribute].GetField('EntryPoint'),
            [Runtime.InteropServices.DllImportAttribute].GetField('ExactSpelling'),
            [Runtime.InteropServices.DllImportAttribute].GetField('SetLastError'),
            [Runtime.InteropServices.DllImportAttribute].GetField('PreserveSig'),
            [Runtime.InteropServices.DllImportAttribute].GetField('CallingConvention'),
            [Runtime.InteropServices.DllImportAttribute].GetField('CharSet')
        )

        $PInvokeMethod = $TypeBuilder.DefineMethod('GetAsyncKeyState', 'Public, Static', [Int16], [Type[]] @([Windows.Forms.Keys]))
        $FieldValueArray = [Object[]] @(
            'GetAsyncKeyState',
            $True,
            $False,
            $True,
            [Runtime.InteropServices.CallingConvention]::Winapi,
            [Runtime.InteropServices.CharSet]::Auto
        )
        $CustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, @('user32.dll'), $FieldArray, $FieldValueArray)
        $PInvokeMethod.SetCustomAttribute($CustomAttribute)

        $PInvokeMethod = $TypeBuilder.DefineMethod('GetKeyboardState', 'Public, Static', [Int32], [Type[]] @([Byte[]]))
        $FieldValueArray = [Object[]] @(
            'GetKeyboardState',
            $True,
            $False,
            $True,
            [Runtime.InteropServices.CallingConvention]::Winapi,
            [Runtime.InteropServices.CharSet]::Auto
        )
        $CustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, @('user32.dll'), $FieldArray, $FieldValueArray)
        $PInvokeMethod.SetCustomAttribute($CustomAttribute)

        $PInvokeMethod = $TypeBuilder.DefineMethod('MapVirtualKey', 'Public, Static', [Int32], [Type[]] @([Int32], [Int32]))
        $FieldValueArray = [Object[]] @(
            'MapVirtualKey',
            $False,
            $False,
            $True,
            [Runtime.InteropServices.CallingConvention]::Winapi,
            [Runtime.InteropServices.CharSet]::Auto
        )
        $CustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, @('user32.dll'), $FieldArray, $FieldValueArray)
        $PInvokeMethod.SetCustomAttribute($CustomAttribute)

        $PInvokeMethod = $TypeBuilder.DefineMethod('ToUnicode', 'Public, Static', [Int32],
            [Type[]] @([UInt32], [UInt32], [Byte[]], [Text.StringBuilder], [Int32], [UInt32]))
        $FieldValueArray = [Object[]] @(
            'ToUnicode',
            $False,
            $False,
            $True,
            [Runtime.InteropServices.CallingConvention]::Winapi,
            [Runtime.InteropServices.CharSet]::Auto
        )
        $CustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, @('user32.dll'), $FieldArray, $FieldValueArray)
        $PInvokeMethod.SetCustomAttribute($CustomAttribute)

        $PInvokeMethod = $TypeBuilder.DefineMethod('GetForegroundWindow', 'Public, Static', [IntPtr], [Type[]] @())
        $FieldValueArray = [Object[]] @(
            'GetForegroundWindow',
            $True,
            $False,
            $True,
            [Runtime.InteropServices.CallingConvention]::Winapi,
            [Runtime.InteropServices.CharSet]::Auto
        )
        $CustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, @('user32.dll'), $FieldArray, $FieldValueArray)
        $PInvokeMethod.SetCustomAttribute($CustomAttribute)

        $ImportDll = $TypeBuilder.CreateType()
    }

    Start-Sleep -Milliseconds 25

    try
    {

        #loop through typeable characters to see which is pressed
        for ($TypeableChar = 1; $TypeableChar -le 254; $TypeableChar++)
        {
            $VirtualKey = $TypeableChar
            $KeyResult = $ImportDll::GetAsyncKeyState($VirtualKey)
            #if the key is pressed
            if (($KeyResult -band 0x8000) -eq 0x8000)
            {

                #check for keys not mapped by virtual keyboard
                $LeftShift    = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::LShiftKey) -band 0x8000) -eq 0x8000
                $RightShift   = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::RShiftKey) -band 0x8000) -eq 0x8000
                #$LeftCtrl     = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::LControlKey) -band 0x8000) -eq 0x8000
                #$RightCtrl    = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::RControlKey) -band 0x8000) -eq 0x8000
                $LeftAlt      = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::LMenu) -band 0x8000) -eq 0x8000
                $RightAlt     = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::RMenu) -band 0x8000) -eq 0x8000
                #$TabKey       = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::Tab) -band 0x8000) -eq 0x8000
                #$SpaceBar     = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::Space) -band 0x8000) -eq 0x8000
                #$DeleteKey    = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::Delete) -band 0x8000) -eq 0x8000
                #$BackSpaceKey = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::Back) -band 0x8000) -eq 0x8000
                #$LeftArrow    = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::Left) -band 0x8000) -eq 0x8000
                #$RightArrow   = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::Right) -band 0x8000) -eq 0x8000
                #$UpArrow      = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::Up) -band 0x8000) -eq 0x8000
                #$DownArrow    = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::Down) -band 0x8000) -eq 0x8000
                $one         = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::D1) -band 0x8000) -eq 0x8000
                $two         = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::D2) -band 0x8000) -eq 0x8000
                $three         = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::D3) -band 0x8000) -eq 0x8000
                $four         = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::D4) -band 0x8000) -eq 0x8000
                $five         = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::D5) -band 0x8000) -eq 0x8000
                $six         = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::D6) -band 0x8000) -eq 0x8000
                $seven         = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::D7) -band 0x8000) -eq 0x8000
                $eight         = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::D8) -band 0x8000) -eq 0x8000
                $nine         = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::D9) -band 0x8000) -eq 0x8000
                $zero         = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::D0) -band 0x8000) -eq 0x8000
                #$escape         = ($ImportDll::GetAsyncKeyState([Windows.Forms.Keys]::Escape) -band 0x8000) -eq 0x8000

                #check for capslock
                #$caps = ([Console]::CapsLock)

                if (($LeftAlt -or $RightAlt) -and ($LeftShift -or $RightShift))  {
                    if ($one) {
                        [System.Windows.Forms.SendKeys]::SendWait($savedData[1])
                        return
                    }
                    elseif ($two) {
                        [System.Windows.Forms.SendKeys]::SendWait($savedData[2])
                        return
                    }
                    elseif ($three) {
                        [System.Windows.Forms.SendKeys]::SendWait($savedData[3])
                        return
                    }
                    elseif ($four) {
                        [System.Windows.Forms.SendKeys]::SendWait($savedData[4])
                        return
                    }
                    elseif ($five) {
                        [System.Windows.Forms.SendKeys]::SendWait($savedData[5])
                        return
                    }
                    elseif ($six) {
                        [System.Windows.Forms.SendKeys]::SendWait($savedData[6])
                        return
                    }
                    elseif ($seven) {
                        [System.Windows.Forms.SendKeys]::SendWait($savedData[7])
                        return
                    }
                    elseif ($eight) {
                        [System.Windows.Forms.SendKeys]::SendWait($savedData[8])
                        return
                    }
                    elseif ($nine) {
                        [System.Windows.Forms.SendKeys]::SendWait($savedData[9])                           
                        return
                    }
                    elseif ($zero) {  
                        [System.Windows.Forms.SendKeys]::SendWait($savedData[0])                         
                        return
                    }
                }
            }
        }
    }
    catch {}
}

0 个答案:

没有答案