Your program looks okay.
First, you may check the way how you connect the small wires to the micro switch.
If you connect like this way, the output is 0 when press the key and is 1 when unpressed.
If you connect like this way, the output is 1 when press the key and 0 when unpressed.
Secondly, check if you have connected the MegaPi to a battery and if the power switch on MegaPi is pulled to ON.