Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do event listeners work?

Tags:

Do they repeatedly check for the condition and execute if the condition is met. Ex, how the OS knows exactly when a USB device is plugged in, or how MSN knows exactly when you get an email. How does this work?

Thanks

like image 589
jmasterx Avatar asked Jul 05 '10 21:07

jmasterx


People also ask

How do js event listeners work?

Event listeners are called only when the event happens in the context of the object they are registered on. That example attaches a handler to the button node. Clicks on the button cause that handler to run, but clicks on the rest of the document do not. Giving a node an onclick attribute has a similar effect.

What do event listeners do?

An event listener is a procedure or function in a computer program that waits for an event to occur. Examples of an event are the user clicking or moving the mouse, pressing a key on the keyboard, disk I/O, network activity, or an internal timer or interrupt.

How does click listener work?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.

How does listeners work in Java?

An event listener in Java is designed to process some kind of event — it "listens" for an event, such as a user's mouse click or a key press, and then it responds accordingly. An event listener must be connected to an event object that defines the event.


2 Answers

At the low level, the OS kernel "knows" when something happens, because the device in question sends the CPU a hardware interrupt.

So when, say a network packet arrives, the network controller sends an interrupt, and the OS kernel responds as appropriate.

At the program level, it works quite differently - most application programs run an "event loop", where they fetch a message (say, a message from the OS saying that "the mouse was clicked on this point in your application"), perform the appropriate actions in response to that, and then, listen for more messages. If there is no message, the OS sleeps the thread until it has a message to deliver.

like image 123
Anon. Avatar answered Oct 06 '22 22:10

Anon.


Take a look at Interrupts this should explain how the hardware initiates certain 'events'

like image 28
Dog Ears Avatar answered Oct 07 '22 00:10

Dog Ears