The general problem here is that modifier keys like <CTRL> and <ALT> are being interpreted by the tty itself.  You could try something like this, however:

#!/usr/bin/perl use Term::ReadKey; my $my_sequence = qr/\x1b\x02\z/; # <CTRL>+<ALT>+B my $key; my $key_buf; ReadMode 4; while ($key ne 'q') { $key = ReadKey(); printf "key=\\x%02x\n", ord($key); # debug $key_buf .= $key; if ($key_buf =~ $my_sequence) { system "xmessage 'OH YA!'"; last; } }; ReadMode 0;

This has its limits, too (for example I wouldn't know how to tell apart <CTRL>+<ALT>+B from <CTRL>+<ALT>+<SHIFT>+B), but maybe it's sufficient for your purpose.

<CTRL>+A .. <CTRL>+Z typically maps to 1..26 (ord($key), that is); and <ALT>+... typically generates a two-byte sequence with a leading ESC (27 or \x1b). Other keys (cursor keys, etc.) generate three-byte sequences...

Printing out the sequence you get when you hit a certain key/modifier combination will tell you which substring to match (you don't necessarily need a regex for this, btw; index() would do).  But in case portability is an issue, be aware that terminals sometimes differ with respect to the sequences they generate — in particular the multi-byte ones...

Update: the loop exit condition ($key eq 'q') is probably too simple for production use (it would also exit on hitting <ALT>+q, for example, due to the second byte of the escape sequence being 'q')...  Fixing this is left as an exercise for the reader :)


In reply to Re: Capturing control characters by almut
in thread Capturing control characters by aztlan667

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.