It's the keyboard ribbon cable. I had accidentally pulled on it (thinking it was the battery cable) then the problem arose on my 2012 15" MacBook Pro Retina, "Are you sure you want to shut down your computer?" Kept popping up randomly and even logging in without hitting a key, the computer would turn off or screen would go black. I'd have to hit a key while logging in or keep pushing "Cancel" every 6 to 15 seconds. Although while holding a key down, the computer functioned normally.
Solution: Clean the ribbon cable with alcohol and reposition it back properly. Hasn't happened since.