C# 从Powershell调用user32.dll中的keybd_事件
我正试图阻止屏幕保护程序启动。 我尝试过发送击键,但它们都需要按标题抓取一个窗口。 如果桌面上没有打开的窗口,就没有什么可抓取的 所以我找到了一些可能有效的方法,但我不是C#wizard。 这是基于 下面的代码应该发送1,但我遗漏了一些东西。在从Powershell调用新类型之前,我不会收到任何错误。 之后,我想让它发送一个F15重置屏幕保护程序倒计时,但不修改屏幕上的东西。(但我必须先通过发送1来爬网)C# 从Powershell调用user32.dll中的keybd_事件,c#,powershell,C#,Powershell,我正试图阻止屏幕保护程序启动。 我尝试过发送击键,但它们都需要按标题抓取一个窗口。 如果桌面上没有打开的窗口,就没有什么可抓取的 所以我找到了一些可能有效的方法,但我不是C#wizard。 这是基于 下面的代码应该发送1,但我遗漏了一些东西。在从Powershell调用新类型之前,我不会收到任何错误。 之后,我想让它发送一个F15重置屏幕保护程序倒计时,但不修改屏幕上的东西。(但我必须先通过发送1来爬网) 似乎您在该类型定义中缺少了一些使用-语句的: Add-Type @" using Syst
似乎您在该类型定义中缺少了一些使用-语句的
:
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);
}
}
}
"@
现在应该可以这样做了:
[ConsoleApplication1.PressKeyForMe]::Main()
您还可以只添加方法,并将自动生成的类型分配给变量:
$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
现在,您可以在不使用完整类型名的情况下调用该方法:
PS C:\> $KeyPresser::PressOne()
PS C:\> $KeyPresser::PressOne(400,120) # you really need to press 3 seconds?
谢谢Mathias修复了我上面的代码。我能够完成我的脚本
对于好奇的人来说,下面是我是如何让F15钥匙工作的。
只需每隔几分钟使用一次[ScreenSpender.PressKeyForMe]::Main()
,即可让屏幕保护程序停止运行。
感谢John Savard在键盘扫描代码方面的帮助
#The following is an attempt to prevent the SceenSaver from kicking-in during installation
#We call it using this
#[ScreenSpender.PressKeyForMe]::Main()
Add-Type @"
using System;
using System.Threading;
using System.Runtime.InteropServices;
namespace ScreenSpender
{
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()
{
//bScan = PS/2 scan code. (We happen to be using Set 1)
// Make = when you press the key
// Break = when you release the key
//VK_F15 0x7E
//SC_F15 0x5D (Scan code set 1) (or 0x64) (http://www.quadibloc.com/comp/scan.htm - by John Savard)
//private const int KEYEVENTF_KEYUP = 0x02;
//This code will press and hold the 'F15' key for 250 Milliseconds, and then will release for 200 Milliseconds
keybd_event((byte)0x7E, (byte)0x5D, 0, UIntPtr.Zero);
Thread.Sleep(250);
// Release for 200 Milliseconds
keybd_event((byte)0x7E, (byte)0x5D, (uint)0x2, UIntPtr.Zero);
Thread.Sleep(200);
}
}
}
"@
谢谢你,马蒂亚斯!这很有效。不,我不需要按住那把钥匙3秒钟。我只是不确定在事情破裂之前我能改变什么。@Mr.Cool:)如果这解决了你的问题,请点击左边的复选标记,将答案标记为“接受”。我正在寻找一个大的绿色框,上面写着“接受此解决方案”,大约一个月。。
#The following is an attempt to prevent the SceenSaver from kicking-in during installation
#We call it using this
#[ScreenSpender.PressKeyForMe]::Main()
Add-Type @"
using System;
using System.Threading;
using System.Runtime.InteropServices;
namespace ScreenSpender
{
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()
{
//bScan = PS/2 scan code. (We happen to be using Set 1)
// Make = when you press the key
// Break = when you release the key
//VK_F15 0x7E
//SC_F15 0x5D (Scan code set 1) (or 0x64) (http://www.quadibloc.com/comp/scan.htm - by John Savard)
//private const int KEYEVENTF_KEYUP = 0x02;
//This code will press and hold the 'F15' key for 250 Milliseconds, and then will release for 200 Milliseconds
keybd_event((byte)0x7E, (byte)0x5D, 0, UIntPtr.Zero);
Thread.Sleep(250);
// Release for 200 Milliseconds
keybd_event((byte)0x7E, (byte)0x5D, (uint)0x2, UIntPtr.Zero);
Thread.Sleep(200);
}
}
}
"@