Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Morse input from keyboard

by tybalt89 (Monsignor)
on Feb 11, 2020 at 06:13 UTC ( [id://11112759]=note: print w/replies, xml ) Need Help??


in reply to Morse input from keyboard

Keys can be a problem because of autorepeat, but here's one that uses the left mouse button for the keyer.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11112751 use warnings; use Time::HiRes qw( time ); use Tk; my $morse = ''; my $text = ''; my $down = 0; my $lasttime = time; my $interval = shift // 0.1; # seconds for timing my $maxlength = 30; my $mw = new MainWindow; $mw->geometry('+500+500'); $mw->Label(-text => "Use left mouse button in green window.\n" . "Tweek \$interval to your coding speed")->pack; my $t = $mw->Label(-textvariable => \$morse, -width => $maxlength, -bg => 'lightgreen', -font => 'courierbold 36', )->pack; $mw->Label(-textvariable => \$text, -width => $maxlength, -font => 'courierbold 36', )->pack; $mw->Button(-text => 'Clear', -command => sub { $morse = $text = '' }, )->pack; $mw->Button(-text => 'Exit', -command => sub {$mw->destroy})->pack; $t->bind('<1>' => \&press ); $t->bind('<ButtonRelease-1>' => \&release ); MainLoop; sub morse { $_[0] =~ tr/.-/01/r =~ s/\S+/ my $pos = oct 'b1' . $&; $pos > 30 and $pos = 30; substr '__etianmsurwdkgohvf_l_pjbxcyzq?', $pos, 1 /ger =~ s/ *\K //gr; } sub press { if( time > $lasttime + 6 * $interval ) { $morse .= ' '; } elsif( time > $lasttime + 2 * $interval ) { $morse .= ' '; } length $morse > $maxlength and substr $morse, 0, length($morse) - $maxlength, ''; $lasttime = time; } sub release { if( time < $lasttime + 2 * $interval ) { $morse .= '.'; } else { $morse .= '-'; } length $morse > $maxlength and substr $morse, 0, length($morse) - $maxlength, ''; $text = morse($morse); $lasttime = time; }

Replies are listed 'Best First'.
Re^2: Morse input from keyboard
by afoken (Chancellor) on Feb 11, 2020 at 19:25 UTC
    Keys can be a problem because of autorepeat

    There are generally several keys that are not subject to autorepeat. On A PC, both Shift keys, both Alt keys (right Alt = AltGr on international keyboards), both Ctrl keys, both Logo keys, Menu key, Shift/Caps Lock key, Scroll Lock key, Num Lock key, Pause key. Pause could be a little bit problematic because it can behave different from all other keys at the hardware level.

    Polling the Shft, Ctrl, and Alt keys should be easy even through driver, operating system and toolkit layers.

    Some systems may treat repeated key presses as shortcut for accessibility functions. Pressing shift five times makes Windows 7 (and others?) switch on "Sticky keys", keeping Num Lock pressed down for five seconds may enable "Toggle keys", keeping right shift pressed down for eight seconds enables "Filter keys". So you may want to use Ctrl or Alt on Windows instead, to prevent that from happening without reconfiguring Windows.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^2: Morse input from keyboard
by pierrot (Novice) on Feb 11, 2020 at 20:27 UTC
    This is much more than I expected. Thank you very much.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-20 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found