Keyboard key stuck or not being recognized; how to fix
There are two scenarios which you will encounter:

keys that don't respond when pressed
keys that are stuck; they act like they are being held down

The first case is potentially salvageable through the use of software to remap a key, or just changing your behavior when possible (i.e. use right Cmd  instead of left Cmd).
The second case, where the key is acting like it's pressed, the keyboard will likely have to be replaced because there is a physical connection being made and it's being sent to the logic board to be interpreted.
Key not Responding
If a key isn’t responding it means that the electrical connection mapping the key is blocked.  Remapping keys can be done via 3rd party tools such as Karabiner or Karabiner Elements (Sierra and up).
However, there's only so much remapping you can do and this isn't practical in the pre-boot stage (i.e. ⇧Shift keys or ⌥Option keys don't work).  This means if you need to go into Safe Mode (hold ⇧Shift, run Apple Hardware Test (hold D) or any other boot time key sequences, it won't work; you'll need an external USB keyboard.   You can remap once macOS has loaded, but not before.
With this scenario, you may be able to get around the unresponsive keys, but eventually you will have to replace it.
Key is Stuck
The keyboard needs to be replaced.  There is a contaminant that is shorting electrical connections.  When you press a key, you are intentionally creating this short, but a liquid like water or soda (which can conduct electrical current) can bridge the connections closing the loop.  In these cases, they keyboard needs to be removed and cleaned with an electrical solvent.
Given the amount a labor to do this (there are a TON of screws – about 60 in total), it’s much more cost effective just to replace they keyboard with a new one.
The Butterfly Keyboard Issues
This applies to the 2016 through 2018 MacBook Pros and covers the gen 1, 2 and 3 keyboards.
The butterfly keyboard is a mechanical design change from the traditional keyboard mechanism.  The underlying electronic circuitry remains the same.

The &quot;shallower&quot; profile allows for less space requirements resulting in a thinner profile for the laptop.  However, a thinner profile means tighter operating tolerances. Contaminants and regular wear are now much more likely to contribute to premature failure resulting in either stuck or non-responsive keys.
Replacing the Keyboard
Apple will traditionally replace your top case assembly. This is the aluminum housing that holds all the internals.

Note:  For the Early 2015 through 2017 MacBook and 2016 through 2017 MacBook Pros, Apple has acknowledged the issue and is replacing the top case assembly through a Service Program.  However, they aren't replacing the gen 1 or gen 2 keyboards with the new gen 3 design.  Basically, they'll replace it with the same (flawed) model.  (IMO this puts owning an Apple MacBook/Pro on par with owning a Jaguar).
To replace this, you will have to remove the battery, fans, speakers, and logic board to swap the internals to the new assembly.  If you are going to go this far, just replace the keyboard and save yourself the money.
Replacement MacBook Pro keyboards are relatively inexpensive (13&quot; MacBook Pro Keyboard and 15&quot; MacBook Pro Keyboard) versus a top case assembly - $25-$30 versus $250-$400.
iFixit.com has excellent guides to walk you through, but if you are unsure of your tech skills it's best to get a pro to do the job.

How a Keyboard Works
To understand all of this better, I &quot;dissected&quot; a (dead) MacBook Pro keyboard to better show how all of this comes together
Keyboard Internals
The keyboard is made up of multiple layers:

Keys (mechanical)
Plastic membrane
Circuit trace
Backplate

Looking at the back of the keyboard (a dead donor from a 2009 MacBook Pro that had to be cut out due to a stripped screw) there is a black protective cover.  The only electrical contacts that can be seen is for the backlight and the keyboard connector interface

Removing the back cover, we can get a better look at the backplate.  This piece provides the stability and structure for the keyboard.

Looking at the front of the keyboard with the keys removed, we can see through the plastic membrane layers all the way to the back plate (this portion is where the stripped screw was that had to be cut out)

Taking an even closer look at the key, we can see how the mechanism attaches physically to the back plate to keep it in place.  The rubber cup is what provides the “spring” action to return the key to its unpressed position.

Peeling back the layers we can see how it all comes together.  The plastic membrane with the rubber cups on top of the plastic membrane with the electrical traces on top of the metal back plate.

Cutting out a section of the trace membrane we can see how the electrical connections are made.  Looking closely, you can see that each key has a vertical connection with the key above/below it and a horizontal connection between the keys to the right/left.

A keyboard works by completing the circuit between two circuits in a “grid” pattern.  This is how we can get 100+ keys with only 29 circuits.  For example, to get the letter “P” using the keyboard diagram below, it would create a closed circuit between Pin 1 (&quot;INT 5&quot;) and Pin 8 (&quot;1Y1&quot;).

TL;DR - What this all means...

Key(s) are stuck.  The circuit path is shorted.  This could be due to a foreign contaminant (including the key mechanism itself) putting pressure on the contact pad or some sort of FOD (foreign object debris) shorting out the circuit path.

Key(s) not responding.  The circuit path is broken.  When you press a key and nothing happens, no signal is being sent for the keyboard to recognize a key press.  This could be due to a short, bad contacts, a physically broken path, etc.


A note on intermittent problems.  An intermittent electronic failure is a subset of the above conditions. Dry solder joints1, decayed glue2, and even bad/failed solid state components (i.e. resistors, capacitors, ICs, etc.)3; any/all of these can play a role in a circuit path failure.

1 Dry joints are the main form of problem solder joint. These solder joints may be completely open circuit, or they may be intermittent, high resistance or noisy. It is therefore essential that no dry solder joints are present in any electronics equipment. Source: Electronic Notes - Solder Joints
2 An electrically conductive adhesive is a glue that is primarily used for electronics. The electric conductivity is caused by a component that makes ca. 80% of the total mass of an electrically conductive adhesive. Beware of Decayed Glue under IC
3  What causes this resistor to fail - open circuit and no visible damage?