<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Henrique Sousa</title>
    <description>The latest articles on DEV Community by Henrique Sousa (@hensou).</description>
    <link>https://dev.to/hensou</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F200586%2F6558e541-9cc1-40e6-9950-6cc0c11b6b7d.png</url>
      <title>DEV Community: Henrique Sousa</title>
      <link>https://dev.to/hensou</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hensou"/>
    <language>en</language>
    <item>
      <title>How to set the keyboard layout for a specific device</title>
      <dc:creator>Henrique Sousa</dc:creator>
      <pubDate>Fri, 10 Jun 2022 13:47:39 +0000</pubDate>
      <link>https://dev.to/hensou/how-to-set-keyboard-layout-for-a-specific-device-4hhd</link>
      <guid>https://dev.to/hensou/how-to-set-keyboard-layout-for-a-specific-device-4hhd</guid>
      <description>&lt;p&gt;I have laptop with a Portuguese (PT) keyboard layout and recently I've bought a Keychron K2 which has an US layout. I find it annoying to have to change the keyboard layout to whatever keyboard I'm currently using so I started to try ways to set each keyboard it's proper layout automatically. &lt;/p&gt;

&lt;p&gt;After a lot of struggles trying to understand X11 config files I think I finally got it, and it all comes down to the creation of a &lt;code&gt;01-keyboard.conf&lt;/code&gt; file at &lt;code&gt;/etc/X11/xorg.conf.d/&lt;/code&gt; folder, with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Section "InputClass"
        Identifier "Keychron_K2"
        MatchVendor "Keychron"
        MatchProduct "Keychron K2"
        MatchIsKeyboard "on"
        Option "XkbLayout" "us"
        Option "XkbVariant" ",intl"
        Option "XkbOptions" "compose:ralt"
EndSection
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this configuration file X11, the thing that handles Monitors, Keyboards, Pointing and Touch Devices, is able to proper configure the keyboard layout whenever I have it plugged in.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is happening here?
&lt;/h2&gt;

&lt;p&gt;Whenever you plug a device to a Linux machine, its driver gets loaded by the kernel. You can check the status of that loading processing, and its messages,  by running the &lt;code&gt;dmesg&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Running it on my machine gave me the following results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[...] usb 3-1: new full-speed USB device number 6 using xhci_hcd
[...] usb 3-1: New USB device found, idVendor=05ac, idProduct=024f, bcdDevice= 1.12
[...] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[...] usb 3-1: Product: Keychron K2
[...] usb 3-1: Manufacturer: Keychron
[...] input: Keychron Keychron K2 as /devices/.../usb3/.../input/input33
[...] apple 0003:05AC:024F.000C: input,hidraw0: USB HID v1.11 Keyboard [Keychron Keychron K2] on usb-0000:04:00.3-1/input0
[...] apple 0003:05AC:024F.000D: Fn key not found (Apple Wireless Keyboard clone?), disabling Fn key handling
[...] input: Keychron Keychron K2 as /devices/.../usb3/.../input/input34
[...] apple 0003:05AC:024F.000D: input,hiddev98,hidraw5: USB HID v1.11 Keyboard [Keychron Keychron K2] on usb-0000:04:00.3-1/input1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, &lt;code&gt;dmesg&lt;/code&gt; shows details about the device and its manufacturer. From here, it is just a matter of leveraging that information to specify which settings we want for your keyboard.&lt;br&gt;
And for that you need to create a X.org configuration file.&lt;/p&gt;

&lt;p&gt;You can check the &lt;a href="https://www.x.org/releases/current/doc/man/man5/xorg.conf.5.xhtml"&gt;X.org conf documentation&lt;/a&gt; to better understand how it works but, for simplicity, think of it as a way to  select a device, based on its characteristics and features, to be able to a specify configuration or command for that set of devices.&lt;/p&gt;

&lt;p&gt;The are a lot of possible settings for those Section file, but for the keyboard layout, I think the following are enough:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Section "InputClass"&lt;/strong&gt;
The &lt;strong&gt;InputClass&lt;/strong&gt; indicates that we are about to specify configurations for a input device.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identifier&lt;/strong&gt;
This entry is required and it should be an unique name. Kind of like an &lt;strong&gt;Id&lt;/strong&gt; for this &lt;strong&gt;InputClass&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MatchVendor&lt;/strong&gt;
This entry tries to match the device vendors name with the specified name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MatchProduct&lt;/strong&gt;
This entry tries to match the device product name with the specified name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MatchIsKeyboard&lt;/strong&gt;
This is entry matches only if the device type is a keyboard.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Option "&lt;code&gt;&amp;lt;OptionName&amp;gt;&lt;/code&gt;" "&lt;code&gt;&amp;lt;OptionValue&amp;gt;&lt;/code&gt;"&lt;/strong&gt;
These entries basically defines options for the xkb command, that should be executed for all of the matched devices. You can specify any option or layout that &lt;code&gt;xkb&lt;/code&gt; accepts.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After creating that file you are pretty much done. Just logout or reboot you computer and it should assign the specified layout to each device you've identified.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://wiki.gentoo.org/wiki/Xorg.conf"&gt;Xorg Conf&lt;/a&gt;&lt;br&gt;
&lt;a href="https://fedoraproject.org/wiki/Input_device_configuration"&gt;Input Device Configuration&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.x.org/releases/current/doc/man/man5/xorg.conf.5.xhtml"&gt;Input Class Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://wiki.gentoo.org/wiki/Compose_key"&gt;Compose key&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.charvolant.org/doug/xkb/html/index.html"&gt;Xkb Configuration&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>linux</category>
      <category>x11</category>
      <category>windowmanager</category>
    </item>
  </channel>
</rss>
