Try this, but note that you should take care of any key presses unless you run this in a window environment (e.g. Win32::GUI):
#!/usr/bin/perl -w use strict; use Win32::API; use constant VK_SHIFT => 0x10; use constant VK_CONTROL => 0x11; use constant VK_MENU => 0x12; =head2 GetAsyncKeyState($keyCode) Retrieve the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off-- alternating each time the key is pressed). $keyCode -- A single character string: A..Z0..9, or a virtual key code. Example: VK_SHIFT, "C" Return 1 if the key is depressed, 0 if it's not. =cut my $rsGetAsyncKeyState = new Win32::API("user32", "GetAsyncKeyState", +"I", "N"); sub GetAsyncKeyState { my ($keyCode) = @_; $keyCode = ord($keyCode) if($keyCode =~ /^[A-Z0-9]$/); my $ret = $rsGetAsyncKeyState->Call($keyCode); $ret = unpack("S", pack("s", $ret)); return( $ret & 2**15 ? 1 : 0 ); } my $key = VK_SHIFT; #my $key = "8"; #my $key = "J"; while(1) { print "state: " . GetAsyncKeyState($key) . "\n"; sleep(1); }

There is more to read about this if you look up GetAsyncKeyState in the Win32 API, e.g. here: http://www.sxlist.com/techref/os/win/api/win32/func/src/f27_3.htm

/J


In reply to Re: GetAsyncKeyState API Question by jplindstrom
in thread GetAsyncKeyState API Question by alkaloid

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.