Automatic detection of keyboard layout

Michael Neumann mneumann at ntecs.de
Thu Dec 25 02:06:45 PST 2025


On Wed, Dec 24, 2025 at 11:07:00PM +0000, Kate wrote:
> Hi,
> 
> Thanks for the reply,
> I forgot to mention but i'm using USB keyboards.
> 
> Is there a way to modify that keymap value on the fly from the dragonfly
> boot menu? (although i haven't tried but that might be the same story there,
> in which case this wouldn't be useful)

Within the boot loader itself, I think you are stuck with whatever
keymap is configured in your UEFI BIOS, but...

...you can add a boot menu item that sets a kernel environment
variable, which you then later read out and use it to configure your
keyboard. That should be fairly easy.

At first you need a boot menu entry. Look at "man loader", "man
loader.conf" and /boot/defaults/dloader.menu. We used to have a Forth
boot loader inherited from FreeBSD, but dillon rewrote it in C. You'll
find it in /usr/src/stand/boot/dloader, but you don't have to touch it
as you should be able to do everything by configuration.

Just copy /boot/defaults/dloader.menu to /boot/dloader.menu and make
your changes there. You'd just need another "menuitem" with your
custom "menuadd set YOURVARIABLE=uk" line there.

You can read out the kernel environment from userland via "kenv". For
example:

	$ kenv kernelname
	# => /boot/kernel/kernel

If you put "kbdmap=uk" into /boot/loader.conf (or dloader.menu), "kenv
kbdmap" will give you "uk".

You could then just modify your /etc/rc.conf like this:

	if [ "$(kenv kbdmap)" = "uk" ]; then
	  keymap="uk.iso"
	elif [ "$(kenv kbdmap)" = "us" ]; then
	  keymap="us.iso"
	fi

You get the idea.

A better approach would be to add the related code to /etc/rc.d/syscons,
because /etc/rc.conf is sourced into all /etc/rc.d scripts and you
generally don't want to run too much code from within rc.conf itself.

For example, you could add a flag "keymap_kenv=YOUR-KENV-VARIABLE" flag
to /etc/rc.conf and from within /etc/rc.d/syscons check whether it is
nonempty, in which case you'd gather the "keymap" from "kenv
${keymap_kenv}" in addition to whatever is set in rc.conf. 

Feel free to come up with a pull request if you like (we don't use
github officially, but you could create a PR there...). Also note
that most DragonFly developers hang out on our IRC channel. The
mailing list is rather silent :)

Happy X-mas / hacking,

  Michael

> 
> Cheers,
> Kate
> 
> On 12/24/25 22:52, Michael Neumann wrote:
> > Hi Kate,
> > 
> > I doubt it is technically possible to detect keyboard layout, at least for PS/2 keyboards, USB keyboards might be a different story. As far as I know, the PS/2 keyboard just sends "scancodes" which basically just tells us which key in the keyboard matrix was pressed, but not which character that key corresponds to, as that depends on the keycap which you can easily replace :).
> > 
> > What you can do is to configure the keyboard layout in /etc/rc.conf via keymap="uk.iso.kbd" for UK or keymap="us.iso" for US - I use the latter to type on a japanese keyboard and struggle with umlauts :) . You can also run kbdmap to configure your keyboard layout.
> > For X11, there also is setxkbmap.
> > 
> > Best regards,
> > 
> > Michael
> > 
> > 
> > 
> > Am 24. Dezember 2025 17:52:08 MEZ schrieb Kate <kit-ty-kate at exn.st>:
> > > Hi,
> > > 
> > > I installed 6.4.2 a few months ago on subpartition[1] of a shared machine.
> > > At the time, the keyboard i used to install it was a UK type keyboard and it worked just fine.
> > > 
> > > However since, we've changed the keyboard to a US type layout and now the letters are all jumbled up and it is impossible to log-in.
> > > Dusting off the old keyboard it works again but it would be nice if both worked out of the box.
> > > 
> > > Is there a way to enable some sort of automatic detection of the keyboard layout?
> > > 
> > > [1]: using the following installation instructions: https://www.dragonflybsd.org/docs/handbook/Installation/#index7h1
> > > 
> > > Cheers,
> > > Kate
> 

-- 
Michael Neumann
NTECS Consulting
www.ntecs.de


More information about the Users mailing list