Xmodmap (keyboard layout customization) question

hi all. I am using 65 keyboard and the arrow key is on the down right, I kind of have to move my wrist to press the arrow key, thus I want to do some keyboard layout like

Ctrl + hjkl -> Left Down Up Right

After searching the internet, seems xmodmap is the tool to achieve it. Nixos also have a wiki regarding it keyboard layout.
I mimic the code and wrote down the following:

{ config, lib, pkgs, ... }:
let
    myCustomLayout = pkgs.writeText "xkb-layout" ''
      keycode 37 = Mode_switch
      keysym h = h H Left
      keysym l = l L Right
      keysym k = k K Up
      keysym j = j J Down
    '';
in {
  services.xserver.displayManager.sessionCommands = "${pkgs.xorg.xmodmap}/bin/xmodmap ${myCustomLayout}";
}

My question is:

  1. In the very bottom the wiki mentioned suspend/resume, suspend and resume what? xserver? what’s the command should I use?
  2. the suspend/resume needs to be done every time I restart the computer? or start a new window?
    is there any solution which can be done once and forever?

Thanks!!

It refers to suspending the machine to RAM (i.e. sleep mode). It says that the solution works even after resuming from sleep (unlike some other methods, which require manually loading the custom layout).

thanks for the explanation. Seems I misunderstood the wiki…

if that’s the case, then seems this is exactly what I want, need to explore why it does not work though…

You might need to log out and log in after running nixos-rebuild switch for the command to run.

tried restart, no luck probably i3’s issue?

What happens if you store the xkb-layout into a file and run xmodmap on it manually?

Xmodmap is configuring the x server, so logging in and out i3 won’t be enough. I would pull the plug with

$ sudo killall X

or reboot if necessary. I assume you are still running a graphic environment when logged out of i3.

I rebooted since the sessionCommands means

      services.xserver.displayManager.sessionCommands
           Shell commands executed just before the window or desktop manager is started. These commands are not currently sourced for Wayland sessions.

however, the xmodmap does not work even after reboot

good idea! I first tried

xmodmap -e "kecode 64 = Mode_switch"

in the terminal, and use command

xmodmap -pke

to print the keymap table and check if xmodmap command works or not.

If using xmodmap in command line, the cusomization keymap works, and I can using Mode_switch + hjkl as arrows.

However, the

{ config, lib, pkgs, ... }:
let
    myCustomLayout = pkgs.writeText "xkb-layout" ''
      keycode 37 = Mode_switch
      keysym h = h H Left
      keysym l = l L Right
      keysym k = k K Up
      keysym j = j J Down
    '';
in {
  services.xserver.displayManager.sessionCommands = "${pkgs.xorg.xmodmap}/bin/xmodmap ${myCustomLayout}";
}

does not work.
I tried to mimic the command line command by:

{ config, lib, pkgs, ... }:
let
  myRemap = ''
      keycode 64 = Mode_switch
    '';
in {
  services.xserver.displayManager.sessionCommands = "${pkgs.xorg.xmodmap}/bin/xmodmap -e ${myRemap}";
}

which I believe is exactly the same with

xmodmap -e "keycode 64  = Mode_switch"

in the command line?

sudo nixos-rebuild switch

and reboot, the keymap does not take effect…

I managed to get xmodmap works on sessionCommands by adding delay before it.

sleep 5 && xmodmap -e 'keysym Alt_R = Mode_switch' &
sleep 5 && xmodmap -e 'keycode 43 = h H Left H' &