console-keymap-switching.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="generator" content="Hugo 0.92.0" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="/assets/css/theme.css">
  8. <link rel="alternate" href="/rss.xml" type="application/rss+xml" title="Pleasant Programmer">
  9. <script type="text/javascript" src="//use.typekit.net/iwm5axp.js"></script>
  10. <script type="text/javascript">try{Typekit.load();}catch(e){}</script>
  11. <title>Console Keymap Switching - Pleasant Programmer</title>
  12. </head>
  13. <body>
  14. <header id="header" role="banner">
  15. <div id="thomas">
  16. <img src="/assets/img/thomas.gif" alt="DJ THOMAS IN DA HAUS">
  17. <img src="/assets/img/thomas.png" alt="Pleasant Programmer">
  18. </div>
  19. <h1 class="site-title"><a href="/">Pleasant Programmer</a></h1>
  20. <nav id="menu" role="navigation">
  21. <ul>
  22. <li><a href="/pages/projects.html">projects</a></li>
  23. <li><a href="/posts.html">archives</a></li>
  24. <li><a href="/tags.html">tags</a></li>
  25. <li><a href="/rss.xml">rss</a></li>
  26. </ul>
  27. </nav>
  28. </header>
  29. <div id="container">
  30. <main id="content" role="main">
  31. <article itemscope itemtype="http://schema.org/BlogPosting">
  32. <h1 class="p-name entry-title" itemprop="headline name">
  33. <a href="/posts/console-keymap-switching.html">Console Keymap Switching</a></h1>
  34. <small>
  35. <span class="dateline">Posted: <time itemprop="datePublished" datetime="2013-10-29">2013-10-29</time></span>
  36. | More posts about
  37. <a class="tag p-category" href="/tags/sysadmin.html" rel="tag">
  38. sysadmin
  39. </a>
  40. <a class="tag p-category" href="/tags/systemd.html" rel="tag">
  41. systemd
  42. </a>
  43. </small>
  44. <div class="e-content entry-content" itemprop="entry-text">
  45. <p>At the office, we have some people who use DVORAK. Normally, this isn&rsquo;t a problem. To each his own after all. It does become a bit problematic though, when we&rsquo;re dealing with the servers around the office.</p>
  46. <p>We normally leave the servers on QWERTY. After all, most people start off as QWERTY typists and migrate to something else. That said, it&rsquo;s apparently difficult to stay fluent in both. People tend to forget how to type in QWERTY once they learn DVORAK or something else. While it is true that they can just look a the keyboard while typing, my coworkers would prefer it to just be in DVORAK.</p>
  47. <p>For the console, they&rsquo;d typically do <code>sudo loadkeys dvorak</code> after logging in. The problem with this is, after they logout, the keymapping is still on DVORAK. This has been quite annoying for a few times since I can&rsquo;t even login to change the keymap. What I wanted was something like you get in the graphical login screens where you can pick your keymap before logging in. Apparently, there isn&rsquo;t a readily available thing for the console.</p>
  48. <p>I googled around for solutions and came across <a href="http://superuser.com/questions/548234/how-can-i-easily-toggle-between-dvorak-and-qwerty-keyboard-layouts-from-a-linux">a nice idea</a>. You could alias <code>asdf</code> to load the DVORAK mapping and <code>aoeu</code> (the equivalent to asdf in DVORAK) to load the QWERTY mapping. This actually makes sense since you don&rsquo;t really have to know where the letters are. The only problem is, you once again have to be logged in to change the key mappings.</p>
  49. <p>After some further searching, I found <a href="http://unix.stackexchange.com/questions/2884/toggle-between-dvorak-and-qwerty">something close to what I wanted</a>. Apparently, Alt+Up sends a KeyboardSignal keycode to the init process, which can act on that. It also works anywhere, even before being logged in. For SysVinit systems, you can just add a line to your inittab for a command to be run when Alt+Up is pressed.</p>
  50. <p>In the office, however, we generally use Arch Linux which uses SystemD. But apparently, it also has a mechanism of accepting the Alt+Up press. It runs the kbrequest target whenever it gets the keypress. <code>kbrequest.target</code> is normally aliased to run the rescue service though, so you have to manually create the file in <code>/etc/systemd/system/kbrequest.target</code> and fill it with a description:</p>
  51. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ini" data-lang="ini"><span style="color:#fff;font-weight:bold">[Unit]</span>
  52. <span style="color:#007f7f">Description</span>=<span style="color:#0ff;font-weight:bold">kbrequest target</span>
  53. </code></pre></div><p>We can then add a service to be run whenever the target is called. Something like <code>/etc/systemd/system/keymap-switch.service</code>:</p>
  54. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ini" data-lang="ini"><span style="color:#fff;font-weight:bold">[Unit]</span>
  55. <span style="color:#007f7f">Description</span>=<span style="color:#0ff;font-weight:bold">Keymap Switch Service</span>
  56. <span style="color:#fff;font-weight:bold">[Service]</span>
  57. <span style="color:#007f7f">Type</span>=<span style="color:#0ff;font-weight:bold">oneshot</span>
  58. <span style="color:#007f7f">ExecStart</span>=<span style="color:#0ff;font-weight:bold">/usr/local/bin/keymap-switch</span>
  59. <span style="color:#fff;font-weight:bold">[Install]</span>
  60. <span style="color:#007f7f">WantedBy</span>=<span style="color:#0ff;font-weight:bold">kbrequest.target</span>
  61. </code></pre></div><p>After enabling said service, we only need the actual keymap switcher, <code>/usr/local/bin/keymap-switch</code>. The StackOverflow answer provides different ways of detecting the current keymap so we know which one to switch to. Since we&rsquo;re using SystemD, we can use that instead for managing which keymap we&rsquo;re actually using. It stores the current settings inside <code>/etc/vconsole.conf</code>. We can also then switch keymaps by using <code>localectl set-keymap</code>.</p>
  62. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"><span style="color:#0f0;font-weight:bold">#!/bin/sh
  63. </span><span style="color:#0f0;font-weight:bold"></span><span style="color:#fff;font-weight:bold">source</span> /etc/vconsole.conf
  64. <span style="color:#fff;font-weight:bold">if</span> [ <span style="color:#0ff;font-weight:bold">&#34;</span>$TERM<span style="color:#0ff;font-weight:bold">&#34;</span> = <span style="color:#0ff;font-weight:bold">&#34;dumb&#34;</span> ]; <span style="color:#fff;font-weight:bold">then</span>
  65. <span style="color:#fff;font-weight:bold">if</span> [ <span style="color:#0ff;font-weight:bold">&#34;</span>$KEYMAP<span style="color:#0ff;font-weight:bold">&#34;</span> = <span style="color:#0ff;font-weight:bold">&#34;dvorak&#34;</span> ]; <span style="color:#fff;font-weight:bold">then</span>
  66. localectl set-keymap us
  67. <span style="color:#fff;font-weight:bold">else</span>
  68. localectl set-keymap dvorak
  69. <span style="color:#fff;font-weight:bold">fi</span>
  70. <span style="color:#fff;font-weight:bold">fi</span>
  71. </code></pre></div><p>After putting it all together, it works! We can switch keymaps on the fly by simply pressing Alt+Up.</p>
  72. </div>
  73. <aside class="postpromonav">
  74. <nav>
  75. <ul class="pager clearfix">
  76. <li class="previous">
  77. <a href="/posts/geocoding-services.html" rel="prev" title="Geocoding Services">&larr; Previous post</a>
  78. </li>
  79. <li class="next">
  80. <a href="/posts/removing-pldtmydslbiz-from-the-zyxel-p-2612hnu.html" rel="next" title="Removing PLDTMyDSLBiz from the ZyXEL P-2612HNU">Next post &rarr;</a>
  81. </li>
  82. </ul>
  83. </nav>
  84. </aside>
  85. <section class="comments">
  86. <script
  87. data-isso="https://isso.pleasantprogrammer.com/"
  88. data-isso-require-author="true"
  89. data-isso-vote="false"
  90. src="https://isso.pleasantprogrammer.com/js/embed.min.js">
  91. </script>
  92. <section id="isso-thread"></section>
  93. </section>
  94. </article>
  95. </main>
  96. <footer id="footer" role="contentinfo">
  97. <p>
  98. <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/deed.en_US">
  99. <img alt="CC-BY-SA" style="border-width:0" src="https://licensebuttons.net/l/by-sa/3.0/80x15.png">
  100. </a> &copy; 2022 Thomas Dy - Powered by <a href="http://gohugo.io">Hugo</a></p>
  101. </footer>
  102. </div>
  103. <script src="/assets/js/konami.js"></script>
  104. <script>
  105. var easter_egg = new Konami();
  106. easter_egg.code = function() {
  107. var el = document.getElementById('thomas');
  108. if(el.className == "whoa") {
  109. el.className = "";
  110. }
  111. else {
  112. el.className = "whoa";
  113. }
  114. document.body.scrollTop = document.documentElement.scrollTop = 0;
  115. }
  116. easter_egg.load();
  117. </script>
  118. </body>
  119. </html>