您需要将WM_CHAR或WM_UNICHAR消息发送到有问题的窗口,而不是依赖于处于活动状态并处于前台的窗口。
这个例子似乎对我有用。在将键发送到在窗口中links
运行时,我能够切换到其他应用程序。cmd.exe
当我输入完电子邮件时,链接已经导航到example.com
.
#!/usr/bin/env perl
use strict; use warnings;
use Encode qw(encode);
use Time::HiRes qw( usleep );
use Win32::GuiTest qw(SendMessage ShowWindow WaitWindow);
use constant WM_CHAR => 0x0102;
use constant SW_HIDE => 0;
use constant SW_SHOW => 5;
system qw(start links);
my $hwnd = WaitWindow('Links', 5);
ShowWindow $hwnd, SW_HIDE;
send_message($hwnd, "ghttp://example.com/\r");
ShowWindow $hwnd, SW_SHOW;
send_message($hwnd, "q\r", 3_000_000);
sub send_message {
my ($hwnd, $msg, $delay) = @_;
$delay ||= 25_000;
my @keys = encode_keys($msg);
for my $key (@keys) {
SendMessage $hwnd, WM_CHAR, $key, 0;
usleep $delay;
}
return;
}
sub encode_keys {
my ($msg) = @_;
map ord, map encode('UTF16-LE', $_), split //, $msg;
}