G'day pierrot,

"The idea is that you press a single key and depending on how long you press it, that stroke is interpreted as a dot or a dash."

Here's a very basic implementation that does that using Tk.

The Tk::Button widget invokes its callback (the -command code) when the button is released, so that implements "depending on how long you press it"; although, quickly pressing and releasing the button, then varying the time between presses, produces the same effect.

The website you linked has a lot of other features, such as translating input into actual characters and accomodating multiple users. You didn't ask for that and, if you want one or more of those features, I'd suggest giving it a bash yourself — it'd be a good learning exercise if nothing else. I did retain the [key] label that's used there.

Also note that I just used the time function; take a look at Time::HiRes core module which provides higher resolution time functions.

#!/usr/bin/env perl use strict; use warnings; use Tk; use constant { DOT => '.', DASH => '-', BREAK => ' ', DEAD => '', DOT_MIN => 1, DASH_MIN => 2, }; my $mw = MainWindow::->new(); my $press = $mw->Button( -text => '[key]', -command => sub { print _get_char() }, )->pack(); MainLoop; { my $last_time; INIT { $last_time = time } sub _get_interval { my $now = time; my $interval = $now - $last_time; $last_time = $now; return $interval; } } { my $last_char; INIT { $last_char = DEAD } sub _get_char { my $interval = _get_interval(); my $char = $interval <= DOT_MIN ? DOT : $interval <= DASH_MIN ? DASH : BREAK; $char = DEAD if $char eq BREAK && ($last_char eq BREAK || $last_char eq DEAD); return $last_char = $char; } }

— Ken


In reply to Re: Morse input from keyboard by kcott
in thread Morse input from keyboard by pierrot

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.