从 Powershell 调用 user32.dll 中的 keybd_event

Mr.*_*yed 4 c# powershell

我试图阻止屏幕保护程序启动。我尝试过发送击键,但它们都需要按标题抓取窗口。如果桌面上没有打开的窗口,则没有什么可抓取的。

所以我找到了一些可能有用的东西,但我不是 C# 向导。它基于在另一个应用程序中模拟按键和按键释放?

下面的代码应该发送 1 但我遗漏了一些东西。在从 Powershell 调用新类型之前,我不会收到任何错误。完成此操作后,我想让它发送 F15 来重置屏幕保护程序倒计时,但不修改屏幕上的内容。(但我得先发送1秒爬行)

Add-Type @"
using System.Runtime.InteropServices;

namespace ConsoleApplication1 
{
    public static class PressKeyForMe 
    { 
        [DllImport("user32.dll")] 
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);

        //public static void Main(string[] args)
        public static void Main()
        {            
            //This code will press and hold the '1' button for 3 secs, and then will release for 1 second
            //VK_F15 0x7E
            keybd_event((byte)0x31, (byte)0x02, 0, UIntPtr.Zero);
            Thread.Sleep(3000);

            keybd_event((byte)0x31, (byte)0x82, (uint)0x2, UIntPtr.Zero);
            Thread.Sleep(1000);
        }
    }

}
"@
cls

#all of these give me: Unable to find type
[void] [PressKeyForMe]::Main()
[void] [ConsoleApplication1]::PressKeyForMe()
[void] [PressKeyForMe.Main]
[void] [ConsoleApplication1.Main]
[void] [ConsoleApplication1.PressKeyForMe]::Main()
Run Code Online (Sandbox Code Playgroud)

Mat*_*sen 6

似乎您using在该类型定义中缺少一些 - 语句:

Add-Type @"
using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace ConsoleApplication1 
{
    public static class PressKeyForMe 
    { 
        [DllImport("user32.dll")] 
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);

        //public static void Main(string[] args)
        public static void Main()
        {            
            //This code will press and hold the '1' button for 3 secs, and then will release for 1 second
            //VK_F15 0x7E
            keybd_event((byte)0x31, (byte)0x02, 0, UIntPtr.Zero);
            Thread.Sleep(3000);

            keybd_event((byte)0x31, (byte)0x82, (uint)0x2, UIntPtr.Zero);
            Thread.Sleep(1000);
        }
    }

}
"@
Run Code Online (Sandbox Code Playgroud)

这现在应该可以工作:

[ConsoleApplication1.PressKeyForMe]::Main()
Run Code Online (Sandbox Code Playgroud)

您还可以仅添加方法并将自动生成的类型分配给变量:

$KeyPresser = Add-Type -MemberDefinition @"
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);

public static void PressOne(int press, int release)
{
    //This code will press and hold the '1' button for 3 secs, and then will release for 1 second
    //VK_F15 0x7E
    keybd_event((byte)0x31, (byte)0x02, 0, UIntPtr.Zero);
    Thread.Sleep(press);

    keybd_event((byte)0x31, (byte)0x82, (uint)0x2, UIntPtr.Zero);
    Thread.Sleep(release);
}

public static void PressOne()
{
    PressOne(3000, 1000);
}
"@ -Name PressKeyForMe -UsingNamespace System.Threading -PassThru
Run Code Online (Sandbox Code Playgroud)

现在您可以在没有完整类型名称的情况下调用该方法:

PS C:\> $KeyPresser::PressOne() 
PS C:\> $KeyPresser::PressOne(400,120) # you really need to press 3 seconds?
Run Code Online (Sandbox Code Playgroud)