Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Morse input from keyboard

by kcott (Archbishop)
on Feb 11, 2020 at 07:21 UTC ( [id://11112760]=note: print w/replies, xml ) Need Help??


in reply to Morse input from keyboard

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

Replies are listed 'Best First'.
Re^2: Morse input from keyboard
by Anonymous Monk on Feb 11, 2020 at 09:45 UTC

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

    Tk provides already as  $Tk::event->t

Re^2: Morse input from keyboard
by Anonymous Monk on Feb 11, 2020 at 09:39 UTC

    $mw->Button(-text => 'Exit', -command => sub {$mw->destroy})->pack;

    no need for closures

    -command => sub { $Tk::widget->toplevel->destroy }

    -command => 'exit'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11112760]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found