15
HANDLING EVENTS
Some programs work with direct user input, such as mouse and keyboard actions. That kind of input isn’t available ahead of time, as a well-organized data structure—it comes in piece by piece, in real time, and the program must respond to it as it happens.
Event Handlers
Imagine an interface where the only way to find out whether a key on the keyboard is being pressed is to read the current state of that key. To be able to react to keypresses, you would have to constantly read the key’s state to catch it before it is released again. It would be dangerous to perform other time-intensive computations, since you might miss a keypress.
Some primitive machines handle input like this. A step up from this is for the hardware or operating ...
Get Eloquent JavaScript, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.