4

in Ubuntu (or for that matter most other linux distros), I could use the shortcut ctrl+t to open a new tab (in firefox or similar), or I could use alt+tab to make unity switch highlighted window, or I could use alt+ctrl+F<1-6> to get to another tty. What part of linux handles and resolves these shortcuts? What if several programs/processes share the same shortcut, how is priority resolved?

(For the latter I'm assuming that this is only relevant for programs on different 'levels', e.g. firefox and the session script might compete, but firefox and chrome would never compete because they should not both be responding at the same time)

funklute
  • 237
  • 3
  • 7

2 Answers2

4

Think of your desktop as running a variety of programs always, even if they have no windows, terminals, or show up as daemons. Most are running a window manager, perhaps various toolbars, a desktop, and so forth.

Now let's see what happens when a key is pressed. The key press and key release are treated separately, though a library may deal with combining them for the programmer. The keyboard driver handles some things, such as treating the Shift key as a modifier rather than a discrete key. The driver itself may capture some key combinations for its own use, or it can throw it to a program.

Which program gets it? Programs with windows, as a standard, only get notified of keystrokes when they have input focus. A program can also "grab" certain keys for its own use. Alt+Tab is grabbed by Unity, denying any other program seeing it. Ctrl+T is grabbed by Firefox's menu system, but only when a Firefox window with a menu that wants to know about it has input focus.

So who determines who gets what? Besides input focus, as I already mentioned, it's often first-come, first-grabbed. Alt+Tab gets grabbed by Unity (and many other window managers), so a user program can rarely snatch it away. An end-user is at the mercy of the programs in use; if a program, such as vim, allows you to configure certain key combinations for various things, then you're good to go, as long as the program gets the keystroke.

4

What part of linux handles and resolves these shortcuts?

For the most part, individual applications or a window manager(WM)/desktop environment(DE). There are a few caught and handled by the kernel, such as VT switching with Cntl-Alt-F[N].

The actual event propagates:

  • From the kernel
  • To the Xorg server
  • To the WM/DE
  • To the application

If caught and handled at any point therein, it will probably not continue to the next level down.

If you run a (non-GUI) application inside a GUI terminal, the GUI terminal will have precedence over it.

What if several programs/processes share the same shortcut, how is priority resolved?

The WM/DE will take priority over the application.

goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • "If you run an application in a GUI terminal, the GUI terminal will have precedence over it" is incorrect in some cases. Running gvim, for example, will bring up the GUI version of vim, so the editor's GUI would get keyboard events as long as it had input focus. The "GUI terminal" would act just like any other GUI window, getting keystrokes only when its window has input focus. – Joe Sewell Jan 13 '15 at 18:42
  • @JoeSewell Sorry, I'll clarify that. By "in" I meant CLI/TUI apps (such as plain vim). – goldilocks Jan 13 '15 at 18:47