Added distance calculation function based on layout and last key pressed.