Plan 9: Key Visualizer


Simple key visualizer for capturing keystrokes during a screencast using kbdfs, awk and statusmsg.

#!/bin/rc

fn notify {
    scr=`{awk '{print $4, $5}' /dev/screen}
    w=200
    h=60
    ws=$scr(1)
    hs=$scr(2)
    minx=`{echo $ws - $w | bc}
    warg=`{echo -w  $minx,0,$ws,$h}
    font=/n/ttf/mine/32/font aux/statusmsg $warg $1
}

{
    tee /dev/kbdtap |
    tr '\000' '\034' |
    awk -v 'RS=\034' '
    BEGIN {
        esc   = sprintf("%c", 033)
        tab   = sprintf("%c", 011)
        bksp  = sprintf("%c", 010)
        del   = sprintf("%c", 0177)
        enter = sprintf("%c", 012)

        shift = "\357\200\226"
        ctl   = "\357\200\227"
        alt   = "\357\200\225"

        home  = "\357\200\215"
        pgup  = "\357\200\217"
        pgdn  = "\357\200\223"
        end   = "\357\200\230"

        up    = "\357\200\216"
        left  = "\357\200\221"
        right = "\357\200\222"
        down  = "\357\200\200"
    }

    $0 ~ /^k/ {
        s = substr($0, 2)

        gsub(shift, "Shift ", s)
        gsub(ctl,   "Ctrl ",  s)
        gsub(alt,   "Alt ",   s)

        gsub(home,  "Home ",  s)
        gsub(end,   "End ",   s)
        gsub(pgup,  "PgUp ",  s)
        gsub(pgdn,  "PgDn ",  s)

        gsub(up,    "Up ",    s)
        gsub(down,  "Down ",  s)
        gsub(left,  "Left ",  s)
        gsub(right, "Right ", s)

        gsub(esc,   "Esc ",   s)
        gsub(tab,   "Tab ",   s)
        gsub(bksp,  "Bksp ",  s)
        gsub(del,   "Del ",   s)
        gsub(enter, "Enter ", s)

        print s
    }' |
    # font=/n/ttf/mine/32/font aux/statusmsg -k -w 0,0,300,60
    notify
}
 
  

Lessons Learnt

  1. `split {command} is a second form of command execution where you supply your own split string instead of $ifs.
  2. Use octal values (\1) or hex values (\x1) for control characters while setting environment variable e.g. ifs=`""{echo -n xy | tr 'xy' '\12'} 

 

References

  1. Man pages kbdfs 

 

Comments

Popular posts from this blog

Plan 9 : The Infinity Notebook

Plan 9: Quick Boot with UEFI

Plan 9: Dynamic HTML