http://www.perlmonks.org?node_id=277565

J0hny_Lightning has asked for the wisdom of the Perl Monks concerning the following question:

I came across the following link: www.mghansen.de/dosstuff/keyctlhowto.html that shows how to control the LEDs on the keyboard. I'm pretty new to perl and was wondering if anyone could point me in the right direction on implementing this in a perl script. This is similar to a plugin I saw once for xmms that blinks the LEDs to the music, but I couldnt find the link for your guys. Thanks in advance. --J0hny_Lightning

Replies are listed 'Best First'.
Re: manipulating the lights on a keyboard
by derby (Abbot) on Jul 24, 2003 at 17:31 UTC
    Here's how you can do it with Inline under linux (the only OS I tested). You need the appropriate perms to the console so YMMV (also, most error checking removed for brevity sake).

    (original Inline code)

    -derby

    Update: Whoa! I forgot that ioctl is normally available under pure perl. I hade to steal the KD_* and LED_* constants from the include file but it works fine.

    (buggy pure perl code)

    Update (again): Noticed a small bug in the above perl code when resetting the lights back to original. Here it is again:

    #!/usr/bin/perl require "sys/ioctl.ph"; open( FH, "/dev/console" ) || die "cannot open console"; $KDSETLED = 0x4B32; $KDGETLED = 0x4B31; $LED_NUM = 0x02; $LED_CAP = 0x04; $LED_SCR = 0x01; ioctl(FH, $KDGETLED, $orig); @ary = unpack("ccccs",$orig); foreach $state ( $LED_NUM, $LED_CAP, $LED_SCR, $ary[0] ) { $i = 0; $i |= $state; ioctl(FH, $KDSETLED, $i); sleep( 2 ); }

Re: manipulating the lights on a keyboard
by skyknight (Hermit) on Jul 24, 2003 at 15:08 UTC
    I am not aware of any libraries in CPAN that give you this functionality, but this might be good excuse to learn XS. If you can figure out how to twiddle the LEDs in C/C++, then just wrap it up with XS so you can call into it with Perl. From a short term practicality standpoint this would probably be a largely futile effort, but from a scholarly point of view, just learning XS would make it worth your troubles.
Re: manipulating the lights on a keyboard
by The Mad Hatter (Priest) on Jul 24, 2003 at 16:33 UTC
    Looking at the BASIC code on that website, I noticed they used peek and poke. MJD's PeekPoke module for Perl provides peek and poke functions. I don't know if it would work, but you might be able to adapt that BASIC code to Perl code using PeekPoke. Just a thought...
Re: manipulating the lights on a keyboard
by Abigail-II (Bishop) on Jul 24, 2003 at 15:09 UTC
    You can't decend low enough into the OS to do this directly from Perl. But you should be able to do this with the help of XS, and perhaps some C.

    And whatever you need to do on your system to get it working is very likely not going to work on a different OS.

    Abigail

      There are some platform-specific libraries for manipulating keyboard LEDs (like xleds), but the link above is working with hardcoded memory locations. It'll work on any i386 platform, regardless of OS. Now, getting to work on a different architecture is a different matter . . .

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

Re: manipulating the lights on a keyboard
by PodMaster (Abbot) on Jul 25, 2003 at 07:27 UTC
Re: manipulating the lights on a keyboard
by JaWi (Hermit) on Jul 25, 2003 at 07:13 UTC
    From a `real' console which understands EMCA-48 CSI sequences (like Linux for example), you can use the following snippet to control the leds on your keyboard:
    #!/usr/bin/perl -w while ( 1 ) { print "\e[".$_."q" and sleep 1 for ( 0..3 ); }
    This will turn on each individual led on turn.
    For more information on see the man-page of console_codes(4).

    HTH,

    JaWi

    -- JaWi

    "A chicken is an egg's way of producing more eggs."

Re: manipulating the lights on a keyboard
by J0hny_Lightning (Novice) on Jul 24, 2003 at 19:01 UTC
    Awesome stuff, thanks a lot for your help.


    --J0hny_Lightning