Remap/rebind keys

Hello,
I had a very simple script for AutoHotKey under Windows which mapped the arrow keys to WASD and similar. I found out that there is an imitation for Linux called ahk_x11. So I installed ahk_x11 from the AUR and it seems to work with my script too (I had to adapt it a little, or rather delete the entry #IfWindowActive, as the function works differently in akh_x11). But now there is a problem. If you press two keys at the same time, e.g. W+D to navigate to the top right, one key remains stuck or pressed, as if it doesn’t recognize when the key has been released. This makes it impossible to play in the heat of the moment. After some searching, I found many supposed solutions. I often read about xdotool, xinput, xbindkeys and xremap. But now I’m desperate to implement it. I thought (after my research) xremap would be the easiest way to do it.
What should happen:

  • The script should load automatically (it doesn’t have to, I can click on it every time before I start the game but it would be nice to have).
  • It should only be active when the game is running.
  • It should remap keys without any hangs or bugs!
  • In the game you can open the chat with enter and send the message with enter, so (in the Windows version with AutoHotKey) I suppressed the script when you pressed enter to open the chat and be able to write “normally” and activated it again when you pressed enter again to send the message. That worked great and should also work with the new solution.
    Can you please help me implement it like this.

Info:
KDE Plasma @ x11
EDIT: I forgot to mention I am on Nvidia

echo $XDG_SESSION_TYPE                                                       ✔ 
x11

wmctrl -x -l                                                                 ✔ 
...
0x09a00003  0 steam_app_231430.steam_app_231430  tom-pc Company Of Heroes 2

sudo xremap ~/.xremap_remap.yml                                            1 ✘ 

Selecting devices from the following list:
------------------------------------------------------------------------------
...
/dev/input/event3 : Logitech K400 Plus
...
------------------------------------------------------------------------------
Selected keyboards automatically since --device options weren't specified:
------------------------------------------------------------------------------
Error: Failed to prepare input devices: No device was selected!

AutoHotKey script:

#if !GetKeyState("LCtrl", "P") && WinActive("Company Of Heroes 2")

w::up
a::left
s::down
d::right
e::t
f::d
q::u
c::n
x::Numpad0

~Enter::Suspend
+~Enter::Suspend
return

Maybe it would help to set a higher priority to the ahk_x11 process, I don’t know.

And just to clarify. No, it is not possible to set the hotkeys in the menu of Company of Heroes 2. This is a huge flaw for a real-time strategy game but otherwise it is one of the best RTS games.

There are various ways to remap keys, but most are designed to be more permanent.

Most find the input remapper utilities to be the easiest, I haven’t used them myself so I’m not sure if they’d be suitable for your use case.

https://wiki.archlinux.org/title/Input_remap_utilities

xmodmap might be ok for this (as you’re using X11) but it’s been deprecated for a long time, and it’s been ages since I’ve used it. I prefer custom layouts myself.


For this I might suggest a custom keyboard layout, as you can switch between layouts easily with a keyboard shortcut and it’ll also work on Wayland (unlike xmodmap).

I can make one for you, if you tell me which layout you’re using and clarify a couple of things (as I’ve never used autohotkey or ahk_x11).

Just to be clear you’re saying you want w to act as the up arrow key?

Not sure how to interpret that one.

Yes, thats right.

As I wrote above
Press enter surpessed the script, so you can write normal in the chat (without the remapped letters) and press enter again activate the script

Ah yes, I clearly missed that rather important requirement. :man_facepalming:

:thinking:

it should work somehow like this (I have no programming knowledge) :

var remap_is_active = true

If enter_is_pressed:
    remap_is_active = ! remap_is_active

If remap_is_active & WindowIsActive ("name_of_the_application"):
    w = arrow_up
    d = arrow_down
    .... etc.
else:
    w = w 
    d = d
    .... etc.

or maybe (I really don’t know):

If enter_is_pressed:
    remap_is_active = ! remap_is_active

If remap_is_active & WindowIsActive ("name_of_the_application"):
    start xremap.my_script
else:
    kill xremap #or whatever tool is used

I think this could be realized with a combination of xdotool and xremap but I just guess

Just tested this idea (using a custom keyboard layout) with a native game (urban-terror), and it worked there. I don’t know if it’ll work with a steam game, but I’d say it’s worth a try.

It’s not quite the same, there’d be no check to see if the game is running. Just a key or key combo to switch keyboard layout manually and it wouldn’t be Enter (well it could be Enter to turn if off, but turning it back on would require a different key or a combo otherwise you wouldn’t have an Enter key anymore).

Would that be acceptable?

I’d still need to know what keyboard layout you use (for instance I use English UK also known as gb).

Perhaps, but I’ve barely used xdotool and never used xremap. Not sure you’d need both, but I’d have to research it to know for sure.

EDIT:

If you can use global keyboard shortcuts within the game then we may be able to check if the game is running.

I have no idea about custom keyboard layouts and I also don’t know how to use it.
I’m new to Linux in general

EDIT:
I use a german layout QWERTZ

As I said I can make one, and of course tell you how configure it.

I use a german layout QWERTZ
I edited my post above

Wow there are quite a few variants, could you provide the output from this command please:

setxkbmap -print
setxkbmap -print                                                 ✔ 
xkb_keymap {
        xkb_keycodes  { include "evdev+aliases(qwertz)" };
        xkb_types     { include "complete"      };
        xkb_compat    { include "complete"      };
        xkb_symbols   { include "pc+de+inet(evdev)"     };
        xkb_geometry  { include "pc(pc104)"     };
};

1 Like

It’ll take me a little while.

1 Like

Thank you for your effort

1 Like

Have a look at

System Settings → Keyboard → Keyboard → Key Bindings → Switching to another layout

See which key or combo you’d like to use, if you want something that isn’t there I may be able to do that too.

In fact if you choose an option, and add another layout for testing you could see if this will work. If you can switch layouts in your game it should work.

1 Like

How can I post an image. I get an error: You can not embad an image in the post

It’s because you’re a new user, welcome by the way. :slight_smile:

You can use a site like this.

Paste the link, then select it and click the </> icon

which will give this:

https://imgur.com/upload

For more information:

It’s bad that you have to create an account everywhere these days. :frowning:

https://imgur.com/MkwM3fU

Indeed it is.

It would help if I could read German, but yes that seems to be the one. Just any existing layout that you can distinguish inside the game, for instance when typing a message.

OK, I set german layout QWERTZ on ctrl*F11 and english layout QWERTY on ctrl+F12
And it works!
So that would be a possible workaround for now. But before you make too much effort, I’ll be honest. I would prefer a solution with a script as mentioned at the beginning.
But this idea is better than nothing at least for now