Solving the problem of switching over alt + shift in Linux, in applications on Electron

Hello colleagues!



I want to share to share my solution to the problem, which is indicated in the header. The article was inspired by a colleague brnovk , who was not too lazy and proposed a partial (for me) solution to the problem. I made my “crutch” that helped me. I share with you.



Description of the problem



Used Ubuntu 18.04 to work and recently noticed that when switching layouts by alt + shift in applications such as Visual Studio Code, Skype, Slack and others that were created using Electron, the following problem arises: the focus from the input field goes to top panel of the window (menu). For other reasons, I moved to Fedora + KDE and realized that the problem did not go away. In search of a solution, I found a wonderful article Do it yourself Skype . Many thanks to comrade brnovk for explaining in detail about the problem and sharing his way to solve it. But the method specified in the article covered the question with only one application, namely Skype. It was still 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 proposed a solution in which the application menu disappears completely, but I would not really like to lose the menu in VS Code.



I tried to understand what was the matter



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



I opened Visual Studio Code and started clicking on different combinations Alt + <% something%>, looking at the reaction of the application. In almost all cases, all combinations, except Alt + Shift, worked without loss of focus. It seemed that someone eats 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 logical to him.



I opened the keyboard layout switch settings (well, you know this long list with checkboxes and all kinds of settings for keys) and set the keyboard layout switch for the Alt button, without any additional presses.



image



After that Alt + Tab stopped working to switch windows. Only Tab worked, that is, someone again “ate” my Alt. Who this “someone” had no questions left, 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, turn off the hotkey to switch the keyboard layout (remove all checkboxes in the Switch to another layout section);
  2. Create your own hotkey to switch my layout


Solution Description



First, install a program that allows you to assign commands to the Xbindkeys keys. Unfortunately, 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, create a sample settings file for the program. The sample is quite short, with just 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. It 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 with the focus 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 keyboard shortcut Alt + Shift looks like this:



 m:0x8 + c:50
      
      





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



 setxkbmap ru setxkbmap us
      
      





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



 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.



Total



Colleagues, I hope that this article can help someone quickly get rid of the annoying problem. Personally, I spent my entire day off sorting out and solving the problem somehow, so as not to be distracted by this anymore 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 switch 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.



All Articles