Let us say that somebody is going on an outing on Yom Tov and is worried that were he to leave his home unlocked, thieves would come and steal his Yom Tov food. Under these circumstances, he may lock the door and take the keys with him, as this carrying is for a Yom Tov need. Similarly, one may carry the keys for rooms that contain clothing or jewelry which could be worn on Yom Tov.
However, poskim disagree as to whether one who leaves his home may take the key to his safe, if he is afraid that while he is away thieves will steal his money. Some maintain that this is prohibited, as carrying is permitted only for the enjoyment of Yom Tov, but not in order to avoid monetary loss (Rosh; Maharil). Others maintain that alleviating a concern is also considered a Yom Tov need, and therefore one is permitted to carry his safe key (Hagahot Smak; Rema 518:1). In practice, one who wishes to be lenient has an opinion on which to rely. Le-khatḥila, it is better to leave the key with neighbors. If he still wishes to take the key with him, it is preferable to carry it with a shinui, for example in his sock or hat.[3]
In contrast, one may not carry something in the public domain for a non-Jew, even if the non-Jew promises him food in return for his service. This is because there is no direct link between the carrying and the food preparation. Even if the non-Jew threatens to steal his food if the Jew refuses to carry something for him through the public domain, he still may not desecrate Yom Tov for this. This is because doing melakha that is necessary to prepare food or that directly contributes to Yom Tov enjoyment is permissible, but doing a melakha that has an indirect link to acquiring or safeguarding food is not (R. Shlomo Zalman Auerbach in SSK ch. 19 n. 17).