Как клавиатуры обрабатывают одновременные вводы?

750
Bennett Yeo

В последнее время я работаю с основными понятиями параллелизма в программировании. Одна мысль, которая пришла мне в голову, была одновременным вводом с клавиатуры. В программировании метод или функция вызывается через прослушиватель событий, который одновременно выполняет только одно нажатие клавиши. Программист должен запрограммировать код определенным образом для одновременного / многократного нажатия клавиш. Мне интересно, как аппаратно обрабатывает «по-настоящему одновременный» ввод. Существует ли какой-то механизм, который автоматически расставляет приоритеты для воспринимаемых входов в определенном порядке?

0
Ничто не является действительно одновременным. Метки времени могут иметь точность до миллисекунды ... DavidPostill 9 лет назад 0
так что совершенно невозможно добиться «одновременного ввода» нарочно или по чистой случайности? Bennett Yeo 9 лет назад 0

2 ответа на вопрос

0
user3767013

Each pressing or releasing of the key will be transfered from the keyboard to the computer through the USB cable, so there is no way for the keyboard to send true simultaneous key operations. The keyboard just send messages when a key is pressed or released. The computer will tell the application that two keys are pressed if one "key pressed" message arrives before the "key released" message for the last pressed key.

Хорошо, что он получает только 0 или 1. Интересно, может ли квантовая электроника достичь параллелизма ... Bennett Yeo 9 лет назад 0
0
Chenmunka

Taking your specific question about keyboard inputs. The computer receives keystrokes along a serial link, therefore the keystrokes are not received simultaneously as they must be - er - serial.

WIthin the keyboard itself is a small processor that reads keypresses and sends them out to the computer on the serial link. Let us consider the case where someone mananges to press two keys 'simultaneously' (whatever that actually means in terms of temporal precision). The keyboard processor is scanning a matrix of digital inputs which give it the keypresses. It will always see one press before another, simply because of its scan across the keys. Even in this scan takes microseconds, one press will be detected before the other.

It is indeed possible that, due to this scanning, the presses may be very close together but the second is seen first.

In the more general field of real-time process controls, it is likely that an interrupt mechanism is employed whereby inputs interrupt a processor to inform it of an event. Again, even if the events cannot be separated in time, one interrupt will get to the processor before the other.

Decently written event-driven software must allow for the fact that two events may be detected very close together and could actually be seen in reverse sequence.

Other sequencing gotchas can be found in cases of I/O processors reporting to a controller computer with a protocol that effectively aggregates inputs.

In summary, can truly simultaneous input be achieved - No.

Я подозревал, что эти клавиатуры используют «массив», и один «ввод» всегда будет регистрироваться первым, но теперь я точно знаю. Bennett Yeo 9 лет назад 0