Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

manipulating the lights on a keyboard

by J0hny_Lightning (Novice)
on Jul 24, 2003 at 14:56 UTC ( #277565=perlquestion: print w/replies, xml ) Need Help??

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

I came across the following link: 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)


    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/"; 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.


      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).



    -- 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.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://277565]
Approved by dragonchild
Front-paged by arthas
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2022-10-01 11:16 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (126 votes). Check out past polls.