The following code should use the SendInput to send abc to the keyboard. Windows responds that "The operation completed successfully."
However I don't see any abc in the console.
am I still wrong with the way I encode the character in the wScan field ?
Is this because I'm SendingInput in another thread ?
Here is the codeuse 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=v +s.85).aspx die("Error: $^E") unless ( Win32::API::More->Import( 'user32', 'UINT WINAPI SendInput(UINT nInputs, LPINPUT pInputs, int cbSi +ze)' ) ); 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}->{dwExtraInf +o} ) = ( 0, 0 ); my $code = ord($v); #A hardware scan code for the key. If dwFlags specifies KEYEVE +NTF_UNICODE, #wScan specifies a Unicode character which is to be sent to th +e foreground application. print "$code\n"; ( $input[0]->{ki}->{wScan}, $input[1]->{ki}->{wScan} ) = ( $co +de, $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");
In reply to Re^4: Win32::API and keyboard hook
by frazap
in thread Win32::API and keyboard hook
by frazap
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |