Hi guys,
A small pet project of mine that I am working on is writing a set of scripts that duplicates certain keyboard input from one computer to another. I got the server and client set up and running, and I can enter input from the server and have the client receive it.
The catch is that I want it to be able to both send and receive input while the windows aren't in focus. Through searching on this site, I found that someone recommended Win32::GuiTest for sending keyboard events without focus, and that is working great, but I haven't found anything for receiving keyboard events without focus.
Just incase anyone is curious, I'm writing this for use of running two characters in WoW - I want the "1" key to trigger on both computers when I press it on one. I'm fully aware that reading input while the program isn't in focus is exactly what is necessary to write a keylogger, but there are plenty of other legitimate uses for such functionality (global hotkeys being the first example to come to mind), so I expected something like this to be available, but I haven't come across it yet. Is there some package that does this?
Here is my current code... (not that it isn't functioning as I currently intend - I'm just missing a better alternative to ReadKey)
server:
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use Term::ReadKey;
ReadMode 1;
my ($PORT, $server, $client);
$PORT = 9000; # pick something not in use
$server = IO::Socket::INET->new(
Proto => 'tcp',
LocalPort => $PORT,
Listen => SOMAXCONN,
Reuse => 1
);
die "can't setup server" unless $server;
print "[Server accepting clients]\n";
while ($client = $server->accept()) {
$client->autoflush(1);
print "Received connection!\n";
while (1)
{
my ($key);
while (not defined ($key)) {
$key = ReadKey(-1);
}
print "Get key $key\n";
print $client "$key\n";
$key = ReadKey(-1); #clear $key
}
}
close $client;
ReadMode 0;
client:
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use Win32::GuiTest qw(:ALL);
my $host = shift || 'localhost';
my $port = shift || '9000';
my ($kidpid, $socket, $line);
# create a tcp connection to the specified host and port
$socket = IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => $host,
PeerPort => $port
)
or die "can't connect to port $port on $host: $!";
print "[Connected to $host:$port]\n";
while (1)
{
while (defined ($line = <$socket>)) {
sleep(1);
chomp($line);
SendKeys($line);
print "$line\n";
}
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.