Thanks !

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 code
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=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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.