【问题标题】:In powershell how can a simulate the shift key being held down?在 powershell 中,如何模拟按住 shift 键?
【发布时间】:2013-05-08 18:10:37
【问题描述】:

我需要运行以下 2 行代码来打开 access 数据库。问题是这个数据库有一个自动加载的模型表单,它会阻止 powershell 脚本的其余部分运行直到它关闭。

$ms_access = New-Object -ComObject "Access.Application"
$ms_access.OpenCurrentDatabase("C:\db.accdb", $false)

如果按下 shift 键,我可以在打开数据库时避免对话框。我已经对此进行了测试,并且可以正常工作。现在我希望在运行我的 powershell 脚本时不必按住 shift 键,那么 powershell 有没有办法模拟按下的 shift 键?不仅仅是一个 SendKey,而是在 OpenCurrentDatabase 的持续时间内按住?

类似的东西?

$ms_access = New-Object -ComObject "Access.Application"
Set-Shift-Down
$ms_access.OpenCurrentDatabase("C:\db.accdb", $false)
Set-Shift-Up

这适用于 Powershell-v2

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    我会使用p/invoke:

    Add-Type @"
    using System;                                                                     
    using System.Runtime.InteropServices;
    public class Tricks {
    [DllImport("user32.dll")]                                                            
    public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);}
    "@   
    
    [system.reflection.assembly]::Loadwithpartialname("system.windows.forms")
    
    #left shift key pressed
    [tricks]::keybd_event([System.Windows.Forms.Keys]::LShiftKey, 0x45, 0, 0);
    ... do stuff here
    #left shift key released
    [tricks]::keybd_event([System.Windows.Forms.Keys]::LShiftKey, 0x45, 0x2, 0);    
    

    如果您不想加载 windows.forms 程序集,请使用 0xA0 作为 LShiftKey 的值

    我真的不知道原生 powershell 方式,p/invoke 应该可以工作,但我无法像你想使用的那样测试它。

    【讨论】:

      猜你喜欢
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 2010-10-21
      • 1970-01-01
      相关资源
      最近更新 更多