Solving the problem with switching by alt + shift in Linux, in applications on Electron

Hello colleagues!

I want to share my solution to the problem that is indicated in the title. Writing an article inspired by a colleague brnovk, who was not too lazy and offered a partial (for me) solution to the problem. I made my "crutch" that helped me. I share with you.

Description of the problem

I was using Ubuntu 18.04 for work and recently I noticed that when switching the layout with alt + shift in applications such as Visual Studio Code, Skype, Slack and others that are created with Electron, the following problem arises: focus from the input field goes to the top bar of the window (menu). For other reasons, I moved to Fedora + KDE and realized that the problem had not gone away. In search of a solution, I found a wonderful article Do-it-yourself Skype repair. Many thanks comrade brnovkthat he spoke in detail about the problem and shared his way of solving it. But the method indicated in the article closed the issue with only one application, namely Skype. It was also critical for me to deal with Visual Studio Code, because writing messages with a jumping menu is annoying, but not so much if you are developing. Plus, a colleague suggested a solution where the application menu disappears completely, and I would not really like to lose the menu in VS Code.

Tried to figure out what's going on

So, I decided to take the time to figure out what was going on. Now I will briefly describe which way I went, maybe someone more savvy in this matter will help clarify the difficulties that I encountered.

I opened up Visual Studio Code and started pressing different combinations of Alt+<%something%>, looking at how the application reacted. In almost all cases, all combinations, except for Alt + Shift, worked out without losing focus. It seemed that someone was eating the pressed Shift, which followed after holding and holding Alt, and the application thinks that I pressed Alt, then did not press anything, released Alt and it joyfully threw my focus into its menu, which seemed quite logical to him.

I opened the keyboard layout switching settings (you know, this long list with checkboxes and all sorts of settings for the keys) and set the keyboard layout switching to the Alt button, without any additional clicks.

Solving the problem with switching by alt + shift in Linux, in applications on Electron

After that Alt+Tab stopped working for switching windows. Only Tab worked, that is, someone "ate" my Alt again. Who this β€œsomeone” was no longer questions, but I have no idea what can be done with him.

But since the problem had to be solved at least somehow, then the solution came to mind:

  1. In the settings, disable the hotkey for switching keyboard layouts (clear all checkboxes in the Switch to another layout section);
  2. Create your own hotkey that would switch my layout

Solution Description

First, let's install a program that allows you to assign commands to Xbindkeys. Unfortunately, the regular tools did not allow me to create a hotkey for a combination of the Alt + Shift type through a beautiful interface. Can be done for Alt+S, Alt+1, Alt+shift+Y etc. etc., but this is not suitable for our task.

sudo dnf install xbindkeysrc

More details about it are on ArchWiki
Next, let's create a sample settings file for the program. The sample is quite short, with a few commands just what you need to figure out how to work with it:

xbindkeys -d > ~/.xbindkeysrc

As you can see from the example that is in the file, we need to specify the hotkey that we want to use and the command that should be executed. Looks simple.


# Examples of commands:
"xbindkeys_show"
  control+shift + q
# set directly keycode (here control + f with my keyboard)
"xterm"
  c:41 + m:0x4

As a hotkey, you can use human-readable spelling or use key codes. It worked for me only with codes, but no one forbids you to experiment a little.

To get the codes you need to use the command:

xbindkeys -k

A small "X" window will open. You only need to press the keys when focusing on this window! Only in this case you will see something like this in the terminal:


[podkmax@localhost ~]$ xbindkeys -k
Press combination of keys or/and click under the window.
You can use one of the two lines after "NoCommand"
in $HOME/.xbindkeysrc to bind a key.
"(Scheme function)"
    m:0x4 + c:39
    Control + s

In my case, the combination for the Alt + Shift keys looks like this:

m:0x8 + c:50

Now you need to make sure that when you click on this combination, the layout is switched. I found only one working command to specify the layout:


setxkbmap ru
setxkbmap us

As you can see from the example, she can only turn on one or another layout, so nothing other than writing a script came to my mind.


vim ~/layout.sh
#!/bin/bash
LAYOUT=$(setxkbmap -print | awk -F + '/xkb_symbols/ {print $2}')
if [ "$LAYOUT" == "ru" ]
        then `/usr/bin/setxkbmap us`
        else `/usr/bin/setxkbmap ru`
fi

Now if the .xbindkeysrc and layout.sh files are in the same directory, then the final form of the .xbindkeysrc file looks like this:


# Examples of commands:

"xbindkeys_show"
  control+shift + q

# set directly keycode (here control + f with my keyboard)
"xterm"
  c:41 + m:0x4

# specify a mouse button
"xterm"
  control + b:2
#А Π²ΠΎΡ‚ Ρ‚ΠΎ, Ρ‡Ρ‚ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΠ» я
"./layout.sh"
  m:0x8 + c:50

After that, apply the changes:


xbindkeys -p

And you can check. Do not forget to disable any options for switching layouts in the standard settings.

Π‘onclusion

Colleagues, I hope that this article can help someone quickly get rid of an annoying problem. Personally, I spent my whole day off trying to figure out and solve the problem somehow, so that I would no longer be distracted by this during working hours. I wrote this article in order to save someone time and nerves. Many of you use an alternative way to switch layouts and do not understand what the problem is. I personally like to toggle with Alt+Shift. And I want it to work that way. If you share my opinion and are faced with this problem, this article should help you.

Source: habr.com

Add a comment