2个或更多进程如何与键盘交互?

时间:2022-02-08 22:54:11

I have been thinking a lot over keyboard handling. How does it work? I can't seem to google me to a good explaining.

我一直在思考键盘处理问题。它是如何工作的?我似乎无法向我提出一个很好的解释。

I know that a keyboard interrupt is made every time a key is pressed. The processor halts whatever it is processing and load the keyboard data from the keyboard buffer, storing it in a system level buffer.

我知道每按一次键就会产生键盘中断。处理器停止正在处理的任何内容并从键盘缓冲区加载键盘数据,并将其存储在系统级缓冲区中。

But what happens next? Let's take a practical example. What happens when I run the following piece of code:

但接下来会发生什么?我们来看一个实际的例子吧。运行以下代码时会发生什么:

...
std::string s;
std::cin >> s;
....

Does the cin read from a user level representation of the system level keyboard buffer? That makes perfect sense in my head because then 2, or more processes can read from the same buffer, and by that way I don't loose any key presses. But does it work this way?

cin是否从系统级键盘缓冲区的用户级别表示中读取?这在我的头脑中是完全合理的,因为那么2个或更多的进程可以从同一缓冲区读取,并且通过这种方式我不会松开任何按键。但这样做有用吗?

I know I'm talking in very general terms. The OS I'm using is OS X.

我知道我的谈话非常笼统。我正在使用的操作系统是OS X.

1 个解决方案

#1


Except in rare situations, your keyboard and display are managed by a Window Manager: X11, Gnome, KDE, Carbon, Cocoa or Windows.

除极少数情况外,您的键盘和显示器由Window Manager管理:X11,Gnome,KDE,Carbon,Cocoa或Windows。

It works like this.

它的工作原理如下。

The keyboard driver is part of the OS.

键盘驱动程序是操作系统的一部分。

The window manager is a privileged process, which acquires the device during startup. The window manager "owns" the device. Exclusively.

窗口管理器是一个特权进程,它在启动期间获取设备。窗口管理器“拥有”设备。只。

  1. The interrupts go to OS.

    中断转到操作系统。

  2. The OS responds the interrupt by queueing. Eventually -- when there's nothing of a higher priority to do -- it captures the keyboard input from the interrupt and buffers it.

    OS通过排队响应中断。最终 - 当没有更高优先级的时候 - 它从中断中捕获键盘输入并缓冲它。

  3. The owning process (the window manager) is reading this buffer. From this, it creates keyboard events.

    拥有进程(窗口管理器)正在读取此缓冲区。从此,它创建了键盘事件。

Your application works through the window manager.

您的应用程序通过窗口管理器工作。

Example 1 -- You're running a command-line application. In a terminal window. When terminal window is front-most, the window manager directs events at the terminal window. Keyboard events become the stdin stream.

示例1 - 您正在运行命令行应用程序。在终端窗口。当终端窗口位于最前面时,窗口管理器在终端窗口指示事件。键盘事件成为stdin流。

Example 2 -- you're running GUI application. In your own application's window. When your application's window is front-most, the window manager direct events at your application window. Keyboard events are available for your various GUI controls to process. Some keyboard events may cycle among the controls or active buttons.

示例2 - 您正在运行GUI应用程序。在您自己的应用程序窗口中。当应用程序的窗口位于最前面时,窗口管理器会在应用程序窗口中指示事件。键盘事件可供您处理的各种GUI控件使用。某些键盘事件可能会在控件或活动按钮之间循环。

#1


Except in rare situations, your keyboard and display are managed by a Window Manager: X11, Gnome, KDE, Carbon, Cocoa or Windows.

除极少数情况外,您的键盘和显示器由Window Manager管理:X11,Gnome,KDE,Carbon,Cocoa或Windows。

It works like this.

它的工作原理如下。

The keyboard driver is part of the OS.

键盘驱动程序是操作系统的一部分。

The window manager is a privileged process, which acquires the device during startup. The window manager "owns" the device. Exclusively.

窗口管理器是一个特权进程,它在启动期间获取设备。窗口管理器“拥有”设备。只。

  1. The interrupts go to OS.

    中断转到操作系统。

  2. The OS responds the interrupt by queueing. Eventually -- when there's nothing of a higher priority to do -- it captures the keyboard input from the interrupt and buffers it.

    OS通过排队响应中断。最终 - 当没有更高优先级的时候 - 它从中断中捕获键盘输入并缓冲它。

  3. The owning process (the window manager) is reading this buffer. From this, it creates keyboard events.

    拥有进程(窗口管理器)正在读取此缓冲区。从此,它创建了键盘事件。

Your application works through the window manager.

您的应用程序通过窗口管理器工作。

Example 1 -- You're running a command-line application. In a terminal window. When terminal window is front-most, the window manager directs events at the terminal window. Keyboard events become the stdin stream.

示例1 - 您正在运行命令行应用程序。在终端窗口。当终端窗口位于最前面时,窗口管理器在终端窗口指示事件。键盘事件成为stdin流。

Example 2 -- you're running GUI application. In your own application's window. When your application's window is front-most, the window manager direct events at your application window. Keyboard events are available for your various GUI controls to process. Some keyboard events may cycle among the controls or active buttons.

示例2 - 您正在运行GUI应用程序。在您自己的应用程序窗口中。当应用程序的窗口位于最前面时,窗口管理器会在应用程序窗口中指示事件。键盘事件可供您处理的各种GUI控件使用。某些键盘事件可能会在控件或活动按钮之间循环。