Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Substitution cipher or keyboard layout demo

by ohcamacj (Beadle)
on Apr 27, 2014 at 04:02 UTC ( #1083973=CUFP: print w/replies, xml ) Need Help??

As a longtime dvorak user, I've occassionally noticed that it's extremely unbalanced left-right. The right hand, does almost all of the work.

A few times, I've tried using xmodmap to create a mirrored keyboard layout; but this didn't work well. Since my typing speed falls 10x, it's always faster to just leave the keyboard layout untouched.

Wasn't there some way to practice a new keyboard layout, without constantly running

setxkbmap -layout dvorak -option ctrl:nocaps; xmodmap new-layout; setxkbmap -layout dvorak -option ctrl:nocaps; xmodmap ~/.xmodmaprc
to switch back and forth ?

So, wrote a perl script, to apply what-if-the-keyboard was a different layout transform to input.

That alone wouldn't be interesting enough to post. A trivial one-liner with s/./$map{$&} ? $map{$&} : $&/eg; is sufficient.

But, it was sorta clumsy to use. In a shell, a lot of keystrokes are necessary (up-arrow, backspace, backspace, backspace) to change the text. So, I eventually wrote a terminal ui for it, to make it easier to use.

The code

#!/usr/bin/perl -w use strict; use Getopt::Long; use Time::HiRes qw(sleep); my $inplace = 0; my $use_delay = 0; my $use_anim = 0; my $delay_time = 1.5; my $anim_time = 0.1; unless(GetOptions("inplace!" => \$inplace, "delay!" => \$use_delay, "delay-time=f" => \$delay_time, "animate!" => \$use_anim, "animation-time=f" => \$anim_time)){ print("Warning: Failed to successfully parse options.\n"); } #Initialize the lookup table my $leftside = "',.pyaoeui;qjkx\x{22}<>PYAOEUI:QJKX"; my $rightside = "fgcrldhtnsbmwvz\x{46}GCRLDHTNSBMWVZ"; sub max { my ($a, $b) = @_; $a >= $b ? $a : $b; } my %transform = (); foreach my $idx (0 .. (max(length($leftside), length($rightside)) - 1) +){ my $leftchar = substr($leftside, $idx, 1); my $rightchar = substr($rightside, $idx, 1); $transform{$leftchar} = $rightchar; $transform{$rightchar} = $leftchar; } print( "Welcome to the perl LR practice interactive shell.\n" . "Use --inplace to insert terminal escape codes.\n" . "Use quit or exit to exit\n\n" ); if($use_delay && $inplace){ printf("Replace delay set to %.2f seconds.\n", $delay_time); } if($use_anim && $inplace){ printf("Char animation frame delay set to %.3f seconds.\n", $anim_ +time); } #main loop. while(1){ my $prompt = "LR practice> "; my $ps2 = " LR result > "; # both prompts should be the same le +ngth, # so that stuff lines up vertically. syswrite(STDOUT, $prompt); my $line = <STDIN>; chomp($line); my $post_LR = perl_LR($line); if($line =~ /^(quit|exit)$/i){ syswrite(STDOUT, "Exiting.\n"); last; } if($inplace){ if($use_delay){ sleep($delay_time); } #use terminal escape codes to move up a line. #Apparently, the magic sequence is esc[1A syswrite(STDOUT, "\033[1A"); unless( $use_anim ){ #normal case syswrite(STDOUT, $ps2 . $post_LR . "\n"); }else{ my $str = $ps2 . $post_LR; $str .= " " x (length($str) >= 26 ? 1 : 27 - length($str)) +; #a little whitespace at the end makes the animation smooth +er, #particularly for very short strings. for my $idx (0 .. (length($str) - 1 )){ if($idx == 0){ }else{ #back up one char syswrite(STDOUT, "\033[1D"); } my $cur_char; if($idx == length($str) - 1){ $cur_char = substr($str, $idx, 1); }else{ $cur_char = substr($str, $idx, 1) . "#"; } syswrite(STDOUT, $cur_char); sleep($anim_time); } syswrite(STDOUT, "\n"); } }else{ syswrite(STDOUT, $ps2 . $post_LR . "\n"); } if($post_LR =~ /^(quit|exit)$/i){ syswrite(STDOUT, "Exiting.\n"); last; } } exit(0); sub perl_LR { my $outstr = ""; my $instr = shift(); foreach my $idx (0 .. (length($instr) - 1)){ my $char = substr($instr, $idx, 1); my $newchar = ""; if(defined($transform{$char})){ $newchar = $transform{$char}; }else{ $newchar = $char; } $outstr .= $newchar; } return $outstr; }

Dvorak keyboard users are rare, people who are trying to become left-handed are extremely rare. So, change $leftside and $rightside to something else, more meaningful.

Replies are listed 'Best First'.
Re: Substitution cipher or keyboard layout demo
by mr_mischief (Monsignor) on May 01, 2014 at 13:39 UTC

    Did you know Dvorak comes in balanced, left-handed, and right-handed varieties?

    Microsoft has a page that may be helpful and was near the top of results for "left-handed Dvorak keyboard". Dvorak Keyboard Layuouts

      Thanks. I tried them out briefly a very long time ago, but two-handed typing is so much faster, I lost interest almost immediately.
Re: Substitution cipher or keyboard layout demo
by ohcamacj (Beadle) on May 23, 2015 at 10:00 UTC

    After intermittent practice, the speed penalty of using an exotic custom keyboard layout has become lower. So, occassionally it's been more comfortable, or at least fun, to switch back and forth. Then realized that if switching infrequently is fun, switching even more frequently should be even more fun. Thus,

    TIME1=240; TIME2=180; set -x; while [ "a" = "a" ]; do setxkbmap -layout dvorak -option ctrl:nocaps; sleep $(perl -e "print + int rand shift" $TIME1); xmodmap ~/xmodap_for_custom_layout.txt; sleep $(perl -e "print int r +and shift" $TIME2); done

    At this point, I feel as though I've fallen into a deep, deep pit of solitary hikikomori-ness from which I may never escape.

Re: Substitution cipher or keyboard layout demo
by sierpinski (Chaplain) on Dec 19, 2014 at 04:58 UTC
    I know this is a moderately older thread, but I was catching up on CUFP and after reading this, it seems remarkably similar to the Enigma encoding machine that the German's used in WWII -- you press one key and another one actually registers... it wouldn't be terribly difficult to create random mappings based on some hash... of course you'd need a decoder as well, but that would be entirely software based since they wouldn't use the keyboard to read it.

    Very cool idea though. I've never used a Dvorak keyboard, but I might check them out sometime.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1083973]
Approved by Athanasius
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2017-07-23 18:49 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (347 votes). Check out past polls.