eli5: How does a computer mouse work besides the basics?

172 views

Not how does it record the signal and send to the computer,

but how does moving my mouse over the back sign and clicking cause the actual webpage to change?

what’s actually happening inside the computer

In: 0

5 Answers

Anonymous 0 Comments

Hardware like a mouse are designed to send electrical signals. That goes for your keyboard, monitor and everything inside the computer – it’s all electrical signals. For devices that are external to the main board, like a mouse, you have a connection to an “outside” connection like USB – which can then be a wire to the mouse or wireless, regardless this device that plugs into the port will send/receive electrical signals following a specific protocol. Technically, the hardware signals are accumulated into registers/buffers that to the world of software are known as ports. Other electrical features like interrupts exist, but let’s keep it simple.

At that point, software is needed. Software can read the state of a large number of ports. Every device, every potential device, has a port. When the computer boots, it assign ports to things it sees plugged in, USB has it’s own ports. The first layer of programming is called the Operating System. For some that’s Linux, others that’s Window, IOS etc. – they will implement the next slightly different, but they do share the main lines. The OS will make a translation from ports to something a programmer understands. Ie. the OS will convert the USB device the mouse is on into a USB address, which a programmer can use to send/receive data to. Once this is done, the mouse’s protocol is implemented in an optional part of the OS, which Windows calls drivers. A driver is just a particular technical piece of program that translates the protocol into a standard set of actions defined by the OS. In most cases, to help programmers a GUI implements a “bus” – like a road way – where messages from devices like mice are sent and received. Every movement, every keypress, every event like opening an application, moving a window are one or more messages on this bus.

When the OS creates the GUI that makes windows (Window Manager) it uses this bus to read/write events to. The programmers of the GUI makes it easy for the programmers that make the software you run, easy. They make it so a “normal” programmer doesn’t have to known the whole OS, but instead can just concentrate on looking for the events that matter for their application. For most applications created, they simply need to create code in a function that may say myButton.onPush() – and the OS software calls this function when it determines the click of the mouse (determined by reading the port values) happened exactly on the button, and the OS often implements some graphical changes so it looks like the button is depressed into the screen. Then the programmers code is called, it may write “Hello World” to the screen, and return or something else.

The actual translation from the port values to x,y of the screen, which window is being clicked on, which line of text etc. is tricky and full of geometry/math. The OS records the cursor location right now, and uses the mouse movements relative to this. The mouse tracks (electrically) movements forward/backward/left/right and between. Some advance mice can record more directions, some mice have a ton of buttons, some have just one etc. It’s up to the OS to convert the movements into relative x,y values on the mouse and change those values. Changing them makes the OS change the display with a new mouse pointer location. When the port values indicate a mouse button push, a signal is sent on the bus that a button was pushed, and the OS determines what part of the OS needs to know about the push.

A computer in other words is a layered system. With hardware on top of hardware, software on top of software. The WAST majority of programmers do not see the hardware at all – they simply make functions that are automatically called when the OS/browser detects movement/pushes.

A browser is just an application. There’s no difference from a mouse perspective between a simple “hello world” with a few buttons and a browser, other than a browser is dynamically creating a page where most traditional programs have a more static look. Which means the browser programmers use the exact same “tricks” to react to a button push. Programmers can also react to a mouse moving over something, moving while holding a key down etc. and take different action depending on the user action.

ELI5 – the computer is complex but not magical. Software translates the hardware electrical signals into software ‘signals’ which programmers use to react to hardware actions.

You are viewing 1 out of 5 answers, click here to view all answers.