简体   繁体   English

使用 PowerShell 将空格字符发送到桌面 - 从睡眠中唤醒屏幕

[英]Using PowerShell to send space character to desktop - to wake screen from sleep

I know the following 2 line PowerShell script sends a space character to a new "shell" object it creates.我知道以下 2 行 PowerShell 脚本将空格字符发送到它创建的新“外壳”object。 Which does bring my sleeping screen out of its slumber.这确实使我的睡眠屏幕摆脱了沉睡。 But that opens, momentarily, a PowerShell window.但这会立即打开 PowerShell window。 So, if the screen is truly asleep-not a problem.所以,如果屏幕真的睡着了——不是问题。

But, there are times of the day when this will run when the screen isn't in a sleeping slumber.但是,一天中的某些时候,当屏幕没有处于睡眠状态时,它会运行。 Hence, when that happens, the user sees a very short annoying flash of the created Wscript.Shell因此,当这种情况发生时,用户会看到创建的 Wscript.Shell 的一个非常短的恼人 flash

I'd like to just send the space keystroke to the desktop - to wake the sleeping screen.我只想将空格键发送到桌面 - 唤醒睡眠屏幕。 Without the momentary opening and closing of the Wscript.Shell ie, send the " " character to "the desktop" and not to a new shell object.在没有 Wscript.Shell 的瞬时打开和关闭的情况下,将“”字符发送到“桌面”而不是新的 shell object。 Is this possible?这可能吗? I know nothing (obviously) about writing PowerShell.我对编写 PowerShell 一无所知(显然)。

$myshell=New-Object -com "Wscript.Shell" 
$myshell.sendkeys(" ")

Perhaps there is a way the PowerShell script could know if the screen and/or computer is not in a sleep state, and therefore this could all just be avoided from running in that situation?也许有一种方法 PowerShell 脚本可以知道屏幕和/或计算机是否没有处于睡眠状态 state,因此这一切都可以避免在这种情况下运行? Any help is much appreciated.任何帮助深表感谢。

It's being setup with a Powershell script to create a Task in task scheduler.它正在使用 Powershell 脚本进行设置,以在任务调度程序中创建任务。 That task then calls the 2 line PowerShell script to send the " " key, to wake the computer up just prior to the day's activity.该任务然后调用 2 行 PowerShell 脚本来发送“”键,以便在当天活动之前唤醒计算机。

I found my answer.我找到了我的答案。 Since I am using AutoHotKey, the following code will use PowerShell to create a new task in task scheduler.由于我使用的是 AutoHotKey,以下代码将使用 PowerShell 在任务调度程序中创建一个新任务。 By using the cmd /c start /min "" powershell... style of command, the pop-up window does not display - but the computer and screen wake up.通过使用cmd /c start /min "" powershell...样式的命令,弹出 window 不会显示 - 但计算机和屏幕会唤醒。

WakeUp:= A_Now
EnvAdd WakeUp, 10, Minutes       ; wake 10 minutes from now
UTC_Delta-= A_NowUTC, Seconds    ; Seconds is more accurate due to rounding issue
UTC_Delta:= Round(-UTC_Delta/60) ; Round to nearest minute to ensure accuracy
WakeUp   += UTC_Delta, Minutes   ; Apply offset to convert to UTC
FormatTime, WakeTime,% WakeUp, "yyyy-MM-ddTHH:mm:ssZ"
psScript =
(
$action = New-ScheduledTaskAction -Execute cmd -Argument '/c start /min "" powershell -WindowStyle Hidden -ExecutionPolicy Bypass -command {$myshell=New-Object -com "Wscript.Shell" $myshell.sendkeys(" ")}'
$settings = New-ScheduledTaskSettingsSet -WakeToRun
$trigger = New-ScheduledTaskTrigger -Once -At %WakeTime%
Register-ScheduledTask -TaskName wakeup -Action $action -Settings $settings -Trigger $trigger -Force
)
RunWait PowerShell.exe -Command &{%psScript%} ,, hide
;use the following if you want to see PowerShell output
;Run PowerShell.exe -NoExit -Command &{%psScript%}

This wakes the screen by mouse movement, not keyboard but seems to work for me.这通过鼠标移动而不是键盘唤醒屏幕,但似乎对我有用。

Based on Screen wake up settings in power options or registry for windows OS?基于windows OS 的电源选项或注册表中的屏幕唤醒设置?

$Signature = @"
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, 
IntPtr lParam);

[DllImport("user32.dll")]
public static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);
"@

Try {
  $ShowWindowAsync = Add-Type -MemberDefinition $Signature -Name 
"Win32ShowWindowAsync" -Namespace Win32Functions -PassThru -ErrorAction Ignore }
Catch { }

$MONITOR_ON = -1;
$MONITOR_OFF = 2;
$MONITOR_STANBY = 1;

[System.Int64]$MOUSEEVENTF_MOVE = 0x0001;

[System.IntPtr]$HWND_BROADCAST = New-Object System.IntPtr(0xffff)
[System.UInt32]$WM_SYSCOMMAND = 0x0112
[System.IntPtr]$SC_MONITORPOWER = New-Object System.IntPtr(0xF170)

# this commands puts monitors to sleep
# $ShowWindowAsync::SendMessage($HWND_BROADCAST, $WM_SYSCOMMAND, $SC_MONITORPOWER, [System.IntPtr]$MONITOR_OFF);

# this command wakes monitors up
$ShowWindowAsync::mouse_event($MOUSEEVENTF_MOVE, 0, 1, 0, [System.UIntPtr]::Zero);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM