Can I connect 2 pins through a resistor and a switch

I have an on/off switch that I want to connect as an input method - I thought about connecting it with a 4k7 resistor between 2 digital pins, setting one as HIGH and reading the other so that I could tell whether the switch is on.
Is it safe? If yes what's the correct way to code it?

(deleted)

You could do that but you would have to disable the pullup resistor on the pin that is the input.

How come you do not want to connect a switch from a pin to ground ?

noweare:
You could do that but you would have to disable the pullup resistor on the pin that is the input.

What do you mean bu that?

I am using a minimal matrix for a compact project and I don't have any more place on it. I want to change modes of operation now and then and don't mind if it's cumbersome.

Why use 2 pins to do what 1 pin can do? What do you think you are gaining by using an IO pin in place of VCC?
You can use the internal pull up resistor and a normally open switch to ground on one pin using:
pinMode(pin, INPUT_PULLUP); and have a free pin to do something else with.
Or you can use an external pull up resistor like the image I attached.

Joe

pull-up-resistor.png

Check the example sketch DigitalInputPullup that ships with the IDE.

aarg:
Check the example sketch DigitalInputPullup that ships with the IDE.

Thanks!