Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Win32::API and keyboard hook

by beech (Parson)
on Sep 15, 2017 at 19:18 UTC ( [id://1199476]=note: print w/replies, xml ) Need Help??


in reply to Win32::API and keyboard hook

Hi,

Yup Win32::API docs are a bit confused

my $CallNextHookEx = Win32::API->Import( 'user32', 'LRESULT WINAPI CallNextHookEx(HHOOK hhk, int nCode, WPARAM wPara +m, LPARAM lParam)' );

Import creates sub CallNextHookEx (which you can call like CallNextHookEx() ), but the return value is a boolean, and you can't ->Call on it

If you want to be able to use $CallNextHookEx->Call() change Win32::API->Import to Win32::API->new

Also, MsgLoop will never get a chance to hear from sendString unless they run simultaneously, which means using two programs or using two threads ( MsgLoop runs in one thread, and sendString runs in another )

Good luck

Replies are listed 'Best First'.
Re^2: Win32::API and keyboard hook
by frazap (Monk) on Sep 19, 2017 at 08:01 UTC
    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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1199476]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-18 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found