use strict; use warnings; use Data::Dumper; use Win32::API; use constant { KEYEVENTF_EXTENDEDKEY => 0x0001, KEYEVENTF_KEYUP => 0x0002, KEYEVENTF_SCANCODE => 0x0008, KEYEVENTF_UNICODE => 0x0004, INPUT_KEYBOARD => 1 }; Win32::API::Struct->typedef( KEYBDINPUT => qw( WORD wVk; WORD wScan; DWORD dwFlags; DWORD time; UINT_PTR dwExtraInfo; DWORD junk1; DWORD junk2; ) ); Win32::API::Struct->typedef( INPUT => qw( DWORD type; KEYBDINPUT ki; ) ); #https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx die("Error: $^E") unless ( Win32::API::More->Import( 'user32', 'UINT WINAPI SendInput(UINT nInputs, LPINPUT pInputs, int cbSize)' ) ); sub sendString { my $val = shift; my @val = split( //, $val ); my @input = ( Win32::API::Struct->new("INPUT"), Win32::API::Struct->new("INPUT") ); $input[0]->{type} = INPUT_KEYBOARD; $input[0]->{ki}->{dwFlags} = KEYEVENTF_UNICODE; #$input[0]->{ki}->{dwFlags} = 0; $input[1] = $input[0]; $input[1]->{ki}->{dwFlags} |= KEYEVENTF_KEYUP; for my $v (@val) { ( $input[0]->{ki}->{wVk}, $input[1]->{ki}->{wVk} ) = ( 0, 0 ); ( $input[0]->{ki}->{junk1}, $input[1]->{ki}->{junk1} ) = ( 0, 0 ); ( $input[0]->{ki}->{junk2}, $input[1]->{ki}->{junk2} ) = ( 0, 0 ); ( $input[0]->{ki}->{time}, $input[1]->{ki}->{time} ) = ( 0, 0 ); ( $input[0]->{ki}->{dwExtraInfo}, $input[1]->{ki}->{dwExtraInfo} ) = ( 0, 0 ); my $code = ord($v); #A hardware scan code for the key. If dwFlags specifies KEYEVENTF_UNICODE, #wScan specifies a Unicode character which is to be sent to the foreground application. print "$code\n"; ( $input[0]->{ki}->{wScan}, $input[1]->{ki}->{wScan} ) = ( $code, $code ); for my $i ( 0 .. 1 ) { my $c = SendInput( 1, $input[$i], $input[$i]->sizeof ); print "SendInput : $c\n"; print Win32::FormatMessage( Win32::GetLastError() ); } } } sendString("abc");