The great JetBrains IDEs (IDEA et al.) assign pretty much every conceivable keyboard shortcut to some function. While mildly overwhelming at times, it also makes for efficient use.
My problem is that Unity assigns some of these shortcuts as well, and they take precedence. One particularly annoying example is CTRL + ALT + L. The issue has been explored before here.
However, neither of the approaches is satisfactory.
- Turning off system shortcuts globally impedes my overall productivity with the system.
- Switching to a different keymap in IDEA will confuse the hell out of me when I develop on different platforms (and have to choose different mappings).
Is there a way to turn off system shortcuts only when a certain application is active, i.e. running and in focus?
I'd be willing to run a script every time I launch the application.
Best Answer
How to automatically disable multiple (specific) shortcuts if (and as long as) a specific application's window is active
The script below will disable specific key shortcuts when an arbitrary application's window is active.
Although you mentioned""I'd be willing to run a script every time I launch the application.", There is no reason to kill the script afterwards, it is extremely low on juice.
The script
How to use
The script needs
xdotool
:Copy the script into an empty file, save it as
disable_shortcuts.py
In the head of the script, replace in the line:
"gedit" by your application, meaning: the process name that owns the window.
Test-run the script by the command:
If all works fine, add it to Startup Applications: Dash > Startup Applications > Add. Add the command:
Adding more shortcuts to be disabled
As an example, I added the shortcut you mentioned: CTRL + ALT + L. Shortcuts are set in the
dconf
database, and can be set or disabled usinggsettings
.In the script, these
gsettings
entries are set in the function:setkeys()
An example to add (disabling) the log out shortcut:
dconf watch /
Re-set the shortcut to itself. In the terminal, you can see the
gsettings
key that belongs to the shortcut:Now we have to add the found key (in a slightly different appearance):
...to the "keys" list in our function:
Now both CTRL + ALT + L and CTRL + ALT + Delete are disabled if your application is in front.
Explanation
As mentioned, shortcuts, like the ones you mention, are set in the
dconf
database. In the example CTRL + ALT + L, the key to set or edit the schortcut is:To disable the key, the command is:
To reset the key to its default value:
The script looks once per second if:
again (only) if so, it disables the shortcuts, listed in
...waiting for the next change in state.
If the active window is not one of your application any more, the keys, mentioned in the list, are reset to default.
Note
As mentioned earlier, the additional burden to the processor of the script is nihil. You could very well run it on startup, as explained in "How to use".
Affecting multiple applications
As discussed in comments, in OP's specific case, it is useful to apply disabling shortcuts on a group of applications, all residing in one directory.
Below a version to apply this on all applications of which the output of
will include a specific directory. In my example, I set the
/opt
directory, so if the active window is one of any of the applications in/opt
, the set shortcuts will be disabled.bringing a window of one of the applications in /opt to front will disable the logout shortcut
re- enabling the shortcut if another window gets focus
The script
How to use
Like the first script,
xdotool
needs to be installed:Copy the script into an empty file, save it as
disable_shortcuts.py
In the head of the script, replace in the line:
"/opt" by the directory your applications are.
Test-run the script by the command:
If all works fine, add it to Startup Applications: Dash > Startup Applications > Add. Add the command:
Adding other shortcuts to the list works exactly similar to version 1 of the script.
Does it work on all applications?
In your answer, you mention:
Windows with pid 0 (like tkinter windows, including Idle), have no window- id in the output of
xprop -id
.Idle
does not have any clashing shortcuts though in my experience. If you run into any application with pid 0 that would require disabling specific shortcuts, please mention.In that case, a possible escape would be to convert the output of
to hex, the format
wmctrl
uses, and subsequently look up the correspondingpid
in the output ofAlthough that seemed the most obvious thing to do to start with, I didn't use it in the script to keep the script as light-weight as possible.