in reply to Re: Win32::API and keyboard hook
in thread Win32::API and keyboard hook

Thanks for the correction.

Can someone show me how to use Win32::API::Struct ?

I have the C INPUT struct and I must send a pointer LPINPUT to that when calling SendInput.

If I understand the doc, I should say

Win32::API::Struct->typedef( *INPUT => qw( DWORD type; KEYBDINPUT ki; ) );

But after that Win32::API::Type->is_known('LPINPUT') return false

How can I define the INPUT structure, put some data in it and then use that in the SendInput call ?

Thanks

Replies are listed 'Best First'.
Re^3: Win32::API and keyboard hook
by beech (Parson) on Sep 19, 2017 at 23:25 UTC

    Hi,

    https://metacpan.org/pod/Win32::API#USING-STRUCTURES gives some explanation about "LP" prefix

    Here is a modified version of samples/GetCursorPos.pl

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; use Win32::API; Win32::API::Struct->typedef( POINT => qw( LONG x; LONG y; ) ); Win32::API->Import('user32' => 'BOOL GetCursorPos(LPPOINT pt)'); #### using OO semantics my $pt = Win32::API::Struct->new('POINT'); @{$pt}{qw/x y/} = (0,0); GetCursorPos($pt) or die "GetCursorPos failed: $^E"; print "Cursor is at: $pt->{x}, $pt->{y}\n"; #### using tie semantics my %pt; tie %pt, 'Win32::API::Struct' => 'POINT'; @pt{qw/x y/}=(0,0); GetCursorPos(\%pt) or die "GetCursorPos failed: $^E"; print "Cursor is at: $pt{x}, $pt{y}\n"; dd( map{ { "is_known $_ ", Win32::API::Type->is_known($_)}; } qw{ POIN +T LPPOINT } ); __END__ Cursor is at: 198, 273 Cursor is at: 198, 273 ({ "is_known POINT " => 1 }, { "is_known LPPOINT " => "" })

    so POINT is know, but LPPOINT is not known, and that isn't a problem

      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");

        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 ?

        Hi

        I don't know, the SendInput docs doesn't have an example(and I'm loathe to interpret those too much), but it does link keybd_event() which appears much simpler and has an example

        Have you heard of Win32::GuiTest? It has a "SendKeys" and WMSetText ...

        update: I gave keybd_event a shot it prints 1 instead of toggling numlock, so SendKeys works to type "abc" :)