Wrap it around in a function and call the function from within click event. This uses jQuery, but it is simple to change to "plain" javascript.
Short example:
function setKeyDown(selector) {
$(selector).on("keydown", "", function (e){
});
}
function buttonClick() {
setKeyDown("txtPrice");
setKeyDown("txtQuntity");
}
If this helps please take time to accept the solution.