In your code below ANSELD = 1; write these code:
Check all your connections on the keypad and make sure your keypad is not making any contact if not pressed. Check continuity between column 3 and all the rows line. If you don’t press 3, they must not be any continuity.
If possible, try to test with a different keypad.