Use a can of compressed air or a vacuum hose to remove the debris underneath the key. Go to the iFixit website if you need instructions on how to remove your key. If that does not work, you may actually have a hardware issue.
If you are still under warranty or have Apple Care, call them and get a case number. Otherwise, take the computer to an Apple store or an AASP. Whichever is more convenient for you.
I tried doing a hardware test but couldn't get my computer to boot up in the right mode
Maybe you were not doing it right - Intel-based Macs: Using Apple Hardware Test